如何从黑白图片中提取车牌

时间:2015-01-11 17:52:49

标签: matlab image-processing

我得到了这个功课,我必须从车的照片中提取车牌。我不需要识别板号和字母(这是同事的工作)。我必须在Matlab中做到这一点。我已经完成了铭牌识别部分,但现在我无法理解如何从图片中正确提取车牌部分。

您可以在下图中看到我到目前为止所做的事情: enter image description here

现在我不知道如何裁剪白色部分,而且它应该是这样的: enter image description here

我的意思是应该旋转它以使其达到水平位置。

这是一个需要由两个人完成的项目。第一个(我)必须从任何类型的图像中仅给出第二个牌照,第二个必须使用OCR。这就是教授所说的。

我没有想法。答案可能很简单,但我整天工作,我找不到解决方案。

2 个答案:

答案 0 :(得分:2)

注意:变量使用不同的语言,但我相信代码是可读的。对不起,我现在没有时间来处理它:(

现在这个算法并不是最好的,但却是一个起点。我现在没有时间研究它,但它肯定可以改进。例如,正如在底部的图像中可以看到的那样,牌照位置填充得非常好,但在某些图片中,最后一行白色像素的长度与上面的不一样,所以你最终可以只有一半的车牌。一种解决方案可以是:在获得牌照的角落之前,使所有白线长度相同(最长线的长度)

BW = edge(rgb2gray(imagine), 'canny');

    %# hough transform
    [H T R] = hough(BW);
    P  = houghpeaks(H, 4, 'threshold',ceil(0.75*max(H(:))));
    lines = houghlines(BW, T, R, P);

    % shearing transformata
    slopes = vertcat(lines.point2) - vertcat(lines.point1);
    slopes = slopes(:,2) ./ slopes(:,1);
    TFORM = maketform('affine', [1 -slopes(1) 0 ; 0 1 0 ; 0 0 1]);
    imagine2 = imtransform(imagine, TFORM);

    %# show image with lines overlayed, and the aligned/rotated image
    axes(handles.ImagineRotita)
    imshow(imagine2); 

    imagineGrey = rgb2gray(imagine2);

%-----------------------------------------------------------------

    axes(handles.ImaginePrel1)
    imshow(imagineGrey);

    imagineFiltrata = medfilt2(imagineGrey,[3 3]);
    axes(handles.ImagineFilt)
    imshow(imagineFiltrata);

    SE = strel('disk',1);
    GrayDil = imdilate(imagineGrey,SE);
    GrayEr = imerode(imagineGrey,SE);
    gdiff = imsubtract(GrayDil,GrayEr);
    gdiff = mat2gray(gdiff);
    gdiff = conv2(gdiff,[1 1;1 1]);
    gdiff = imadjust(gdiff,[0.5 0.7],[0 1],0.1);

    B = logical(gdiff);
    er = imerode(B,strel('line',50,0));
    out1 = imsubtract(B,er);

    imagineSobel = imfill(out1,'holes');

    H = bwmorph(imagineSobel,'thin',1);
    H = imerode(H,strel('line',3,90));

    final = bwareaopen(H,100);

    Iprops = regionprops(final,'BoundingBox','Image');

    axes(handles.ImagineSobel)
    imshow(final);

    imagineCuratata = imclearborder(final,18);
    imagineCuratata = bwareaopen(imagineCuratata,200);
    axes(handles.ImagineSobelEd)
    imshow(imagineCuratata);

    fileID = fopen('ImagineRezultata.txt','wb');
    dlmwrite('ImagineRezultata.txt',imagineCuratata);
    fclose('all');

    imagineCuratata2 = imagineCuratata;

    [dimX,dimY] = size(imagineCuratata2);

%-----------------------------------------------------------------
%-----------The licence plate is filled with white pixels---------
PrimPixelAlbStanga = 0;
PrimPixelAlbDreapta = 0;
Flag = 0;

for i = 1:dimX
    for j = 1:dimY
        if Flag == 0
            if imagineCuratata2(i,j) == 1
                PrimPixelAlbStanga = j;
                Flag = 1;
            end
        end
        if Flag == 1
            if imagineCuratata2(i,j) == 1
                PrimPixelAlbDreapta = j;
            end
        end
    end
    if PrimPixelAlbStanga > 0
        for k = PrimPixelAlbStanga:PrimPixelAlbDreapta
            imagineCuratata2(i,k) = 1;
        end
    end
    PrimPixelAlbStanga = 0;
    PrimPixelAlbDreapta = 0;
    Flag = 0;
end

%-----------------------------------------------------------------

%-----------------------------------------------------------------
%----------The smaller lines with white pixels are removed--------
%----------this is to eliminate all the smaller lines that--------
%--------------remained for example the logo sometimes------------

PrimPixelAlbStanga = 0;
PrimPixelAlbDreapta = 0;
Flag = 0;

for i = 1:dimX
    for j = 1:dimY
        if Flag == 0
            if imagineCuratata2(i,j) == 1
                PrimPixelAlbStanga = j;
                Flag = 1;
            end
        end
        if Flag == 1
            if imagineCuratata2(i,j) == 1
                PrimPixelAlbDreapta = j;
            end
        end
    end
    if PrimPixelAlbStanga > 0
        if (PrimPixelAlbDreapta - PrimPixelAlbStanga) < 40
            for k = PrimPixelAlbStanga:PrimPixelAlbDreapta
                imagineCuratata2(i,k) = 0;
            end
        end
    end
    PrimPixelAlbStanga = 0;
    PrimPixelAlbDreapta = 0;
    Flag = 0;
end

axes(handles.ImagineCropata)
imshow(imagineCuratata2);

%-----------------------------------------------------------------

%-----------------------------------------------------------------
%---------the corners of the licence plate are determined---------

imagineCuratata2 = bwareaopen(imagineCuratata2,500);

PrimaCoordX = 0;
PrimaCoordY = 0;
UltimaCoordX = 0;
UltimaCoordY = 0;
determinat = 0;

for i = 1:dimX
    for j = 1:dimY
        if imagineCuratata2(i,j) == 1
            if determinat == 0
                PrimaCoordX = j;
                PrimaCoordY = i;
                determinat = 1;
            else
                            UltimaCoordX = j;
                            UltimaCoordY = i;
            end            
        end
    end
end

PrimaCoordColX = 0;
PrimaCoordColY = 0;
UltimaCoordColX = 0;
UltimaCoordColY = 0;
determinatCol = 0;

for j = 1:dimY
    for i = 1:dimX
        if imagineCuratata2(i,j) == 1
            if determinatCol == 0
                PrimaCoordColX = j;
                PrimaCoordColY = i;
                determinatCol = 1;
            else                
                    UltimaCoordColX = j;
                    UltimaCoordColY = i;
            end            
        end
    end
end

    xmin = 0;
    ymin = 0;
    width = 0;
    height = 0;

    if PrimaCoordX < dimX/2
        xmin = PrimaCoordX;
        ymin = PrimaCoordY;
    else 
        xmin = PrimaCoordColX;
        ymin = PrimaCoordY;
    end

    xmin = xmin - xmin * 0.1;
    ymin = ymin * 1.1;

    if PrimaCoordX < dimX/2
        width = UltimaCoordX - PrimaCoordX;
        height = UltimaCoordY - PrimaCoordY;
    else
        width = UltimaCoordColX - PrimaCoordColX;
        height = UltimaCoordY - PrimaCoordY;
    end

    rect = [xmin ymin abs(width) abs(height)];

    RezultatCropat = imcrop(imagine2, rect);

    axes(handles.ImagineCropataSiFilt)
    imshow(RezultatCropat);

end

在这些图片中可以看到我所做的结果: enter image description here enter image description here

答案 1 :(得分:0)

如果您正在尝试物理操纵图像以从任意数量的有角度的原件创建最终的展平表示,那么这可能不是继续进行的方式。

如果您已经定义了识别印版边界的逻辑,那么您应该能够捕捉它,然后通过OCR应用程序运行它。这将为您提供文本,然后您可以使用该文本创建一个平面图像。

现在,如果你必须从盘子中捕获任何图形,你需要追踪所有可能的图形图像的数据库,然后编写比较/匹配例程,这样你就可以识别车牌图形,然后使用数据库中的“平面”版本创建展平图像,然后输出OCR文本。

例如,我只删了牌照,并在images.google.com上搜索(使用“按图搜索”功能,我找到了很多"flattened" versions of the image你可以用作你的模板。