how to delete objects in image using opencv

Viewed 39

I'm relatively new to openCV, my requirement is to delete smaller island (could not attach image because of post limitation) I have followed below code: (1)Applied background subtraction to keep only moving objects (2)Applied Erosion Dilation to fill the holes (3)calculated the Average area of objects (contours) (4)Now I want to keep only objects which is greater than Average area(Here is where i need literally help, i tried online but could not find a solution)

from __future__ import print_function
import cv2 as cv
import argparse
import numpy as np

array = [None] * 100
parser = argparse.ArgumentParser(description='')
parser.add_argument('--input', type=str, help='Path to a video or a sequence of image.', default='vtest.avi')
# parser.add_argument('--algo', type=str, help='Background subtraction method (KNN, MOG2).', default='MOG2')
parser.add_argument('--algo', type=str, help='Background subtraction method (KNN, MOG2).', default='KNN')
args = parser.parse_args()

# create Background Subtractor objects
if args.algo == 'MOG2':
    backSub = cv.createBackgroundSubtractorMOG2()
else:
    backSub = cv.createBackgroundSubtractorKNN()

capture = cv.VideoCapture("test.mp4")
if not capture.isOpened():
    print('Unable to open: ' + args.input)
    exit(0)

while True:
    ret, frame = capture.read()
    if frame is None:
        break

    # update the background model
    fgMask = backSub.apply(frame)
    # Erosion and Dilation
    kernel = np.ones((3, 3), np.int8)
    erosion = cv.erode(fgMask, kernel, iterations=1)
    dilation = cv.dilate(erosion, kernel, iterations=2)
    ## [apply]
    # contours, hierarchy = cv2.findContours(dilation, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
    contours, hierarchy = cv.findContours(dilation, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
    print("Number of Contours = " + str(len(contours)))
    # Find max contours
    # contour = max(contours, key=len)
    '''contourImg = cv.drawContours(frame, contour, -1, (0, 255, 0), 3)
    cv.imshow("Contours", contourImg)'''

    Contour_Cnts = 0
    Total_Area_Of_all_Contour = 0
    Average_Area_Of_Contour = 0
    for cnt in contours:
        area = cv.contourArea(cnt)
        Total_Area_Of_all_Contour = Total_Area_Of_all_Contour + area
        Contour_Cnts = Contour_Cnts + 1
    Average_Area_Of_Contour = Total_Area_Of_all_Contour // Contour_Cnts
    print("Number of contours", Contour_Cnts)

    Contour_Cnts = 0
    index = 0
    for cnt in contours:
        Contour_Cnts = Contour_Cnts + 1
        area = cv.contourArea(cnt)
        if area > Average_Area_Of_Contour:
            index = int(index) + 1
            # array[index] = contours[Contour_Cnts]
    # cv.drawContours(frame, array, -1, (0,0,255), 2)

    cv.imshow('Frame', frame)
    cv.imshow('FG Mask', fgMask)
    cv.imshow('Dilation', dilation)

capture.release()
cv.destroyAllWindows()
0 Answers
Related