define region of pixels radially with radius r from x y position in image in matlab

Here is an example code for defining a circular region of pixels with radius r from the center point (x, y) in a grayscale image in MATLAB:

main.m
% Read the grayscale image
I = im2gray(imread('image.jpg'));

% Define the center point (x, y) and the radius r
x = 100;
y = 100;
r = 50;

% Create a meshgrid of the same size as the image
[xx,yy] = meshgrid(1:size(I,2), 1:size(I,1));

% Compute the Euclidean distance from each pixel to the center point
dist = sqrt((xx-x).^2 + (yy-y).^2);

% Create a binary mask indicating which pixels are within the circular region
mask = dist <= r;

% Display the original image with the circular region of interest overlaid
imshow(I);
hold on;
viscircles([x,y],r,'Color','r','LineWidth',2);
hold off;

% Extract the pixels within the circular region
pixels_within_region = I(mask);
672 chars
26 lines

In this code, the meshgrid function creates a grid of x and y coordinates corresponding to the image pixels. The sqrt function computes the Euclidean distance between each pixel and the center point using the coordinates from the meshgrid. The resulting dist matrix is then thresholded to create a binary mask indicating which pixels fall within the desired circular region. Finally, the mask can be used to extract the corresponding pixel intensities from the original grayscale image.

gistlibby LogSnag