"Fill" Binary Image

Hi,
Is there a way to "fill" a binary image such that textured regions become black? As an example, I would like to make this first image:
Look like this second image:
Note how the wall towards the left of the image appears mainly white in the first image. Is there a way to darken it so that it becomes more like that shown in the second image?
I have tried using morphological operators such as:
filled = bwmorph(img, 'fill', 100);
But this doesn't seem to have any affect. I am desperate to hear your thoughts.
Thanks for your help!

 Accepted Answer

Walter Roberson
Walter Roberson on 2 Jun 2011

1 vote

I believe you would want to either erode the white or dilate the black, (possibly followed by the reverse operation), so as to remove small features.
Or alternately, you could bwlabel() and regionprops() and remove the regions of sufficiently small area. That might not get all of the texture lines though.

More Answers (2)

Image Analyst
Image Analyst on 3 Jun 2011

1 vote

You can try imopen if you want to maintain the size - this does an erosion (local min) followed by a dilation (local max). To enlarge the black area, just use imerode. To get rid of small things, use bwareaopen, which does labeling and regionprops internally like Walter said. But your images were not created by any morphological operation of the first one to produce the second one. I'm virtually certain that they were produced by thresholding with different threshold values, because there's no other way to produce the texture in the sky region of the second image - it simply isn't in the first image and there's no way to get that out of a uniform white area. I'd bet my next paycheck on it.
Philip
Philip on 2 Jun 2011

0 votes

Thanks for your help! I really must read the literature for morphological operations... The erode and dilate methodology seems to have done the trick here!
Thanks again!!

16 Comments

Hi Philip, this book is excellent to dig deeper into morphological image processing.
Morphological Image Analysis; Principles and Applications by Pierre Soille, ISBN 3540-65671-5 (1999), 2nd edition (2003)
Cheers, W.
Philip
Philip on 3 Jun 2011
Thanks for your help! I will certainly take a look at that book you recommended!
I managed to get everything working with the two images I posted before... However, it is not working for buildings that are quite reflective. Is there a morphological operator that would darken the building area of this image?
http://imageshack.us/photo/my-images/219/reflective.png/
I have tried simply eroding the image more times, but the result is far too blocky...
If there is a connected, black outline around the building, you can use the imfill function to fill the building.
Philip
Philip on 3 Jun 2011
Thanks! I have applied the 'imfill' function but it seems to only fill small holes in the image. Is there a way to specify a larger fill size?
Like I said in my answer, the two images were generated from thresholding a gray scale image, not from morphologically processing one to get the other. So your best bet is to begin with the original grayscale or color image. Are you not able to get that for some reason? If you can, you can do something like color segmentation or texture segmentation, though anything you do will probably be only applicable to that one image, not all images in the universe that you might possibly capture.
Philip
Philip on 3 Jun 2011
Thanks! Yes, I do have access to the grayscale versions of the images. In fact, I have access to both full-color images (the images were taken from the same camera, but at different exposures). I have converted both of the color images to grayscale and performed some very naîve colour segmentation:
img_gray = rgb2ycbcr(img);
img_gray = img_gray(:,:,1);
seg_img2 = img_gray;
seg_img2(img_gray > 0 & img_gray <= 100) = 0;
seg_img2(img_gray > 100 & img_gray <= 150) = 128;
seg_img2(img_gray > 150 & img_gray <= 255) = 255;
to produce the images shown previously. The morphological operators have been useful to blacken the 'whiter' images, but it doesn't work for cases like the second example, where there are large windows (that appear white).
Philip
Philip on 3 Jun 2011
The texture segmentation solution you suggested, sounds like it should work here... Do you have any methods in mind?
Can you show us the grayscale image?
Philip
Philip on 3 Jun 2011
Certainly:
http://imageshack.us/photo/my-images/97/imgjg.jpg/
That appears to be an image of something different. In the original two images, there is a brick wall going most of the way up the left side, but in this new image, the only brick wall is much smaller and only goes part way up.
Philip
Philip on 3 Jun 2011
Sorry! As mentioned, I have had some success with the first image. But this second image I mentioned later is causing problems.
I don't think I caught what the actual goal is? Is it to differentiate between sky and non-sky?
Philip
Philip on 3 Jun 2011
yes that's the first goal. Ultimately I want to detect the longest, straightest edge from the image (I didn't mention that part previously...). But since the two differently exposed images are 'whiter' and 'darker' than each other, I'm having a hard time finding a solution that works for both...
Longest straightest boundary, or longest straightest edge? An edge could, for example, be part of a texture. A band of ventilation grills running along a building that had an irregular rough could find the band to be the longest straight edge ?
Philip
Philip on 3 Jun 2011
My terminology is poor. The edge must be the edge of an object; so a boundary. It shouldn't be an edge of anything in the texture. I have therefore been using erode and dilate to remove some of this detail first... I'm not sure if that is the best strategy though.
Have you considered using Hough or houghlines for finding lines? Like I said before your images were binarized from a gray scale image. I don't think they are the result of different exposures unless your gain is so incredibly high that the different exposures cause binarization in the first place. Why don't you post the original gray scale or color images and say what you are really trying to find instead of saying things like I want to find the longest line. If you want to find a horizon, say so. Because maybe the longest line won't get you that in all situations.

Sign in to comment.

Categories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!