号码牌中的所有字符都无法识别

时间:2018-01-30 07:58:05

标签: matlab image-processing

我正在从号牌中提取和分割字符。我遇到字符识别问题。我想提取数字和符号,不考虑字母表。我从八个输出中得到一个字符。运行此代码后,我得到的回答是' 1'。我附加了matlab代码用于板的位置和分段。我已经提供了参考的输入图像。untitled0 input image untitled1 untitled2`untitled3] 5 [untitled4] { {3}} [untitled5] 6 [sign] 7任何人都可以帮我纠正此错误吗?

function [ImgPlate] = LocationPlate(I)

%% Cutting and resizing the original image %%
% I=imread('2222.jpg');
% figure,imshow(I);

[rows columns]=size(I);
columns=columns/3;
xmin=round(0.20*rows);
ymin=round(0.20*columns);
width=round((0.85*columns)-(0.10*columns));
height=round((0.85*rows)-(0.15*rows));
Io=imcrop(I,[xmin ymin width height]);
Io=imresize(Io,[480 640]);
Io=rgb2gray(Io);
Io=imadjust(Io);

 %% Image processing to focus the area of number plate %%
 %% Smooth edges and contours to delete characters.

 se=strel('rectangle',[6 30]);

    Ic=imclose(Io,se);
    Ic=imadjust(Ic);
    tophat=Ic-Io;
    Ibw=(tophat>85);

 %% Remove the related elements with fewer than 70 pixels %%
 %% Remove objects that are not plate %%

 plate= bwlabel(Ibw,4);
 obj= max(max(plate));
 dim1 = regionprops(plate, 'area')';

    dim=[dim1.Area];
    dim(find(dim<70))=0;

for i=1:obj


index=find(plate==i);
if dim(i)==0
    plate(index)=0;

else
    plate(index)=1;
end

end


CC=bwconncomp(plate);
P=regionprops(CC,'all');
[rp cp]=size(plate);


 for i=1:CC.NumObjects


    if P(i).MajorAxisLength>(2*cp/3)
    plate(P(i).PixelIdxList(:,1))=0;
    end

  end

 %% Remove objects that are not candidates for plate %%

 se3=strel('rectangle',[30 70]);
 r2=imclose(plate,se3);

 se2=strel('rectangle',[5 30]);
 r=imdilate(r2,se2);


    CC=bwconncomp(r);
    P=regionprops(CC,'all');

 for i=1:CC.NumObjects


    if P(i).MajorAxisLength>(2*cp/3)
    r(P(i).PixelIdxList(:,1))=0;

    end

    end

 %% select the largest connected component after preprocessing, the
    %%plate

 plate1= bwlabel(r,4);
 dim2= regionprops(plate1, 'area')';

    dim1=[dim2.Area];
    f=max(dim1);
    indMax=find(dim1==f);
    plate1(find(plate1~=indMax))=0;

 %% cutting of original image %%

 [cuty, cutx] = find( plate1 > 0);
 up = min(cuty);

    down = max(cuty);
    left = min(cutx);
    right = max(cutx);
    img_cut_v = Io(up:down,:,:);
    img_cut_h = img_cut_v(:,left:right,:);

    ImgPlate = img_cut_h;

    %% different mask for location plate %%


    [r c]=size(ImgPlate);


    if r<25 || r>65

    [rows columns]=size(I);
    columns=columns/3;
    xmin=round(0.20*rows);
    ymin=round(0.20*columns);
    width=round((0.85*columns)-(0.10*columns));
    height=round((0.85*rows)-(0.15*rows));
    Io=imcrop(I,[xmin ymin width height]);
    Io=imresize(Io,[480 640]);
    Io=rgb2gray(Io);
    Io=imadjust(Io);

    se=strel('rectangle',[6 30]);
    Ic=imclose(Io,se);
    Ic=imadjust(Ic);
    tophat=Ic-Io;
    Ibw1=(tophat>85);
    mask=zeros(480,640);
    for i=40:370
    for j=40:575
    mask(i,j)=1;
    end
    end


    Ibw=Ibw1 & im2bw(mask);
    plate= bwlabel(Ibw,4);
    obj= max(max(plate));
    dim1 = regionprops(plate, 'area')';
    dim=[dim1.Area];
    dim(find(dim<70))=0;


    for i=1:obj
    index=find(plate==i);
    if dim(i)==0
    plate(index)=0;

    else
    plate(index)=1;

    end
    end


    CC=bwconncomp(plate);
    P=regionprops(CC,'all');
    [rp cp]=size(plate);


    for i=1:CC.NumObjects
    if P(i).MajorAxisLength>(cp/3)
    plate(P(i).PixelIdxList(:,1))=0;

    end
    end


    se3=strel('rectangle',[30 70])



    r2=imclose(plate,se3);
    se2=strel('rectangle',[5 30]);
    r=imdilate(r2,se2);


    plate1= bwlabel(r,4);
    dim2= regionprops(plate1, 'area')';
    dim1=[dim2.Area];
    f=max(dim1);
    indMax=find(dim1==f);
    plate1(find(plate1~=indMax))=0;

    [cuty, cutx] = find( plate1 > 0);

    up = min(cuty);
    down = max(cuty);
    left = min(cutx);
    right = max(cutx);
    img_cut_v = Io(up:down,:,:);
    img_cut_h = img_cut_v(:,left:right,:);
    ImgPlate = img_cut_h;

    end

    %% Representation %%


    % figure(1);
    % imshow(I);
    % subplot(2,2,1);imshow(I);
    % subplot(2,2,2);imshow(Ic);% subplot(2,2,3);imshow(plate);
    % subplot(2,2,4);imshow(plate1);


    figure(2);  imshow(img_cut_h);title('output location plate');
    end
function [Objects,ImgChar]=Segmentation(ImgPlate)

    %% Binarize the image %%
    level = graythresh(ImgPlate);
    Ibw =(im2bw(ImgPlate,level));


    %% Select the orientation of the largest object in the image.
    %% Turn this angle at the picture.
    %% Plate cutting to segment the characters that compose %%


    Fl=bwlabel(Ibw);
    Statsbf=regionprops(Fl,'all');
    Flmax=find([Statsbf.Area]==max([Statsbf.Area]));
    angle=Statsbf(Flmax).Orientation;
    F2=imrotate(Fl,-angle);
    L=bwlabel(F2);
    Statsbf=regionprops(L,'all');
    maxi=find([Statsbf.Area]==max([Statsbf.Area]));
    BB=Statsbf(maxi).BoundingBox;
    F2=imcrop(F2,[BB(1,1) BB(1,2) BB(1,3) BB(1,4)]);
% figure,imshow(F2);

    %% First three and last three rows to zero.
    %% First two and last two columns to zero.
    %% So remove connectivity between characters and background %%
    %% Remove small impurities %%


    L4=not(F2);
    [r c]=size(L4);
    L4(1,:)=0;
    L4(2,:)=0;
    L4(3,:)=0;
    L4(r,:)=0;
    L4(r-1,:)=0;
    L4(r-2,:)=0;
    L4(:,1)=0;
    L4(:,2)=0;
    L4(:,c)=0;
    L4(:,c-1)=0;


    L4b=bwlabel(L4);
    Stats3=regionprops(L4b,'all');
    sarea3=[Stats3.Area];
    G=find(sarea3<70);


    for cv=1:length(G)
    G1=find(L4b==G(cv));
    L4(G1)=0;

    end
    [r c]=size(L4);
    CC=bwconncomp(L4);
    L=bwlabel(L4);
    ind2=max(L(:,c-2));
    P=regionprops(CC,'all');

    %% Remove objects smaller and larger than a character %%


    i=1;
    if  (max(P(i,1).PixelList(:,1))-min(P(i,1).PixelList(:,1)))<(c/13)
             L4(CC.PixelIdxList{1,i})=0;
    end

    for i=1:CC.NumObjects


    if (max(P(i,1).PixelList(:,1))-min(P(i,1).PixelList(:,1)))>(2*c/3)
         L4(CC.PixelIdxList{1,i})=0;

    end

    if (max(P(i,1).PixelList(:,2))-min(P(i,1).PixelList(:,2)))<(r/3)

       L4(CC.PixelIdxList{1,i})=0;
    end


    if (max(P(i,1).PixelList(:,1))-min(P(i,1).PixelList(:,1)))<(c/8)
        L4(find(L==ind2))=0;

    end

    end


    L4=imclose(L4,strel('disk',1));
    L4=imopen(L4,strel('disk',1));
%   figure(4);
    imshow(L4);
    L4b=bwlabel(L4);
    Stats3b=regionprops(L4b,'all');

    N=length(Stats3b);


    while N>8
    L4=imdilate(L4,strel('disk',1));
    L4b=bwlabel(L4);
    Stats3b=regionprops(L4b,'all');
    N=length(Stats3b);

    end


    L4b=bwlabel(L4);
    Stats3b=regionprops(L4b,'all');
    ImgChar=zeros(100,100,N);

 %% Dividing characters which are connected %%
 %% Remove objects that have been listed as characters but are not%
 %% Show every character in the correct position %%


    cont=0;
    cont1=0;

 for i=1:N

    [r1 c1]=size(Stats3b(i,1).Image);


    if c1>round(c/6)
    cont1=cont;
    Stats3b(i,1).Image(:,round(c1/2))=0;
    L5=Stats3b(i,1).Image;
    CC=bwconncomp(L5);
    CC1=regionprops(CC,'all');

    for j=1:CC.NumObjects
    [r2 c2]=size(CC1(j,1).Image);
    if c2>round(c/7)

    CC1(j,1).Image(:,round(c2/2))=0;
    L6=CC1(j,1).Image;
    LL=bwconncomp(L6);
    CC2=regionprops(LL,'all');
    for k=1:LL.NumObjects

    CC2(k).Image=imresize(CC2(k).Image, [100 100]);
    figure;imshow((CC2(k).Image))
    ImgChar(:,:,i+cont1)=not(CC2(k).Image);
    cont1=cont1+1;
    end
    cont=cont+1;
    else

    CC1(j).Image=imresize(CC1(j).Image, [100 100]);

    figure;imshow((CC1(j).Image))
    ImgChar(:,:,i+cont1)=not(CC1(j).Image);
    cont1=cont1+1;
    end
    end

    cont=cont+1;


    else
    Stats3b(i).Image=imresize(Stats3b(i).Image, [100 100]);
    figure;imshow((Stats3b(i).Image));


    if cont~=0
    ImgChar(:,:,i+cont)=not(Stats3b(i).Image);
    else
    ImgChar(:,:,i)=not(Stats3b(i).Image);

    end
end
end

 %% Remove spurious %%

 [x, y, Objects]=size(ImgChar);
end

function [strPlate] = Recognition(I)
    I=imread('2222.jpg');
    [ImgPlate] = LocationPlate(I);
    [Objects,ImgChar]=Segmentation(ImgPlate);
    N=struct('Image',{});
    numbers={'0','1','2','3','4','5','sign'};
    N(1).Image=imresize(im2bw(uint8(imread('untitled0.bmp'))),[100 100]);
    N(2).Image=imresize(im2bw(uint8(imread('untitled1.bmp'))),[100 100]);
    N(3).Image=imresize(im2bw(uint8(imread('untitled2.bmp'))),[100 100]);
    N(4).Image=imresize(im2bw(uint8(imread('untitled3.bmp'))),[100 100]);
    N(5).Image=imresize(im2bw(uint8(imread('untitled4.bmp'))),[100 100]);
    N(6).Image=imresize(im2bw(uint8(imread('untitled5.bmp'))),[100 100]);
    N(7).Image=imresize(im2bw(uint8(imread('sign.jpg'))),[100 100]);
    if Objects==8
       strPlate=[];
         for i=1:Objects
         char=ImgChar(:,:,i);
            if  (i==2)||(i==3)||(i==4) || (i==5) || (i==6)||(i==7) ||(i==8)
                list_corr=[];
                for j=1:7
                    corr=corr2(N(j).Image,char);
                   list_corr=[list_corr corr];
               end
            f=max(list_corr);
            maxcorr=find(list_corr==f);
            strPlate=[strPlate numbers(maxcorr)];
         end
      end
    end
 end

1 个答案:

答案 0 :(得分:0)

我发现您将char图片与默认值ImgChar(零)进行比较。我通过添加imshow(char)发现了这一点,如下面的代码所示。

 for i=1:Objects
        char=ImgChar(:,:,i);
        if  (i==2)||(i==3)||(i==4) || (i==5) || (i==6)||(i==7) ||(i==8)
            list_corr=[];
            for j=1:6
                corr=corr2(N(j).Image,char);
                list_corr=[list_corr corr];
            end
            close all
            figure;imshow(char)
            f=max(list_corr);
            maxcorr=find(list_corr==f);
            strPlate=[strPlate numbers(maxcorr)];
        end
    end

为了纠正上述错误,我修改了以下代码,以便在ImgChar中存储图像。这一行可能不正确,但我相信我的回答可能对你有帮助。

Stats3b(i).Image=imresize(Stats3b(i).Image, [100 100]);
        figure;
        imshow((Stats3b(i).Image));

        ImgChar(:,:,i) = Stats3b(i).Image;% added

        if cont~=0

        else
            ImgChar(:,:,i)=not(Stats3b(i).Image);

        end