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

92 views (last 30 days)
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

Accepted Answer

Image Analyst
Image Analyst on 25 May 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.
Image Analyst
Image Analyst on 25 May 2021
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.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!