clear
close all
I = imread('E:\wu\pppp.jpg');
J= rgb2gray(I);
figure(1)
imshow(J);
title('灰度化图像 ');
[e1,e2]=size(J);
Im=imcrop(J,[e2/2-200,e1/2-200,400,400])
figure(2)
subplot(1,2,1),imshow(Im)
title('中心区域 ');
subplot(1,2,2),imhist(Im)
title('中心区域直方图');
[xa,ya]=size(Im);
b=double(Im);
zd=double(max(Im))
zx=double(min(Im))
T=double((zd+zx))/2;
count=double(0);
while 1
count=count+1;
S0=0.0; n0=0.0;
S1=0.0; n1=0.0;
for i=1:xa
for j=1:ya
if double(Im(i,j))>=T
S1=S1+double(Im(i,j));
n1=n1+1;
else
S0=S0+double(Im(i,j));
n0=n0+1;
end
end
end
T0=S0/n0;
T1=S1/n1;
if abs(T-((T0+T1)/2))<0.1
break;
else
T=(T0+T1)/2;
end
end
count
T
K=find(J<T);
J(K)=0;
K=find(J>=T);
J(K)=255;
figure(3)
imshow(J)
title(' 图像二值化 ');
B=medfilt2(J,[5,1]);
figure(4)
imshow(B)
title('中值滤波后图像');
[y0,x0]=size(B);
BW = edge(B,'log');
figure(5);imshow(BW);title('边缘检测图像')
[y,x]=size(BW);
rho_max=floor(sqrt(x^2+y^2))+1;
accarray=zeros(rho_max,180);
Theta=[0:pi/180:pi];
for n=1:y,
for m=1:x
if BW(n,m)==1
for k=1:180
rho=(m*cos(Theta(k)))+(n*sin(Theta(k)));
rho_int=round(rho/2+rho_max/2);
accarray(rho_int,k)=accarray(rho_int,k)+1;
end
end
end
end
figure(6);colormap gray;
imagesc(accarray);title('hough变换后的图')
xlabel('\theta'), ylabel('\rho');
[ysize,xsize]=size(accarray);
fmax=0;
for j=1:1:ysize
for i=1:1:xsize
nf=accarray(j,i);
if (nf>fmax)
fmax=nf;
ymax=j;
xmax=i;
end
end
end
Thetaa=180*xmax/xsize;
Thetab=90-Thetaa;
bw1=imrotate(B,Thetab,'bicubic');
figure(7);
imshow(bw1);title('纠正后的二值图')
BW1=imrotate(BW,Thetab,'bicubic');
figure(8);
imshow(BW1);title('纠正后的边缘图')
[p,q]=size(BW1);
m=0;
for y=ceil(p/2):p
for x=1:q
if BW1(y,x)==1
m=m+1
else m=m
end
end
if m<60
y1=y
break
else m=0
end
end
n=0;
for yl=floor(p/2):-1:1
for xl=1:q
if BW1(yl,xl)==1
n=n+1
else n=n
end
end
if n<60
y2=yl
break
else n=0
end
end
BW2=imcrop(BW1,[1,y2,q,y1-y2]);
figure(9);imshow(BW2);title('上下分割')
bw2=imcrop(bw1,[1,y2,q,y1-y2]);
figure(10);imshow(bw2);title('上下分割')
[a,b]=size(BW2);
k=0
for yi=1:a
for xi=1:ceil(b/3)
if BW2(yi,xi)==1
k=k+1
A(k)=xi
else k=k
end
end
K=k
for c=1:K-4
L1=A(c+4)-A(c+3);
L2=A(c+3)-A(c+2);
L3=A(c+2)-A(c+1);
L4=A(c+1)-A(c);
L=(L1+L2+L3)/3
if (L2/L1)>0.5&(L2/L1)<1.5&(L3/L2)>0.5&(L3/L2)<1.5&(L4/L)>9
C=c
xx1=A(C)+1
break
else k=0
continue
end
end
end
k1=0
for y1i=1:a
for x1i=b:-1:floor(2*b/3)
if BW2(y1i,x1i)==1
k1=k1+1
A(k1)=x1i
else k1=k1
end
end
K1=k1
for c1=1:K1-4
L1=A(c1+4)-A(c1+3);
L2=A(c1+3)-A(c1+2);
L3=A(c1+2)-A(c1+1);
L4=A(c1+1)-A(c1);
L=(L1+L2+L3)/3
if (L1/L2)>0.5&(L1/L2)<1.5&(L2/L3)>0.5&(L2/L3)<1.5&(L4/L)>3
C1=c1
xx2=A(C1)-1
break
else k1=0
continue
end
end
end
BW3=imcrop(BW2,[xx1,1,xx2-xx1,a]);
figure(11);imshow(BW3);title('左右分割')
bar_10=imcrop(bw2,[xx1,1,xx2-xx1,a]);
figure(12);imshow(bar_10);title('左右分割')
check_left = [13,25,19,61,35,49,47,59,55,11;...
39,51,27,33,29,57, 5,17, 9,23];
check_right = [114,102,108,66,92,78,80,68,72,116];
first_num = [31,20,18,17,12,6,3,10,9,5];
[m,n]=size(bar_10)
l = 0;
for i=1:m
k = 1;
l = l+1;
for j=1:n-1
if bar_10(i,j)~=bar_10(i,j+1)
%bar_x(l,k) = i;
bar_y(l,k) = j;
k = k+1;
end
if k>61
l = l-1;
break
end
end
if k<61
l = l-1;
end
end
[m,n] = size(bar_y);
if m<=1
code = '0';
fprintf(1,'GameOver!\n');
return
end
for i=1:m
for j=1:n-1
bar_num(i,j) = bar_y(i,j+1) - bar_y(i,j);
if bar_num(i,j)<0
bar_num(i,j) = 0;
end
end
end
bar_sum = sum(bar_num)/m;
k = 0;
for i=1:59
k = k + bar_sum(i);
end
k = k/95;
for i=1:59
bar_int(i) = round(bar_sum(i)/k);
end
k = 1;
for i=1:59
if rem(i,2)
for j=1:bar_int(i)
bar_01(k) = 1;
k = k+1;
end
else
for j=1:bar_int(i)
bar_01(k) = 0;
k = k+1;
end
end
end
if ((bar_01(1)&&~bar_01(2)&&bar_01(3))...
&&(~bar_01(46)&&bar_01(47)&&~bar_01(48)&&bar_01(49)&&~bar_01(50))...
&&(bar_01(95)&&~bar_01(94)&&bar_01(93)))
l = 1;
for i=1:6
bar_left(l) = 0;
for k=1:7
bar_left(l) = bar_left(l)+bar_01(7*(i-1)+k+3)*(2^(7-k));
end
l = l+1;
end
l = 1;
for i=1:6
bar_right(l) = 0;
for k=1:7
bar_right(l) = bar_right(l)+bar_01(7*(i+6)+k+1)*(2^(7-k));
k = k-1;
end
l = l+1;
end
end
num_bar = '';
num_first = 0;
first = 2;
for i=1:6
for j=0:1
for k=0:9
if bar_left(i)==check_left(j+1,k+1)
num_bar = strcat(num_bar , num2str(k));
switch first
case 2
first = j;
break;
case 1
num_first = num_first + j*(2^(6-i));
break;
case 0
num_first = num_first + ~j*(2^(6-i));
break;
otherwise
break;
end
end
end
end
end
for i=1:6
for j=0:9
if bar_right(i)==check_right(j+1)
num_bar = strcat(num_bar , num2str(j));
end
end
end
for i=0:9
if num_first==first_num(i+1)
num_bar = strcat(num2str(i) , num_bar);
break;
end
end
if numel(num_bar)~=13
fprintf(1,'Please Turn It Around!\n');
return
end
check_code = 0;
for i=1:12
if rem(i,2)
check_code = check_code + str2num(num_bar(i));
else
check_code = check_code + str2num(num_bar(i))*3;
end
end
check_code = rem(check_code,10);
if check_code>0
check_code = 10 - check_code;
end
if check_code==str2num(num_bar(13))
code = num_bar
else
fprintf(1,'Please Turn It Around!\n');
return
end