www.pudn.com > work.rar > work.m, change:2009-03-06,size:2796b


clear all; 
I=imread('car.jpg'); 
[y,x,z]=size(I); 
myI=double(I); 
tic   % 测定算法执行的时,开始计时 
Blue_y=zeros(y,1); 
for i=1:y 
    for j=1:x 
            if((myI(i,j,1)<=75)&&((myI(i,j,2)<=105)&&(myI(i,j,2)>=70))&&((myI(i,j,3)<=175)&&(myI(i,j,3)>=115))) % 蓝色RGB的灰度范围 
               Blue_y(i,1)= Blue_y(i,1)+1;     % 蓝色象素点统计   
           end   
    end        
end 
[temp MaxY]=max(Blue_y);        % Y方向车牌区域确定 
PY1=MaxY; 
while ((Blue_y(PY1,1)>=5)&&(PY1>1))  %向上搜索 
       PY1=PY1-1; 
end     
PY2=MaxY; 
while ((Blue_y(PY2,1)>=5)&&(PY2<y)) 
       PY2=PY2+1; 
end 
IY=I(PY1:PY2,:,:); 
Blue_x=zeros(1,x);             % 进一步确定X方向的车牌区域 
for j=1:x 
    for i=PY1:PY2 
            if((myI(i,j,1)<=75)&&((myI(i,j,2)<=105)&&(myI(i,j,2)>=70))&&((myI(i,j,3)<=175)&&(myI(i,j,3)>=115))) 
               Blue_x(1,j)= Blue_x(1,j)+1;                
           end   
    end        
end 
PX1=1; 
while ((Blue_x(1,PX1)<3)&&(PX1<x)) 
       PX1=PX1+1; 
end     
PX2=x; 
while ((Blue_x(1,PX2)<3)&&(PX2>PX1)) 
       PX2=PX2-1; 
end 
PX1=PX1-2; % 对车牌区域的修正 
PX2=PX2+2; 
Plate=I(PY1:PY2,PX1-2:PX2,:); 
figure,imshow(Plate); 
I1 = rgb2gray(Plate); 
I3 = im2bw(I1,0.5); 
[m,n]=size(I3); 
figure,imshow(I3); 
jp=zeros(m,1); 
if(mod(m,2)~=0) 
    m=m+1; 
end 
up=1;tr=12;down=m; 
for i=m/2:m 
    for j=1:n-1 
        if(I3(i,j)~=I3(i,j+1)) 
            jp(i,1)=jp(i,1)+1; 
        end 
    end 
    if jp(i,1)<=tr 
        down=i;break; 
    end 
end 
l=m/2; 
while l>=1 
      l=l-1; 
    for j=1:n-1 
        if(I3(l,j)~=I3(l,j+1)) 
            jp(l,1)=jp(l,1)+1; 
        end 
     end 
    if jp(l,1)<=tr 
        up=l;break; 
    end 
end 
I2=I3(up:down,:); 
figure,imshow(I2); 
[m,n]=size(I2); 
point_x=zeros(1,n); 
for i=1:n 
    for x=1:m 
            if(I2(x,i)>=1)  
               point_x(1,i)= point_x(1,i)+1;     
           end   
    end        
end 
figure,plot(point_x(1,:)); 
pl=zeros(1,n); 
pr=zeros(1,n); 
pb=zeros(1,n);%波峰或波谷的状态变量 
pn=1; 
min=3;%设定最低值 
for j=1:n 
    if (point_x(1,j)>min) 
            pb(1,j) = 1; 
    end 
end 
for j=1:n-1 
     if point_x(1,j)<min 
         if ((point_x(1,j+1)<min)&&pb(1,j+1)==0) 
             pb(1,j)=2; 
         else if pb(1,j)==0 
                 pl(1,pn)=j+1;pb(1,j)=1;pn=pn+1; 
             end 
         end 
     else if point_x(1,j+1)<min 
             pr(1,pn) = j+1;pb(1,j)=2; 
         end 
     end 
end 
figure; 
subplot(1,9,1); 
imshow(I2(:,pl(1,1):pr(1,2))); 
subplot(1,9,2); 
imshow(I2(:,pl(1,2):pr(1,3))); 
subplot(1,9,3); 
imshow(I2(:,pl(1,3):pr(1,4))); 
subplot(1,9,4); 
imshow(I2(:,pl(1,4):pr(1,5))); 
subplot(1,9,5); 
imshow(I2(:,pl(1,5):pr(1,6))); 
subplot(1,9,6); 
imshow(I2(:,pl(1,6):pr(1,7))); 
subplot(1,9,7); 
imshow(I2(:,pl(1,7):pr(1,8))); 
t=toc; % 读取计时