-1

I have read this:

Remove spurious small islands of noise in an image - Python OpenCV

erosion/dilation are suggested,and just wondering is it possible to do same thing by using connectedComponectsStats?I have googled,got these:

http://www.itkeyword.com/doc/5188188731707068417/how-to-use-opencvs-connected-components-with-stats-in-python

http://docs.opencv.org/3.0-beta/doc/py_tutorials/py_feature2d/py_features_harris/py_features_harris.html#corner-with-subpixel-accuracy

but from these,I can't figure out how to remove dots,so would you please tell me how to do it,thanks in advance!

update: What I really want to do is extracting human shape from blow image by remove small dots.

demo image

Community
  • 1
  • 1
Alex Luya
  • 9,412
  • 15
  • 59
  • 91
  • Using stats, you could use the flag `cv2.CC_STAT_AREA` to remove dots with respect to their area. Additionally, if you know the location of the dots to remove you could use the `centroid`. I don't quite get what you don't understand. – Rick M. May 01 '17 at 12:05
  • @RickM.,thanks,I think a area is produced by a ploygon(should be very small),I don't know how to remove this small polygon.With controid and boudaries in hand,I can only remove a rectangle,am I right or I missunderstand something? – Alex Luya May 02 '17 at 00:06
  • The area corresponds to the area of the contour. This doesn't necessarily have to be a contour. What exactly do you mean by dots? Can you upload an image? – Rick M. May 02 '17 at 07:44
  • @RickM.,I added an image,and what I really want to do is extract human shape from image. – Alex Luya May 02 '17 at 11:52
  • I've added an answer, accept it or comment if it helps you or not! – Rick M. May 03 '17 at 07:49

1 Answers1

3

Using connectedComponentsWithStats you can do something like this:

img = cv2.imread(directory + "canny.png")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
labelnum, labelimg, contours, GoCs = cv2.connectedComponentsWithStats(gray)
for label in xrange(1, labelnum):
    x,y,w,h,size = contours[label]
    if size <= 50:
         img[y:y+h, x:x+w] = 0
cv2.imwrite(directory + "cca_image.png", img)

For the image you uploaded, I got this as the result :

Result image

Does this work for you? I guess dilation/erosion before calling connectedComponentsWithStats would help, but I leave that for you to decide.

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