select pixels in image radially from x, y position in matlab

To select pixels radially from an x,y position in an image using MATLAB, you can follow these steps:

  1. Read in the image using imread function.
  2. Define the center point (x,y) around which you want to select pixels radially.
  3. Calculate the distances of all pixels in the image from the center point.
  4. Define a radius value, r, which will determine how far out from the center point you want to select pixels.
  5. Create a logical mask for the pixels within the desired radius by setting all pixels with distances less than or equal to r to 1, and all others to 0.
  6. Use the logical mask to select the desired pixels in the image.

Here's an example code snippet that demonstrates these steps:

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

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

% Calculate distances of all pixels from center point
[rows,cols] = size(img);
[X,Y] = meshgrid(1:cols,1:rows);
distances = sqrt((X-center_x).^2 + (Y-center_y).^2);

% Create logical mask for pixels within radius r
mask = distances <= r;

% Use mask to select desired pixels
selected_pixels = img(mask);
445 chars
19 lines

Note that the above code will give you a 1D vector of selected pixels. If you want to create a new image that only contains the selected pixels, you can reshape the vector back into the original shape of the image:

main.m
new_img = zeros(rows,cols);
new_img(mask) = selected_pixels;
61 chars
3 lines

gistlibby LogSnag