Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Python, eliminate small contours

I'm attempting to do the same thing as https://answers.opencv.org/question/120499/how-to-eliminate-small-contours-in-a-binary-image/ but in Python and I am working from their solution. I am getting an error "SystemError: <built-in function="" drawcontours=""> returned NULL without setting an error". I also suspect I am doing other things wrong as I don't know what the Vector lines are for and I removed the Point() at the end of the findCountours function as it was causing errors. Here is what I have, I appreciate any help:

import numpy as np 
import cv2

# Mat result
# vector<vector<Point> >contours
# vector<Vec4i>hierarchy
savedContour = -1
maxArea = 0.0

# load image
image = cv2.imread('vlcsnap2.png')
binarayImage = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Find the largest contour
contours, hierarchy = cv2.findContours(binarayImage, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
for i in range(0, len(contours)):
    area = cv2.contourArea(contours[i])
    if area > maxArea:
        maxArea = area
        savedContour = i

# Create mask
result = cv2.drawContours(binarayImage, savedContour, 255, cv2.FILLED, 8)

# apply the mask:
binarayImage &= result

#show image
cv2.imshow("Result", result)

cv2.waitKey(0)
cv2.destroyAllWindows()

Ultimately, I'm trying to get from here:

image description

to Here:

image description