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

147 vues (au cours des 30 derniers jours)
Turbulence Analysis
Turbulence Analysis le 25 Mai 2021
Modifié(e) : DGM le 3 Mar 2023
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

Réponse acceptée

Image Analyst
Image Analyst le 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 commentaires
Pauline Audurier
Pauline Audurier le 2 Mar 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 le 3 Mar 2023
Modifié(e) : DGM le 3 Mar 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:

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by