create greyscale circle shaped mask with center at x,y and radius of r in matlab

Here's the code to create a greyscale circle shaped mask with a center at (x,y) and a radius of r in Matlab:

main.m
%Set the center and radius of the circle
x = 50; % x coordinate
y = 50; % y coordinate
r = 30; % radius

%Create a grid of coordinates
[xx,yy] = ndgrid((1:100)-x, (1:100)-y);

%Create the mask
mask = uint8((xx.^2 + yy.^2) <= r^2);
mask = repmat(mask, [1, 1, 3]);

%Display the mask
imshow(mask)
295 chars
15 lines

In the code above, we create a grid of coordinates using the ndgrid function for the x and y coordinates. We then create a Boolean mask of pixels where their distance to the center of the circle is less than or equal to the radius. The uint8 function is used to convert the values in the mask to 8-bit unsigned integer values for displaying the greyscale values of the image. Finally, the repmat function repeats the mask along the third dimension to make it a 3-channel image, with each channel containing the same mask. The output is then displayed using the imshow function.

gistlibby LogSnag