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

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

Thanks for the suggestion, I realized that your attached code of circle_masking_demo_drawcircle.m serves my purpose. Actually my image details are stored in the array named fu7, hence I assigned the same to original image, however, while executing the code I got the below error.
I am attaching my fu7 matrix for your reference.
Unrecognized property 'Radius' for class 'matlab.ui.Figure'.
% Get image.
originalImage = fu7;
[rows, columns, numberOfColorChannels] = size(originalImage);
subplot(2, 2, 1);
imagesc(originalImage);
axis('on', 'image');
title('Original Image', 'FontSize', fontSize);
% Maximize the window to make it easier to draw.
g = gcf;
g.WindowState = 'maximized'
% Ask user to draw a circle:
uiwait(helpdlg('Please click and drag out a circle.'));
h.Radius = 0;
while h.Radius == 0
h = drawcircle('Color','k','FaceAlpha',0.4)
if h.Radius == 0
uiwait(helpdlg('You double-clicked. You need to single click, then drag, then single click again.'));
end
end
% Get coordinates of the circle.
angles = linspace(0, 2*pi, 10000);
x = cos(angles) * h.Radius + h.Center(1);
y = sin(angles) * h.Radius + h.Center(2);
% Show circle over image.
subplot(2, 2, 2);
imagesc(originalImage);
axis('on', 'image');
hold on;
plot(x, y, 'r-', 'LineWidth', 2);
title('Original image with circle mask overlaid', 'FontSize', fontSize);
% Get a mask of the circle
mask = poly2mask(x, y, rows, columns);
subplot(2, 2, 3);
imshow(mask);
axis('on', 'image');
title('Circle Mask', 'FontSize', fontSize);
% Mask the image with the circle.
if numberOfColorChannels == 1
maskedImage = originalImage; % Initialize with the entire image.
maskedImage(~circleImage) = 0; % Zero image outside the circle mask.
else
% Mask the image.
maskedImage = bsxfun(@times, originalImage, cast(mask, class(originalImage)));
end
% Crop the image to the bounding box.
props = regionprops(mask, 'BoundingBox');
maskedImage = imcrop(maskedImage, props.BoundingBox);
% Display it in the lower right plot.
subplot(2, 2, 4);
imshow(maskedImage, []);
% Change imshow to image() if you don't have the Image Processing Toolbox.
title('Image masked with the circle', 'FontSize', fontSize);
fprintf('Done running %s.m ...\n', mfilename);
Looks like you defined h prior somewhere. Call h something different in the above code, like hROI or something. You'll also need to rename circleimage to mask since the mask is called mask, not circleimage. Or else rename all the masks to circleimage.
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
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개)

카테고리

도움말 센터File Exchange에서 Images에 대해 자세히 알아보기

질문:

2021년 5월 25일

편집:

DGM
2023년 3월 3일

Community Treasure Hunt

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

Start Hunting!

Translated by