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

조회 수: 148 (최근 30일)
Turbulence Analysis
Turbulence Analysis 2021년 5월 25일
편집: DGM 2023년 3월 3일
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