回复 2 # 雨人 的帖子
现在用的是,把图形里面用填充,膨胀的方法,和外面的矩形分开
clc
clear
name=input('输入图片名');
AA=imread(name);
Wide=input('输入图片宽度');
Long=input('输入图片长度');
Wide1=input('输入所选原始图片纵坐标长度');
Long1=input('输入所选原始图片橫坐标长度');
%image(AA)
imshow(AA)
%info=imfinfo('1234.jpg')
for i=1:Wide
for j=1:Long
if (AA(i,j,1)>80&AA(i,j,2)>100&AA(i,j,3)>80)%|(AA(i,j,1)==&AA(i,j,2)<150&AA(i,j,3)==0)
BB(i,j,1)=0;
BB(i,j,2)=0;
BB(i,j,3)=0;
else
BB(i,j,1)=1;
BB(i,j,2)=1;
BB(i,j,3)=1;
end
end
end
figure
image(BB)
Se=strel('line',7,7);
BB2=imdilate(BB,Se);
%figure
%image(BB2)
BB22=im2bw(BB2);
%figure
%image(BB22)
BB3=bwmorph(BB22,'thin');
%figure
%image(BB3,[0 0 0;1 1 1])
BB4=bwmorph(BB3,'bridge');
%BB4 = bwmorph(BB33,'shrink',Inf);
for i=1
for j=1:Long
BB4(i,j)=0;
end
end
for i=Wide
for j=1:Long
BB4(i,j)=0;
end
end
for i=1:Wide
for j=1
BB4(i,j)=0;
end
end
for i=1:Wide
for j=Long
BB4(i,j)=0;
end
end
BB40=bwmorph(BB4,'bridge');
%figure
%image(BB4)
%figure
%image(BB4)
BB5=bwmorph(BB40,'skel',Inf);
%figure
%image(BB5)
BB6=bwmorph(BB5,'spur',Inf);
%figure
%image(BB6)
BB7=bwfill(BB6,'hole');
figure
image(BB7)
BB8=double(BB7);
%area0=regionprops(BB8)
area1=bwarea(BB7);
area=area1/(Wide*Long)*Wide1*Long1%地面示功图面积
s= regionprops(BB8, 'centroid');%求示功图质心
centroids = cat(1, s.Centroid);%示功图质心坐标
X0=centroids(1)/Long*Long1%将质心横坐标换算到原始图坐标中,为质心到图左端距离
Y0=centroids(2)/Wide*Wide1%将质心纵坐标换算到原始图坐标中,为质心到图顶的距离
%imshow(BB8)
% hold on
% plot(centroids(:,1), centroids(:,2), 'b*')
% hold off
%求功图七个不变矩
m=regionprops(BB8, 'area');
m00=m.Area;
m10=0;
m01=0;
for i=1:Wide %
for j=1:Long
if BB8(i,j)==1
m10=m10+i;
m01=m01+j;
end
end
end
x1=m10/m00;
y1=m01/m00;
u00=m00;
u11=0;
u12=0;
u21=0;
u20=0;
u02=0;
u30=0;
u03=0;
for i=1:Wide%
for j=1:Long
if BB8(i,j)==1
u11=u11+(i-x1)*(j-y1);
u12=u12+(i-x1)*(j-y1)^2;
u21=u21+(i-x1)^2*(j-y1);
u20=u20+(i-x1)^2;
u02=u02+(j-y1)^2;
u30=u30+(i-x1)^3;
u03=u03+(j-y1)^3;
end
end
end
ata11=u11/(u00^((1+1)/2+1));
ata20=u20/(u00^((2+0)/2+1));
ata02=u02/(u00^((0+2)/2+1));
ata12=u12/(u00^((1+2)/2+1));
ata21=u21/(u00^((2+1)/2+1));
ata30=u30/(u00^((3+0)/2+1));
ata03=u03/(u00^((0+3)/2+1));
fai1=ata20+ata02
fai2=(ata20-ata02)^2+4*ata11^2
fai3=(ata30-ata12)^2+(3*ata21-ata03)^2
fai4=(ata30+ata12)^2+(ata21+ata03)^2
fai5=(ata30-ata12)*(ata30+ata12)*((ata30+ata12)^2-3*(ata21-ata03)^2)+(3*ata21-ata03)*...
(ata21+ata03)*(3*(ata30+ata12)^2-(ata21+ata03)^2)
fai6=(ata20-ata02)*((ata30+ata12)^2-(ata21+ata03)^2)+4*ata11*(ata30+ata12)*(ata21+ata03)
fai7=3*(ata12-ata03)*(ata30+ata12)*((ata30+ata12)^2-3*(ata21+ata03)^2)+(3*ata21-ata03)*...
(ata21+ata03)*(3*(ata30+ata12)^2-(ata21+ata03)^2)
total=[area X0 Y0 fai1 fai2 fai3 fai4 fai5 fai6 fai7]%将所求量总放到一个数组中以便集中存储调用
这个程序顺带计算了一下质心位置
顺带问一下大家,别的电脑运行没问题,昨天我运行也没问题,今天我的电脑总提示这样一个错误:
??? Undefined function or method 'checkinput' for input arguments of type 'cell'.
Error in ==> regionprops>ParseInputs at 839
if ~isempty(stats(k).PixelIdxList)
Error in ==> regionprops at 109
% nonsparse, and contain integers. L can have any numeric class and any
不知道为什么? |