- 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.
- Create a vertical matrix: As you find pixels that fall within the circular region, you an append their greyscale values to a vertical matrix.
Reading and saving values of a matrix by circular shapes
2 次查看(过去 30 天)
显示 更早的评论
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!
0 个评论
回答(1 个)
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:
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);
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
0 个评论
另请参阅
类别
在 Help Center 和 File 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!