发布网友 发布时间:2022-04-26 16:04
共1个回答
热心网友 时间:2023-10-13 10:09
clear;
%读图像,rgb转到ntsc空间,对满足要求的亮度的像素进行标记
I=imread('E:\Matlab\Ƭ\q5.jpg');
O=rgb2ntsc(I);
G=O(:,:,2);
[m n]=size(G);
U=zeros(m,n);
for i=1:m
for j=1:n
if G(i,j)>0.03&&G(i,j)<0.16
U(i,j)=1;
end
end
end
%也就是说到这里有一个和原图一样的二值图
%对图像做了些数学形态学处理
sr=strel('disk',6);
C=imclose(U,sr);
L=bwlabel(C);
B=regionprops(L,'area');
%对结果进行了基于区域块大小的筛选
Se=[B.Area];Sm=max(Se);
if Sm>m*n/27
%对区域块的位置进行了定位,这个其实有函数的,写的太难看了
B1=bwareaopen(C,Sm);
k_y1=m;k2=m;l2=n;
for i=1:m
if any(B1(i,:))==1
k_y1=i;
break
end
end
for i=k_y1:m
if B1(i,:)==0
k2=i;
break
end
end
for j=1:n
if any(B1(:,j))==1
l_y1=j;
break
end
end
for j=l_y1:n
if B1(:,j)==0
l2=j;
break
end
end
%计算了长宽
k_y=k2-k_y1;
l=l2-l_y1;
if k_y>.5*l&&k_y<3*l
I1=imcrop(B1,[l_y1 k_y1 l .4*k_y]);
[n1 m1]=size(I1);
L1=bwlabel(I1);
E=regionprops(L1,'area');
Si=[E.Area];
Sm=max(Si);
if Sm/(n1*m1)>.3
B2=bwareaopen(I1,floor(.5*Sm));
g_y1=m1;g2=m1;
for j=1:m1
if any(B2(:,j))==1
g_y1=j;
break
end
end
for j=g_y1:m1
if B2(:,j)==0;
g2=j;break
end
end
g=g2-g_y1;
%之后是把相关区域用线画出来
figure;imshow(I);
hold on
h1=line([l_y1+g_y1,l_y1+g_y1+g],[k_y1,k_y1]);
h2=line([l_y1+g_y1+g,l_y1+g_y1+g],[k_y1,k_y1+1.1*g]);
h3=line([l_y1+g_y1+g,l_y1+g_y1],[k_y1+1.1*g,k_y1+1.1*g]);
h4=line([l_y1+g_y1,l_y1+g_y1],[k_y1+1.1*g,k_y1]);
h=[h1 h2 h3 h4];
set(h,'Color',[1 0 0],'LineWidth',2);
else
figure;imshow(I);
end
else
figure;imshow(I);
end
else
figure;imshow(I);
end