matlab 车牌分割的算法

function Touying(g)
f=rgb2gray(g);
% [m n]=size(f);
[row col]=size(f);
% T=graythresh(f)
% T=T*255
% for i=1:row
%     for j=1:col
%        if(f(i,j)>T)
%            f(i,j)=255;
%        else f(i,j)=0;
%        end
%     end
% end
f=im2bw(f,0.85);
figure,imshow(f);
a=zeros(row,1);
b=zeros(col,1); 
% h=fspecial('average',3); %均值滤波器
% f=im2bw(round(filter2(h,f)));
% imshow(f)
for i=1:row
    for j=1:col
        a(i)=a(i)+f(i,j);%横向投影
    end
end
% for j=1:col
%     for i=1:row
%         b(j)=b(j)+f(i,j);%纵向投影
%     end
% end
% figure,plot(1:col,b)
%%以上是白色像素点在行列里面的投影
%%%%%%%%%%%%%%原版%%%%%%%%%%%%%%
% for i=1:row%从上往下
%     if (a(i)>60)%横向投影大于50的地方进行切割
%         break;
%     end
% end
% p=i;%记录此时的值
% for i=row:-1:1
%     if (a(i)>60)
%         break;
%     end
% end
% q=i;
%%%%%%%%%%%自己改版%%%%%%%%%%%%%
c=0;d=0;
for i=1:row%从上往下
    if (a(i)>50)%横向投影大于50的地方进行切割
        c=c+1;
        if(c>20)
            break
        end
    end
end
p=i-20;%记录此时的值
for i=row:-1:1
    if (a(i)>50)
        d=d+1;
        if(d>20)
           break
        end
    end
end
q=i+20;
f=f(p:q,:);
%%%%%%%%改版结束,成功%%%%%%%%%
figure,imshow(f)
%%%%%%%%继续新增%%%%%%%%%%%
x=0;y=0;
 [row1 col1]=size(f);
for j=1:col1
    for i=1:row1
        b(j)=b(j)+f(i,j);%纵向投影
    end
end
figure,plot(1:col1,b)
for j=1:col1%从左往右
    if (b(j)>7)%纵向投影大于50的地方进行切割
        x=x+1;
        if(x>15)
            break
        end
    end
end
m=j-20;
for j=col1:-1:1%从右往左
    if (b(j)>7)%纵向投影大于50的地方进行切割
        y=y+1;
        if(y>15)
            break
        end
    end
end
n=j+20;
m,n,col1,row1
f=f(:,m:n);
figure,imshow(f);
%plot(1:row,a),figure,plot(1:col,b)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% wide=0;
% flag=0;
% if sum(f(:,wide+1))~=0
%         flag=1;
%         wide=wide+1;
% end
% if flag==1
%     while sum(f(:,wide+1))~=0
%         wide=wide+1;
%     end
% end
% if (wide<10) %  认为是干扰
% 	    f(:,[1:wide])=0;
% end 
%%%%%%%%%%第二改版%%%%%%%%%%%%%%%%
% figure,imshow(f)
c=zeros(16);
k=1;
if (k<17)
    for i=(c(k)+1):n
        if (mod(k,2))
            if (b(i)>1)%投影的阈值为3
                c(k)=i;
                k=k+1;
            end
        else if (b(i)<1)
               c(k)=i;
               k=k+1;
            end
        end
     end
end
% c(1),c(2),c(3),c(4),c(5),c(6),c(7),c(8),c(9),c(10),c(11),c(12),c(13),c(14)

    z1=f(1:end,c(1):c(2));
    z2=f(1:end,c(3):c(4));
    z3=f(1:end,c(5):c(6));
    z4=f(1:end,c(7):c(8));
    z5=f(1:end,c(9):c(10));
    z6=f(1:end,c(11):c(12));
    z7=f(1:end,c(13):c(14));
%   z8=f(1:end,c(15):c(16));
   figure,
subplot (241) ,imshow(z1) ,title ('one') ;
hold on
subplot (242) ,imshow(z2) ,title ('two') ;
subplot (243) ,imshow(z3) ,title ('three') ;
subplot (244) ,imshow(z4) ,title ('four') ;
subplot (245) ,imshow(z5) ,title ('five') ;
subplot (246) ,imshow(z6) ,title ('six') ;
subplot (247) ,imshow(z7) ,title ('seven') ;
%subplot (248) ,imshow(z8) ,title ('eight') ;
%imwrite(z1,'z1.jpg')
%imwrite(z2,'z2.jpg')
%imwrite(z3,'z3.jpg')
%imwrite(z4,'z4.jpg')
%imwrite(z5,'z5.jpg')
%imwrite(z6,'z6.jpg')
%imwrite(z7,'z7.jpg')
%imwrite(z8,'z8.jpg')

  这段代码经过几次修改,还是失败告终,不知道是不是投影出现了问题。不过如果阈值调得够好的话,是能切出字符的。

你可能感兴趣的:(matlab 车牌分割的算法)