Ask Your Question

How to use connectedComponentsWithStats to remove dots?

asked 2017-05-02 01:18:18 -0500

Luya gravatar image

updated 2017-05-02 01:32:28 -0500

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 wiht ConnectedComponentsStats?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2017-05-02 03:08:47 -0500

kbarni gravatar image

updated 2017-05-02 03:13:01 -0500

Probably connectedComponentsWithStats is not the most efficient way to remove dots. I would rather use the erosion or the opening (erosion+dilatation) morphology operator.

If you want to stuck with connectedComponentsWithStats, the code (C++) will be:

for(int i=0;i<stats.rows;i++)    //for each label 
    if(<ushort>(i,CC_STAT_AREA)<=3)    //if area<3
        for(int<float>(i,1)-1;y<<float>(i,1)+1;y++)   // scan the neighboring pixels
                if(<ushort>(x,y)==i)<uchar>(x,y))=0;   // if the label matches, set the current pixel to 0

I commented the code, so it will be easier to rewrite in Python.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower


Asked: 2017-05-02 01:18:18 -0500

Seen: 535 times

Last updated: May 02 '17