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

To create a circle shaped mask with center at (x,y) and radius of r in Matlab, you can use the meshgrid function to generate pixel coordinates, then calculate the Euclidean distance from each pixel to the center (x,y), and finally set all pixels with distance less than r to 1 and the rest to 0 to create a circular mask.

Here's sample code:

main.m
% define image size and center coordinates
img_size = [512, 512]; % adjust as needed
x = img_size(2) / 2;
y = img_size(1) / 2;
r = 200; % adjust as needed

% generate pixel coordinates
[X, Y] = meshgrid(1:img_size(2), 1:img_size(1));

% calculate Euclidean distance from center
D = sqrt((X - x).^2 + (Y - y).^2);

% create circular mask
mask = zeros(img_size); % initialize mask with zeros
mask(D <= r) = 1; % set all pixels with distance less than r to 1

% display mask
imshow(mask);
486 chars
19 lines

This will create a circular mask with a radius of 200 pixels centered at (256, 256) in a 512x512 image. You can adjust the parameters as needed to create masks of different sizes and shapes.

gistlibby LogSnag