Attempted to access yC(3); index out of bounds because numel(yC)=2. Error in Contour2Area (line 24) s=(xC(2)-xC(1))(yC(3)-yC(2))-(xC(3)-xC(2))(yC(2)-yC(1)); Error in test (line 34) [Area,Centroid]=Contour2Area(cf);
2 views (last 30 days)
Show older comments
function [Area,Centroid]=Contour2Area(C)
nC=length(C);
cc=1;j=1;
while cc<nC
ix(j)=C(2,cc);
cvec_start(j)=cc+1;
cc=cc+ix(j)+1;
j=j+1;
end
%--- find areas Ac and centroid Cxy (special if contour goes outside) ---
for j=1:length(ix)
xC=C(1,cvec_start(j):cvec_start(j)+ix(j)-1);
yC=C(2,cvec_start(j):cvec_start(j)+ix(j)-1);
if ~isempty(find(isnan(xC)))
if length(xC)>1
xC(find(isnan(xC)))=(xC(find(isnan(xC))-1)+xC(find(isnan(xC))+1))/2;
yC(find(isnan(yC)))=(yC(find(isnan(yC))-1)+yC(find(isnan(yC))+1))/2;
end
end
if length(xC)>1
Ac(j)=polyarea(xC,yC); % area
%--- determine clockwise/reverse sign, s
s=(xC(2)-xC(1))*(yC(3)-yC(2))-(xC(3)-xC(2))*(yC(2)-yC(1));
s=s/abs(s);
Cxy(:,j)=s*sum([(xC(1:end-1)+xC(2:end)).*(xC(1:end-1).*yC(2:end)-xC(2:end).*yC(1:end-1));...
(yC(1:end-1)+yC(2:end)).*(xC(1:end-1).*yC(2:end)-xC(2:end).*yC(1:end-1))]')'/...
6/Ac(j); % centroid (centre of mass)
nan0(j)=0;
else
Ac(j)=NaN;
Cxy(:,j)=[NaN;NaN];
nan0(j)=1;
end
%hold on;plot(xC,yC,'r.',Cxy(1,j),Cxy(2,j),'b*'); % plot polygons and centroids
end
%--- Remove NaN contours ---
nanix=find(nan0~=1);
Area=Ac(nanix);
Centroid=Cxy(:,nanix);
%--- Determine relationship between polygons (inside eachother?, parent/child) ---
IN=zeros(length(nanix),length(nanix));
for i=1:length(nanix)
i0=nanix(i);
xC=C(1,cvec_start(i0):cvec_start(i0)+ix(i0)-1);
yC=C(2,cvec_start(i0):cvec_start(i0)+ix(i0)-1);
for j=i+1:length(nanix)
j0=nanix(j);
IN(i,j)=inpolygon(C(1,cvec_start(j0)),C(2,cvec_start(j0)),xC,yC);
end
end
if true
% code
end
the program is:
imout=imread(['frame' num2str(25) '.jpg']);
n = 20;
% Divide into nxn images
s1 = [n*ones(1,floor(size(imout,1)/n)) mod(size(imout,1),n)];
s2 = [n*ones(1,floor(size(imout,2)/n)) mod(size(imout,2),n)];
C = mat2cell(imout, s1, s2);
c=1;
for i=1:size(C,1)
for j=1:size(C,2)
x=find(C{i,j});
if size(x)~=0
v(c,1)=i;
v(c,2)=j;
v(c,3)=sub2ind(size(C),v(c,1),v(c,2));
c=c+1;
end
end
end
for i=1:size(v,1)
cntr=C{v(i,1),v(i,2)};
cf=contourf(cntr,1);
[Area,Centroid]=Contour2Area(cf);
%g(i:size(Area),1)=sub2ind(size(C),v(i,1),v(i,2));
%g(i:size(Area),2)=Area;
end
0 Comments
Answers (1)
Image Analyst
on 17 Nov 2015
There is no third element of yC. Why do you think there should be? Anyway, this link will definitely help you figure out why yC is not the size you think it should be: http://blogs.mathworks.com/videos/2012/07/03/debugging-in-matlab/
4 Comments
Image Analyst
on 18 Nov 2015
How are you defining the blob within the block? Are you thresholding the block? Like
binaryImage = thisBlock > someThresholdValue;
thisArea = sum(binaryImage(:));
What does xC represent? What does size(xC) report?
Why are you using contours? Wouldn't image analysis be easier?
See Also
Categories
Find more on Crystals in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!