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

145 次查看(过去 30 天)
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

采纳的回答

Image Analyst
Image Analyst 2021-5-25
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 个评论
Pauline Audurier
Pauline Audurier 2023-3-2
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 2023-3-3
编辑:DGM 2023-3-3
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:

请先登录,再进行评论。

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by