How to mask part of an image in matlab?

I would like to know how to hide the part of the image that is in BLACK and WHITE?

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

Thank!

EDIT

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

Please explain the code because I really want to find out how it works and implement it in my own understanding.

EDIT2

My image is 480x640 ... Is there a way to mask certain pixels? e.g. 180x440 from image ...

+3
source share
2 answers

If you have a two-dimensional image of the intensity of the shades of gray stored in the matrix A, you can set the bottom 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 lines in A, using the SIZE function , dividing it by 2 and rounding to get the integer index near the center of the image height. Then the vector centerIndex:endindexes all the rows from the central index to the end, and :indexes all the columns. All of these indexed items are set to 0 to represent black.

CLASS A, 0 , function CAST. , 0, , A .

, :

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

mask true (.. "1" ) , , false (.. "0" ) , 0. mask false . , , :

A(~mask) = 0;  %# Set all elements in A corresponding
               %#   to false values in mask to 0
+5
function masked = maskout(src,mask)
    % mask: binary, same size as src, but does not have to be same data type (int vs logical)
    % src: rgb or gray image
    masked = bsxfun(@times, src, cast(mask,class(src)));
end
0

Source: https://habr.com/ru/post/1745597/


All Articles