Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matlab Bwareaopen equivalent function in OpenCV

I'm trying to find similar or equivalent function of Matlabs "Bwareaopen" function in OpenCV?

In MatLab Bwareaopen(image,P) removes from a binary image all connected components (objects) that have fewer than P pixels.

In my 1 channel image I want to simply remove small regions that are not part of bigger ones? Is there any trivial way to solve this?

like image 317
Miha Avatar asked Feb 27 '10 17:02

Miha


3 Answers

Take a look at the cvBlobsLib, it has functions to do what you want. In fact, the code example on the front page of that link does exactly what you want, I think. Essentially, you can use CBlobResult to perform connected-component labeling on your binary image, and then call Filter to exclude blobs according to your criteria.

like image 102
tzaman Avatar answered Nov 10 '22 23:11

tzaman


There is not such a function, but you can 1) find contours 2) Find contours area 3) filter all external contours with area less then threshhold 4) Create new black image 5) Draw left contours on it 6) Mask it with a original image

like image 39
yehuda Avatar answered Nov 11 '22 00:11

yehuda


I had the same problem and came up with a function that uses connectedComponentsWithStats():

    def bwareaopen(img, min_size, connectivity=8):
        """Remove small objects from binary image (approximation of 
        bwareaopen in Matlab for 2D images).
    
        Args:
            img: a binary image (dtype=uint8) to remove small objects from
            min_size: minimum size (in pixels) for an object to remain in the image
            connectivity: Pixel connectivity; either 4 (connected via edges) or 8 (connected via edges and corners).
    
        Returns:
            the binary image with small objects removed
        """
    
        # Find all connected components (called here "labels")
        num_labels, labels, stats, centroids = cv2.connectedComponentsWithStats(
            img, connectivity=connectivity)
        
        # check size of all connected components (area in pixels)
        for i in range(num_labels):
            label_size = stats[i, cv2.CC_STAT_AREA]
            
            # remove connected components smaller than min_size
            if label_size < min_size:
                img[labels == i] = 0
                
        return img

For clarification regarding connectedComponentsWithStats(), see:

How to remove small connected objects using OpenCV
https://www.programcreek.com/python/example/89340/cv2.connectedComponentsWithStats https://python.hotexamples.com/de/examples/cv2/-/connectedComponentsWithStats/python-connectedcomponentswithstats-function-examples.html

like image 40
Thodor Avatar answered Nov 10 '22 22:11

Thodor