views:

392

answers:

1

Hey guys,

I would like to know how to mask part of an image that is in BLACK & WHITE ?

I got an object that needs to be edge detected, but I have other white interfering objects in the background that are below the target objet ... I would like to mask the entire lower part of an image to black, how can I do that ?

Thanks !!

EDIT

I also want to mask some other parts (top part) ... how can I do that ?

Please explain the code because I really wnat to learn how it works and implement it in my own understanding.

EDIT2

My image is 480x640 ... Is there a way to mask specific pixels ? for example 180x440 from the image ...

+2  A: 

If you have a 2-D grayscale intensity image stored in matrix A, you can set the lower half to black by doing the following:

centerIndex = round(size(A,1)/2);         %# Get the center index for the rows
A(centerIndex:end,:) = cast(0,class(A));  %# Set the lower half to the value
                                          %#   0 (of the same type as A)

This works by first getting the number of rows in A using the function SIZE, dividing that by 2, and rounding it off to get an integer index near the center of the image height. Then, the vector centerIndex:end indexes all the rows from the center index to the end, and : indexes all the columns. All of these indexed elements are set to 0 to represent the color black.

The function CLASS is used to get the data type of A so that 0 can be cast to that type using the function CAST. This may not be necessary, though, as 0 will probably be automatically converted to the type of A without them.

If you want to create a logical index to use as a mask, you can do the following:

mask = true(size(A));  %# Create a matrix of true values the same size as A
centerIndex = round(size(A,1)/2);  %# Get the center index for the rows
mask(centerIndex:end,:) = false;   %# Set the lower half to false

Now, mask is a logical matrix with true (i.e. "1") for pixels you want to keep and false (i.e. "0") for pixels you want to set to 0. You can set more elements of mask to false as you wish. Then, when you want to apply the mask, you can do the following:

A(~mask) = 0;  %# Set all elements in A corresponding
               %#   to false values in mask to 0
gnovice
Thanks for the code :) Can you please explain what the code does in detail ? I would like to learn how it works :) Can you also tell me how can I also mask a specific part instead of just half ? because it turns out that I need to mask a few vertical pixels from the top part of image as well .. THANKS !
ZaZu
@ZaZu: No problem. ;) I updated my answer with an explanation and code for creating a logical mask.
gnovice
Thanks alot :) One problem though, the logical index used for masking does not work here :( Im not sure why !! The previous one works well but it cuts the the last point where the object falls (the object falls a bit after 1/2 image size) .. Is there a way I can use the first method to block the upper space as well ? **EDIT** Can I use a way to block specific pixels ? I have a 480x640 image ..
ZaZu
@ZaZu: Not sure why the logical index isn't working. What error message are you getting? You can block other parts of the image by setting other ranges of indices to 0 (like I did in the first example). For example, `A(1:5,:) = 0;` sets the first 5 rows to black, while `A(180,440) = 0` sets the pixel in row 180 and column 440 to black.
gnovice
Alright !! I got it ... I had placed `mask(1:5,:)` instead of `image(1:5,:)` .... Now I got the first few rows = false and im glad that it works :) thanks ... now can I mask these first rows AND the last 50 rows for example ?
ZaZu
Oh you already mentioned A(180,440) example, thanks it works now !! :)Really appreciate your help and replying back !
ZaZu