필터 지우기
필터 지우기

Reading and saving values of a matrix by circular shapes

조회 수: 1 (최근 30일)
Juan Carlos Alvarez Fernandez
Juan Carlos Alvarez Fernandez 2021년 3월 29일
답변: Zinea 2024년 2월 23일
Given a matrix of a greyscale image, I am trying to read this matrix using circular shapes (known centroid and radii). Then, is it possible to store those values of the greyscale matrix in a vertical matrix?
Thank you in advance for your help!

답변 (1개)

Zinea
Zinea 2024년 2월 23일
It is possible to sample values from a greyscale image matrix using circular shapes with known centroids and radii, and then store those sampled values in a vertical matrix (i.e. a column vector).
You can follow the following steps:
  1. Sample the greyscale image: Iterate over the pixels of the greyscale image and determine which pixels fall within the defined circular region. This involves checking if the distance between the pixel and the centroid of the circle is less than or equal to the radius of the circle.
  2. Create a vertical matrix: As you find pixels that fall within the circular region, you an append their greyscale values to a vertical matrix.
Here is an example MATLAB script that uses a dummy greyscale image matrix, a predefined centroid and radius that create a vertical matrix with sampled greyscale values:
% Dummy greyscale image matrix (10x10) with values from 0 to 255
image_matrix = uint8([
255, 200, 200, 200, 200, 200, 200, 200, 200, 255;
200, 100, 100, 100, 100, 100, 100, 100, 100, 200;
200, 100, 50, 50, 50, 50, 50, 50, 100, 200;
200, 100, 50, 25, 25, 25, 25, 50, 100, 200;
200, 100, 50, 25, 0, 0, 25, 50, 100, 200;
200, 100, 50, 25, 0, 0, 25, 50, 100, 200;
200, 100, 50, 25, 25, 25, 25, 50, 100, 200;
200, 100, 50, 50, 50, 50, 50, 50, 100, 200;
200, 100, 100, 100, 100, 100, 100, 100, 100, 200;
255, 200, 200, 200, 200, 200, 200, 200, 200, 255;
]);
centroid = [5, 5];
radius = 3;
vertical_matrix = sample_circle(image_matrix, centroid, radius);
disp(vertical_matrix);
100 50 50 50 50 50 50 25 25 25 25 100 50 25 0 0 25 50 50 25 0 0 25 50 25 25 25 25 50
function vertical_matrix = sample_circle(image_matrix, centroid, radius)
% Initialize an empty array to store the pixel values
vertical_matrix = [];
% Calculate the size of the image
[height, width] = size(image_matrix);
% Loop over every pixel in the image
for x = 1:width
for y = 1:height
% Check if the current pixel is within the circle
if is_within_circle(x, y, centroid, radius)
% If it is, add the pixel value to the vertical matrix
pixel_value = image_matrix(y, x);
vertical_matrix(end+1, 1) = pixel_value;
end
end
end
end
function result = is_within_circle(x, y, centroid, radius)
% Calculate the distance from the pixel to the centroid
distance = sqrt((x - centroid(1))^2 + (y - centroid(2))^2);
% Check if the distance is less than or equal to the radius
result = distance <= radius;
end

카테고리

Help CenterFile Exchange에서 Read, Write, and Modify Image에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by