MATLAB Answers

How can i remove the sky from this image. (About background removal question)

11 views (last 30 days)
How can i remove the sky from this image.
i must use this following command as guideline to complete this problem. (IMSHOW , IMREAD , ZEROS)
A = imread('Problem09.jpg');
imshow (A);
but "zeros" how can i use it ?
Someone can guide me how to slove this problem ?
Thanks in advance.


Sign in to comment.

Accepted Answer

Kenneth Eaton
Kenneth Eaton on 28 Feb 2011

I'm unclear how ZEROS is to be used for this problem, but you could do it with relational operators.

The image is a 3-D RGB (Truecolor) image, which you can see after you load it using IMREAD:

>> img = imread('sky_birds.jpg');  % Load the image
>> ndims(img)                      % Display the number of dimensions
ans =

Now, each "plane" of the image represents a weighting for a particular color for every pixel. Plane 1 (i.e. img(:,:,1) ) is the red plane, plane 2 (i.e. img(:,:,2) ) is the green plane, and plane 3 (i.e. img(:,:,3) ) is the blue plane. Since your sky is mostly blue, you can try to select the pixels for the sky by picking values in the blue plane that have a high value. With some trial and error, you can select a threshold that helps you select primarily sky pixels:

>> mask = img(:,:,3) > 150;  % Find blue values greater than 150

The variable mask is now a 2-D logical matrix with 1 for pixels with high blue values and 0 for pixels with low blue values. Here's what it looks like:

>> imshow(mask);

Notice that this also selects pixels on the birds, because the birds are partially white and the color white is composed of high amounts of red, green, and blue.

Now, if you want to set the white pixels in the mask to be white in the image (which they already are for the birds, so no worries), you have to apply the mask to each color plane, setting the mask pixels to a maximum value of 255 (since you are dealing with unsigned 8-bit integers). You can do this by replicating the 2-D mask matrix into a 3-D matrix of the same size as the image matrix:

>> mask3d(:,:,1) = mask;  % For the red plane
>> mask3d(:,:,2) = mask;  % For the green plane
>> mask3d(:,:,3) = mask;  % For the blue plane

And now you can modify and display your image:

>> img(mask3d) = 255;  % Set masked pixels to white
>> imshow(img);

This looks very similar to the output you're trying to get.

If you want to learn more about image processing, I strongly suggest checking out Steve Eddins' blog.

More Answers (1)

Sean de Wolski
Sean de Wolski on 28 Feb 2011
I don't see how you would use ZEROS. I might use FALSE AND > OR < AND EDGE.

Community Treasure Hunt

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

Start Hunting!