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

Here's a code snippet in MATLAB that creates a binary circular mask with center at (x, y) and radius r:

main.m
% Define center and radius of circle
x = 100;
y = 100;
r = 50;

% Create a grid of points
[xGrid, yGrid] = meshgrid(1:size(image,2), 1:size(image,1));
% Compute distance from center for each point on the grid
distFromCenter = sqrt((xGrid - x).^2 + (yGrid - y).^2);

% Create binary mask
circularMask = distFromCenter <= r;
323 chars
13 lines

This code creates a grid of points and computes the distance of each point on the grid from the center of the circle. Then, a binary mask is created where each pixel inside the circle is set to 1 and outside the circle is set to 0.

gistlibby LogSnag