Divide circular rings in N angular portions

1 次查看(过去 30 天)
I am developing a program to process images where there are rings. Basically it is a 640x640 matrix (called T) and the rings are defined by their positions [center, outer_radius, inner_radius]. What I am trying to do, is to divide such circular rings in ''numDivisions'' angular portions, where each portion has equal number of elements from the matrix, and then store each portion as a logical matrix of size 640x640 in a structure called mask (mask(i).Roi would be each portion). The problem is that the angular portions do not have the same number of elements, even if I divide them with the same aperture and they are defined on a symmetric circular ring. What could be the reason for this? How can I solve it?
T = ones(640,640);
n_ang=4;
center=[153.9959 174.2265];
outer_radius=129.6656;
inner_radius=87.1144;
mask= Circle_Mask(T, center, outer_radius, inner_radius, n_ang);
num_elements=[];
for i=1:4
roi = mask(i).Roi;
num_elements(i) = sum(roi(:));
clear roi;
end
num_elements
num_elements = 1×4
7247 7205 7243 7288
  2 个评论
Matt J
Matt J 2024-2-15
编辑:Matt J 2024-2-15
As a newcomer, you may not know that this forum let's you attach files to your posts, like sample input images. Additionally, it lets you launch code online directly from within your posts (drawing on your image attachments as input), e.g.,
y=(1:5).^2
y = 1×5
1 4 9 16 25
This means you can demonstrate your code and in what way you think it fails. You are strongly advised to do this.
Valentina
Valentina 2024-2-16
Thank you, in fact this is my first publication. I think I did the editing correctly this time.

请先登录,再进行评论。

采纳的回答

Matt J
Matt J 2024-2-15
编辑:Matt J 2024-2-16
The problem is that the angular portions do not have the same number of elements
That's because the image is discretized into pixels. If you take a photo of a pie that has been sliced into perfectly equal wedges, the photo will nevertheless have slightly unequal numbers of pixels in each wedge. That's because tiny box shaped things don't fit perfectly into wedge-shaped regions.
and then store each portion as a logical matrix of size 640x640 in a structure called mask (mask(i).Roi would be each portion)
I strongly recommend against that. A much more natural and efficient way of storing such a result is as a label matrix. There are many tools in the Image Processing Toolbox for operating on different labeled regions of an image when a label map is provided. Additionally, it makes more sense to have Tsize be one of the input arguments, since that's all the function requires, rather than force the user to generate a complete matrix T of that size.
labelmap=Circle_Mask([640,640], [250,320], 200, 100, 5);
num_elements=[regionprops(labelmap,'Area').Area]
num_elements = 1×5
18795 18847 18847 18847 18896
imshow( labelmap ,[])
function labelmap = Circle_Mask(Tsize, center, outer_radius, inner_radius, numDivisions)
if isempty(center)
center=flip(Tsize/2+0.5); %Put center at center of T by default
end
[X,Y]=meshgrid( (1:Tsize(1)) - center(1), (1:Tsize(2)) - center(2));
[Theta,R]=cart2pol(X,Y);
labelmap=discretize(Theta,linspace(-pi,pi,numDivisions+1)).*(inner_radius<=R & R<=outer_radius);
end

更多回答(0 个)

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by