create circular mask and assign zero outside the mask in an image

134 Ansichten (letzte 30 Tage)
Hi,
I have the series of images (e.g. input image.jpg), in which I have to create the circular mask (mask.jpg) and make all the values outide the ciecular mask to be zero (after mask.jpg).
Please let me know how to do this..
Note - I also have co ordinate information for this image

Akzeptierte Antwort

Image Analyst
Image Analyst am 25 Mai 2021
If you have the (x,y) coordinates of the circle, simply do this:
[rows, columns, numColorChannels] = size(grayImage);
mask = poly2mask(x, y, rows, columns);
grayImage(~mask) = 0; % Blacken outside the mask.
A variety of demos are attached.
  4 Kommentare
Pauline Audurier
Pauline Audurier am 2 Mär. 2023
Hi,
Thank you for hte code, it's working well.
I wonder if there is a way to chose the outside color of the mask.
For exemple, in my case, I'ld like the outside color in grey and not in black (my images are colored).
Thank you,
Pauline
DGM
DGM am 3 Mär. 2023
Bearbeitet: DGM am 3 Mär. 2023
If the mask is strictly logical (no transparency) and the output image is expected to be RGB, you can do this with imoverlay().
% mask must be logical, FG tuple must be unit-scale RGB
maskedImage = imoverlay(originalImage,~mask,[1 1 1]*0.25);
The tuple [1 1 1]*0.25 is dark gray in this case, but you can set that to any color you want.
Otherwise, you'll have to use something else if you want more generalization. See this set of examples:

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by