Image processing code debugging
1 回表示 (過去 30 日間)
古いコメントを表示
Hi, I am doing image processing and plans to extract some information from a binary image, however I have encountered some bugs that I can't solve.
Here is the error:
Error using double
Conversion to double from struct is not possible.
Error in ^ (line 44) (the thing is that my code only has 43 lines I dont kno how this error show up)
X = double(X);
Error in features (line 16)
C = P^2/(4 * pi * A);
Here is the code:
% To compute the features;
% input is the binary thresholded image
% outputs are the feature values
function [P, A, C, xbar, ybar, phione] = features(Iin)
%perimeter
P = regionprops(Iin, 'Perimeter'); %regionprops computes the perimeter
%by calculating the distance between
%each adjoining pair of pixels around
%the border of the binary image.
%area
A = sum(sum(Iin == 255)); %summing the number of pixel that are white
%compactness
C = P^2/(4 * pi * A);
%centroid
[rows cols] = size(I);
m = zeros(rows, cols);
for i = 0:1
for j = 0:1
for x=1:rows
for y=1:cols
m(i+1, j+1) = m(i+1, j+1) + (x^i * y^j * I(x,y));
end
end
end
end
xbar = m(2,1) / m(1,1); %m10/m00
ybar = m(1,2) / m(1,1); %m01/m00
%invariant moment
n=[ 0 0 0 0; 0 0 0 0; 0 0 0 0; 0 0 0 0];
for i = 0:3
for j = 0:3
n(i+1,j+1) = u(i+1,j+1)/(u(1,1)^(1+(i+j)/2));
end
end
phione = n(3,1) + n(1,3); %n(2,0) + n(0,2)
end
2 件のコメント
採用された回答
KSSV
2022 年 11 月 6 日
移動済み: Image Analyst
2022 年 11 月 6 日
This line:
P = regionprops(Iin, 'Perimeter');
Check the class of P
class(P)
It seems it is a structure and you can use it in the line:
C = P^2/(4 * pi * A);
So check P and extract the field you want and substitue it in the line.
その他の回答 (1 件)
Image Analyst
2022 年 11 月 6 日
Scrap all that. Your Compactness is called "Circularity" and you can ask regionprops for the centroids, etc.:
function [P, A, C, xbar, ybar, phione] = features(Iin)
props = regionprops(Iin, 'Circularity', 'Centroid', 'Area', 'Perimeter');
A = [props.Area]; % List of all blob areas.
P = [props.Perimeter]; % List of all blob perimeters.
C = [props.Circularity]; % Circularities = (4*Area*pi)/(Perimeter^2)
xy = vertcat(props.Centroid); % List of all (x,y) centroid coordinates. Each blob's coordinates is in a row.
xbar = xy(:, 1); % x coordinate of the blob centroids.
ybar = xy(:, 2); % y coordinate of the blob centroids.
2 件のコメント
Image Analyst
2022 年 11 月 6 日
編集済み: Image Analyst
2022 年 11 月 6 日
But I didn't use any more "off the shelf" functions than you. In fact, I used fewer. You used regionprops like me but you also used - unnecessarily I might add -- bwperim. I don't know what functions you are allowed to use or not use, but you used regionprops so I assume that's allowed and that's all I used. I assume non-toolbox functions like vertcat and sum are allowed.
There is a lot that can be improved with your codes. For example there might be multiple blobs in general so you can't do what you did. You should do
P = regionprops(Iin, 'Circularity', 'Centroid', 'Area', 'Perimeter');
A = [P.Area]; % List of all blob areas.
allPerims = [P.Perimeter]; % List of all blob perimeters.
C = allPerims.^2 ./ (4 * pi * A); % Doing it yourself instead of useing regionprops.
You need to use use the ".Perimeter" field and use square brackets around it do collect perimeters from (possibly) multiple blobs into one list (vector). That's because your P is a structure, even if it's just one blob you are looking at.
I think you could learn much from it.
If I've helped you consider clicking the "Vote" icon for my answer. I think it's a better approach than what you suggested or accepted.
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!