Identifying round objects and calculate the radius

5 次查看(过去 30 天)
Hi everyone,
I have a tricky issue to solve so I will try my best to explain myself clearly.
I have a height field to analyze which is nammed "H" in my code : it's a 3D tensor since the first 2 dimensions represent the space (2 axis of captured images) and the last dimension corresponds to time ("H(x,y,k)" represents the height field measured on an image of coordinates (x,y) for several time values k).
Basically, what I am trying to do is to measure the radius of a certain round shaped figure that is characterized by a certain height value : this figure that evolves with time is not a perfect circle.
I am trying to build a code that helps me to :
1/ Identify for each k of the height field H : the most round-like shapes through a parameter nammed "metric" in my code.
2/ Measure the radius of this figure selected for each time time k.
This the code that I am trying to implement :
% Some useful values
coef = 3.989123911196657e-07;
H = h_reel/coefficient_2;
index_intersection = 148;
echelle = 4.294478527607363e-05;
% Initialization of the customized restricted height field "H_custom" and the radius array "radius_echelle_quint"
H_custom = zeros(401,401,index_intersection);
H_custom = single(H_custom);
radius_echelle_quint = zeros(1,index_intersection);
% We start a loop that goes through all the images for different times k
for k = 1:index_intersection
H_custom(:,:,k) = H(1000:1400,800:1200,k); % We choose a restricted zone in the field
H_custom = imbinarize(H_custom,0); % We binarize (selection of the positive values only)
H_custom = bwareaopen(H_custom,30); % We construct the external boundaries only
H_custom = imfill(H_custom,'holes'); % We fill the figures
[B_bis,L_bis] = bwboundaries(H_custom(:,:,k),'noholes');
stats_bis = regionprops(L_bis,'Area','Centroid'); % We identify the different surfaces of the different figures obtained
for i = 1:length(B_bis) % For a given time k : we identify the most circle-like figure and we try to calculate its radius
boundary = B_bis{i};
delta_sq = diff(boundary).^2;
perimeter = sum(sqrt(sum(delta_sq,2)));
area_bis = stats_bis(k).Area;
metric = 4*pi*area_bis / perimeter^2; % This is the parameter to identify the round figures
metric_string = sprintf('%2.2f',metric);
if metric > 0.2 % The threshold is very low indeed...
surface_bis = stats_bis.Area;
radius_bis = sqrt(max(surface_bis/pi)); % We welect the radius of the biggest figure identified in the image
end
end
radius_echelle_quint(k) = radius_bis * echelle; % Each radius_bis calculated is stored in the global radius array that includes the calculated values for each time.
end
Unfortunately : this doesn't work well since I got several errors. One of them is this one :
Error using imbinarize
Expected I to be one of these types:
uint8, uint16, uint32, int8, int16, int32, single, double
Instead its type was logical.
Once I try to solve it I end up with another error :
Error using bwboundaries
Expected input number 1, BW, to be two-dimensional.
And of course, I am not able to know if the general idea of my code will be functional since the 2 loops are not very neat...
Could you help me to figure out these issues or maybe propose a more elegant way to achieve my objective ?
Thank you in advance.
P.S : I didn't attach the "H" array because it is a heavy matfile but if it's useful to help me I will share it. Also, I am attachin a typical figure that shows at a given time k : the kind of shape obtained and that I am willing to calculate the radius.
  2 个评论
Image Analyst
Image Analyst 2022-1-21
Attach a screenshot so we can see it here. If I open the fig file, it will launch a second instance of MATLAB, which takes longer and which I don't want open.
What is h_reel?
Unrecognized function or variable 'h_reel'.
Error in test7 (line 4)
H = h_reel/coefficient_2;
Wissem-Eddine KHATLA
编辑:Wissem-Eddine KHATLA 2022-1-21
@Image Analyst Yes of course : here is the screenshot and also the file nammes "H_custom" so no need to use the lines :
coef = 3.989123911196657e-07;
H = h_reel/coefficient_2;
index_intersection = 148;
echelle = 4.294478527607363e-05;
And :
H_custom(:,:,k) = H(1000:1400,800:1200,k); % We choose a restricted zone in the field
I saved it directly for the purpose of my question. Hope you could help : Thank you !

请先登录,再进行评论。

回答(1 个)

vidyesh
vidyesh 2023-12-26
Hello Wissem-Eddine,
I understand you're encountering an error related to the data type of 'H_custom' when using it in a loop.
The 'bwareaopen' function outputs a matrix of type "logical". This causes a subsequent error when 'imbinarize' is called within the for loop, as it does not accept "logical" as an input data type. To address this issue, the sequence of operations needs to be adjusted to call 'imbinarize' and 'bwareaopen' prior to the for loop, as demonstrated below:
H_custom = imbinarize(H_custom, 0);
H_custom = bwareaopen(H_custom, 30);
H_custom = imfill(H_custom, 'holes');
for k = 1:inedxintersection
% ... (rest of your loop code)
end
t is important to note that while this approach resolves the data type error, the accuracy of the results will be dependent on the remainder of the code. Additionally, it should be noted that the 'H_custom.mat' file contains a matrix that is entirely zeros.
Hope this answer helps.

Community Treasure Hunt

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

Start Hunting!

Translated by