I1=imread('1111.bmp');
I=imresize(I1,[800,800]);
J=I; %待剪切图像
[m,n]=size(I);
%分块
x=0;y=0;
for j=40:-1:1
if n/j-fix(n/j)==0
x=n/j;
break;
end
end
for i=40:-1:1
if m/i-fix(m/i)==0
y=m/i;
break;
end
end
K=zeros(y,x);
for i=1:1:y
for j=1:1:x
histgram1=zeros(1,256);
for i1=1:1:m/y
for j1=1:1:n/x
k=I((m/y)*(i-1)+i1,(n/x)*(j-1)+j1);
histgram1(k+1)=histgram1(k+1)+1;
end
end
%求对比度
T1=0;
while(1)
sum_low=0;sum_high=0;
for i3=0:T1
sum_low=sum_low+histgram1(i3+1);
end
for i4=T1+1:255
sum_high=sum_high+histgram1(i4+1);
end
if sum_low>=sum_high
break;
else
T1=T1+1;
if T1==255
break;
end
end
end
temp1=0;temp2=0;temp3=0;temp4=0;
for i2=0:T1-1
temp1=temp1+i2*histgram1(i2+1);
temp2=temp2+histgram1(i2+1);
end
dark=temp1/temp2;
for i2=T1:255
temp3=temp3+i2*histgram1(i2+1);
temp4=temp4+histgram1(i2+1);
end
bright=temp3/temp4;
T=bright-dark;
if T<20
I((m/y)*(i-1)+1:m/y*i,(n/x)*(j-1)+1:n/x*j)=0;
else
%方向性检测
temp_0=[5 5 5 -3 0 -3 -3 -3 -3];
temp_45=[-3 5 5 -3 0 5 -3 -3 -3];
temp_90=[-3 -3 5 -3 0 5 -3 -3 5];
temp_135=[-3 -3 -3 -3 0 5 -3 5 5];
n_0=0;n_45=0;n_90=0;n_135=0;
for i1=2:m/y-1
for j1=2:n/x-1
I_temp_0=abs(double(I((m/y)*(i-1)+i1-1,(n/x)*(j-1)+j1-1))*temp_0(1)+double(I((m/y)*(i-1)+i1-1,(n/x)*(j-1)+j1))*temp_0(2)+...
double(I((m/y)*(i-1)+i1-1,(n/x)*(j-1)+j1+1))*temp_0(3)+double(I((m/y)*(i-1)+i1,(n/x)*(j-1)+j1-1))*temp_0(4)+...
double(I((m/y)*(i-1)+i1,(n/x)*(j-1)+j1))*temp_0(5)+double(I((m/y)*(i-1)+i1,(n/x)*(j-1)+j1+1))*temp_0(6)+...
double(I((m/y)*(i-1)+i1+1,(n/x)*(j-1)+j1-1))*temp_0(7)+double(I((m/y)*(i-1)+i1+1,(n/x)*(j-1)+j1))*temp_0(8)+...
double(I((m/y)*(i-1)+i1+1,(n/x)*(j-1)+j1+1))*temp_0(9))/8;
I_temp_45=abs(double(I((m/y)*(i-1)+i1-1,(n/x)*(j-1)+j1-1))*temp_45(1)+double(I((m/y)*(i-1)+i1-1,(n/x)*(j-1)+j1))*temp_45(2)+...
double(I((m/y)*(i-1)+i1-1,(n/x)*(j-1)+j1+1))*temp_45(3)+double(I((m/y)*(i-1)+i1,(n/x)*(j-1)+j1-1))*temp_45(4)+...
double(I((m/y)*(i-1)+i1,(n/x)*(j-1)+j1))*temp_45(5)+double(I((m/y)*(i-1)+i1,(n/x)*(j-1)+j1+1))*temp_45(6)+...
double(I((m/y)*(i-1)+i1+1,(n/x)*(j-1)+j1-1))*temp_45(7)+double(I((m/y)*(i-1)+i1+1,(n/x)*(j-1)+j1))*temp_45(8)+...
double(I((m/y)*(i-1)+i1+1,(n/x)*(j-1)+j1+1))*temp_45(9))/8;
I_temp_90=abs(double(I((m/y)*(i-1)+i1-1,(n/x)*(j-1)+j1-1))*temp_90(1)+double(I((m/y)*(i-1)+i1-1,(n/x)*(j-1)+j1))*temp_90(2)+...
double(I((m/y)*(i-1)+i1-1,(n/x)*(j-1)+j1+1))*temp_90(3)+double(I((m/y)*(i-1)+i1,(n/x)*(j-1)+j1-1))*temp_90(4)+...
double(I((m/y)*(i-1)+i1,(n/x)*(j-1)+j1))*temp_90(5)+double(I((m/y)*(i-1)+i1,(n/x)*(j-1)+j1+1))*temp_90(6)+...
double(I((m/y)*(i-1)+i1+1,(n/x)*(j-1)+j1-1))*temp_90(7)+double(I((m/y)*(i-1)+i1+1,(n/x)*(j-1)+j1))*temp_90(8)+...
double(I((m/y)*(i-1)+i1+1,(n/x)*(j-1)+j1+1))*temp_90(9))/8;
I_temp_135=abs(double(I((m/y)*(i-1)+i1-1,(n/x)*(j-1)+j1-1))*temp_135(1)+double(I((m/y)*(i-1)+i1-1,(n/x)*(j-1)+j1))*temp_135(2)+...
double(I((m/y)*(i-1)+i1-1,(n/x)*(j-1)+j1+1))*temp_135(3)+double(I((m/y)*(i-1)+i1,(n/x)*(j-1)+j1-1))*temp_135(4)+...
double(I((m/y)*(i-1)+i1,(n/x)*(j-1)+j1))*temp_135(5)+double(I((m/y)*(i-1)+i1,(n/x)*(j-1)+j1+1))*temp_135(6)+...
double(I((m/y)*(i-1)+i1+1,(n/x)*(j-1)+j1-1))*temp_135(7)+double(I((m/y)*(i-1)+i1+1,(n/x)*(j-1)+j1))*temp_135(8)+...
double(I((m/y)*(i-1)+i1+1,(n/x)*(j-1)+j1+1))*temp_135(9))/8;
average_I=(double(I((m/y)*(i-1)+i1-1,(n/x)*(j-1)+j1-1))+double(I((m/y)*(i-1)+i1-1,(n/x)*(j-1)+j1))+...
double(I((m/y)*(i-1)+i1-1,(n/x)*(j-1)+j1+1))+double(I((m/y)*(i-1)+i1,(n/x)*(j-1)+j1-1))+...
double(I((m/y)*(i-1)+i1,(n/x)*(j-1)+j1))+double(I((m/y)*(i-1)+i1,(n/x)*(j-1)+j1+1))+...
double(I((m/y)*(i-1)+i1+1,(n/x)*(j-1)+j1-1))+double(I((m/y)*(i-1)+i1+1,(n/x)*(j-1)+j1))+...
double(I((m/y)*(i-1)+i1+1,(n/x)*(j-1)+j1+1)))/9;
if I_temp_0>average_I
n_0=n_0+1;
end
if I_temp_45>average_I
n_45=n_45+1;
end
if I_temp_90>average_I
n_90=n_90+1;
end
if I_temp_135>average_I
n_135=n_135+1;
end
end
end
max_n=n_0;
if n_45>max_n
max_n=n_45;
end
if n_90>max_n
max_n=n_90;
end
if n_135>max_n
max_n=n_135;
end
if max_n<40
I((m/y)*(i-1)+1:m/y*i,(n/x)*(j-1)+1:n/x*j)=0;
else
K(i,j)=255;
I((m/y)*(i-1)+1:m/y*i,(n/x)*(j-1)+1:n/x*j)=I((m/y)*(i-1)+1:m/y*i,(n/x)*(j-1)+1:n/x*j)+1;
end
end
end
end
figure(1)
imshow(I)
[L,num]=bwlabel(K,4);
count=zeros(1,num);
for i=1:y
for j=1:x
for k1=1:num
if L(i,j)==k1
count(k1)=count(k1)+1;
end
end
end
end
max_num=find(count==max(count));
for i=1:y
for j=1:x
if L(i,j)~=max_num
I((m/y)*(i-1)+1:m/y*i,(n/x)*(j-1)+1:n/x*j)=0;
end
end
end
y_low=0;
for i=1:y
for j=1:x-1
if L(i,j)==max_num&&L(i,j+1)==max_num
y_low=m/y*(i-1)+1;
break;
end
end
if y_low~=0
break;
end
end
y_high=0;
for i=y:-1:1
for j=x-1:-1:1
if L(i,j)==max_num&&L(i,j+1)==max_num
y_high=m/y*(i-1)+m/y;
break;
end
end
if y_high~=0
break;
end
end
x_low=0;
for j=1:x
for i=1:y-1
if L(i,j)==max_num&&L(i+1,j)==max_num
x_low=n/x*(j-1)+1;
break;
end
end
if x_low~=0
break;
end
end
x_high=0;
for j=x:-1:1
for i=y-1:-1:1
if L(i,j)==max_num&&L(i+1,j)==max_num
x_high=n/x*(j-1)+n/x;
break;
end
end
if x_high~=0
break;
end
end
if y_low-50>0
y_low=y_low-50;
else
y_low=1;
end
if y_high+50<=m
y_high=y_high+50;
else
y_high=m;
end
if x_low-20>0
x_low=x_low-20;
else
x_low=1;
end
if x_high+50<=n
x_high=x_high+50;
else
x_high=n;
end
for i=1:m
for j=1:n
if i<=y_low||i>y_high||j<=x_low||j>x_high
J(i,j)=0;
end
end
end
%Y=zeros(y_high+20-(y_low-20),(x_high+20)-(y_low-20));
Y=J(y_low+1:y_high-1,x_low+1:x_high); %剪切图像
Y_temp=Y;%待校正提取图像
figure(2)
imshow(Y_temp)
[m1 n1]=size(Y);
Y_edge=edge(Y,'log');
figure(3)
imshow(Y_edge)
se=strel('disk',3);
P=imdilate(Y_edge,se);
figure(4)
imshow(P)
clear L num count;
[L num]=bwlabel(P);
count=zeros(1,
评论0