Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Making A Circle Mask

187 views
Skip to first unread message

jomile

unread,
Apr 16, 2007, 12:58:40 PM4/16/07
to
I am trying to make a circle mask but can seem to find a decent way
to do it.

I tried to use the rectangle function.
rectangle ('position', [-0, -0, 8, 8], 'curvature', [1, 1])
but then i am not sure how to get this data in a matrix. I tried
saveas with gcf but then i end up with a border.

Any suggestions are appreciated

Thanks
Jomile

Screwdriver223

unread,
Apr 16, 2007, 5:29:13 PM4/16/07
to
On Apr 16, 12:58 pm, jomile <u...@domain.com> wrote:
> I am trying to make a circle mask
<SNIP>

I found this nice solution somewhere on the internet a few years
ago... can't remember where it came from. cx and cy are the center
coordinates of the circle, r is its radius, and ix and iy are the size
of the image mask, in pixels.

cx=64;cy=32;ix=128;iy=128;r=30;
[x,y]=meshgrid(-(cx-1):(ix-cx),-(cy-1):(iy-cy));
c_mask=((x.^2+y.^2)<=r^2);
imagesc(c_mask)

Poorya

unread,
Jan 11, 2014, 7:14:08 PM1/11/14
to
"Screwdriver223" <david...@gmail.com> wrote in message <1176758953....@n59g2000hsh.googlegroups.com>...
Great solution. Thanks

G1

unread,
Nov 18, 2014, 5:13:11 AM11/18/14
to
jomile <us...@domain.com> wrote in message <ef543...@webcrossing.raydaftYaTP>...
I know maybe I might be late in the answer, but anyway I hope to help someone!
If I have an image, and I want to realize a circular mask on it, I first have to determine which is the centre and the radius of the circle, by looking at the image.
Then, I type:
cx = coordinate x of the centre;
cy = coordinate y of the centre;
r = radius of the circle;
mask_name = ((x-cx).^2 + (y-cy).^2 <= r^2);
masked_image_name = mask_name.*image_name;
imshow(masked_image_name, [ ] );

Remember that this works for 2D matrices (so the image_name matrix has to be 2D, or part of a 4D matrix, for example image_matrix(: , :, 1, 1); etc.).

Dhorl Jr

unread,
May 25, 2015, 7:17:56 PM5/25/15
to
While I'm myself searching online things that I have done in the past - it is better to post my solution (in case I forgot where I locate the files.

Input :
bb (rectangle or bounding box)
I = image

%% Function 01
function mask = maskCreator2015 (bb,I)

% Return an ellipsoid mask (of binary 1s) of size I

[maxR,maxC] = size (I);
centroid = bbApply( 'getCenter', bb); % [x,y i.e. (cols, rows)]

if bb(4) > bb(3)
ra = bb(4)/2;rb=bb(3)/2;
mask = maskEllipse( maxR, maxC, centroid(2), centroid(1), rb, ra, -0 );
else
rb = bb(4)/2;ra=bb(3)/2;
mask = maskEllipse( maxR, maxC, centroid(2), centroid(1), ra, rb, -0 );
end

%% Function 02

% To create circle boundary of the mask earlier

function MaskCircle = makeRadiusPoint (mask)
MaskCircle = bwperim(mask);

Hope I will help myself in the future too ;)

Enjoice!
Dhorl Jr'

Günter Bachelier

unread,
Mar 11, 2017, 4:35:08 PM3/11/17
to
> cx = coordinate x of the centre;
> cy = coordinate y of the centre;
> r = radius of the circle;
> mask_name = ((x-cx).^2 + (y-cy).^2 <= r^2);
> masked_image_name = mask_name.*image_name;


I got a "Undefined function or variable 'x'." in the line "mask_name = ((x-cx).^2 + (y-cy).^2 <= r^2);"
0 new messages