how to save all value of centoid of many object from one image ?
조회 수: 3 (최근 30일)
이전 댓글 표시
i have a code,
a=imread('cen.png'); contain 2 object
bw=im2bw(a);
labeledimage=bwlabel(bw,4);
object=regionprops(labeledimage);
count=size(objek,1);
centroid = zeros(2,2);
for i=1:count
centroid(i) = object(i).Centroid;
end
i try to get the centroid of each object, and save the all value of centoid object. But i get the error :
In an assignment A(I) = B, the number of elements in B and I must be the same.
Error in cek (line 8) centroid(i) = objek(i).Centroid;
i try to fix this error,but i don't find the way to fix this error. please help me.. thanks
댓글 수: 0
답변 (1개)
Sven
2016년 4월 19일
The centroid is not a scalar value. It has two components (X coordinate, Y coordinate), so you cannot assign it to a scalar variable. Your code will work if you change this line:
centroid(i) = object(i).Centroid;
To this line:
centroid(i,:) = object(i).Centroid;
However you can also clean up the code chunk quite a bit and simply do:
a = imread('cen.png'); contains N objects
bw = im2bw(a);
stats = struct2table(regionprops(bw));
centroid = stats.Centroid; % Will be an N-by-2 array where N is the # of objects
Did this answer the question for you?
Thanks, Sven.
댓글 수: 3
Sven
2016년 4월 20일
It's just that tables are a little easier to use (in my opinion) than the regular output of regionprops() which is a struct. You could also do the following for the same result:
stats = regionprops(bw);
centroid = cat(1,stats.Centroid);
Image Analyst
2016년 4월 20일
Or
allCentroids = [stats.Centroid];
xCentroids = allCentroid(1:2:end);
yCentroids = allCentroid(2:2:end);
참고 항목
카테고리
Help Center 및 File Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!