Click here to Skip to main content
13,000,324 members (56,440 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I have a black and white picture on which I want to remove white areas that are more than 50 pixels.
I found bwareaopen in MATLAB for this purpose. I am using EMGU and want equivalent in EMGU.

Thank you for helping.
Posted 2-Oct-11 6:19am
J imran1.3K
Updated 2-Oct-11 8:12am
André Kraak 2-Oct-11 13:07pm
Please share any relevant code with us, seeing the code might us help understand the problem you are facing.

If you wish to change your question use the Improve Question button.

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1


Well in short NO there is not a built in function that performs the same operation as bwareaopen.

You will simply have to loop through your image and look for a white pixel.

When you find 1 make a blank copy or a list of points that records all pixels attached to the original that are also white. If it is less than 50 pixel then set all these co-ordinates to black else leave it. Use the collection of lists or an overall list/image map to make sure you don't look at pixels you've already checked.

While this process may seem slow it isn't when your talking small areas of white however you may wish to look at some optimisation if a lot of your image is white. Say reduce your image 50% and look for areas less than 10 pixels.

You speed will be comparable to bwareaopen as this is performing the same operation.


I have found a method of implementing a bwareaopen on the opencv yahoo group many thanks to M.Klien for converting the method.[^]

In EMGU the equivalent code in a method is:

private Image<Bgr, byte > bwareaopen(Image<Bgr, byte > Input_Image, int threshold)
    Image<Bgr, byte> bwresults = Input_Image.Copy();
    using (MemStorage storage = new MemStorage())
        for (Contour<Point> contours = Input_Image.Convert<Gray, byte>().FindContours(Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_SIMPLE, Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_LIST, storage); contours != null; contours = contours.HNext)
            Contour<Point> currentContour = contours.ApproxPoly(contours.Perimeter * 0.05, storage);
            if (currentContour.Area < threshold) 
                for (int i = currentContour.BoundingRectangle.X; i < currentContour.BoundingRectangle.X + currentContour.BoundingRectangle.Width; i++)
                    for (int j = currentContour.BoundingRectangle.Y; j < currentContour.BoundingRectangle.Y + currentContour.BoundingRectangle.Height; j++)
                        bwresults.Data[j, i, 0] = 0;
                        bwresults.Data[j, i, 1] = 0;
                        bwresults.Data[j, i, 2] = 0;
    return bwresults;

Cheers, Chris
C_Johnson 30-Nov-11 12:12pm
I hate the formatting on here at times, I will keep trying to get the code up here correctly!...

At last, these ">" characters are a pain

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170624.1 | Last Updated 30 Nov 2011
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100