filled circle in image at center x,y and radius r in matlab

To draw a filled circle in the center of an image at (x,y) with radius r in MATLAB, you can use the following code:

main.m
% Load the image
img = imread('your_image.jpg');

% Get the center of the image
[x,y] = size(img);
cx = x/2;
cy = y/2;

% Define the radius of the circle
r = 50;

% Create a meshgrid of the same size as the image
[X,Y] = meshgrid(1:y,1:x);

% Calculate the distance of each point from the center of the circle
dist = sqrt((X-cx).^2 + (Y-cy).^2);

% Create a logical mask where the distance is less than or equal to the radius
mask = dist <= r;

% Set the pixels in the mask to white (255)
img(mask) = 255;

% Display the image
imshow(img);
540 chars
26 lines

This code first loads the image, then calculates the center of the image (assuming the image has an even size). It then sets a radius value r, and creates a meshgrid of the same size as the image. Using the meshgrid, it calculates the distance of each pixel to the center of the circle using the distance formula. It then creates a logical mask where the distance is less than or equal to the radius, and sets the pixels in the mask to white. Finally, it displays the resulting image with a filled circle in the center.

gistlibby LogSnag