-2

With contour,I can do it like this:

if cv2.contourArea(cntr) <= 3:
      cv2.drawContours(img, [cntr], -1, (0, 0, 0), 1)

How to do it with ConnectedComponentsStats?

Alex Luya
  • 9,412
  • 15
  • 59
  • 91
  • similar question but getting this error? http://stackoverflow.com/questions/44087581/opencv-3-with-python3-getting-this-error-215-npoints-0-in-function-drawcon – Aleks May 20 '17 at 15:29

1 Answers1

2

If you want to do the same with connectedComponentsWithStats these are the following steps:

// This here is for making a custom image
img = np.zeros((500,500,3),dtype=np.uint8)
for i in xrange(1,5):
    img = cv2.circle(img, (i*80,i*80), 5, (255,255,255), -1)     
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

Custom image

// CCA from here
labelnum, labelimg, contours, GoCs = cv2.connectedComponentsWithStats(gray)

for label in xrange(1,labelnum):
    x,y = GoCs[label]
    img = cv2.circle(img, (int(x),int(y)), 1, (0,0,255), -1)    

    x,y,w,h,size = contours[label]
    if size <= 100:
        img = cv2.rectangle(img, (x,y), (x+w,y+h), (255,255,0), 1)    

This gives the following image:

CCA image

Hope it helps!

Rick M.
  • 3,045
  • 1
  • 21
  • 39