matlabcomputer-visionautomatic-license-plate-recognition

LPR with MATLAB: how to find only one rectangle?


I am using the following code in MATLAB to find the rectangle containing a car's license plate:

    clc
clear
close all

%Open Image
I = imread('plate_1.jpg');
figure, imshow(I);

%Gray Image
Ib = rgb2gray(I);
figure,
subplot(1,2,1), imshow(Ib);

%Enhancement
Ih = histeq(Ib);
subplot(1,2,2), imshow(Ih);

figure,
subplot(1,2,1), imhist(Ib);
subplot(1,2,2), imhist(Ih);

%Edge Detection
Ie = edge(Ih, 'sobel');
figure, 
subplot(1,2,1), imshow(Ie);

%Dilation
Id = imdilate(Ie, strel('diamond', 1));
subplot(1,2,2), imshow(Id);


%Fill
If = imfill(Id, 'holes');
figure, imshow(If);

%Find Plate
[lab, n] = bwlabel(If);

regions = regionprops(lab, 'All');
regionsCount = size(regions, 1) ;

for i = 1:regionsCount
    region = regions(i);
    RectangleOfChoice = region.BoundingBox;
    PlateExtent = region.Extent;

    PlateStartX = fix(RectangleOfChoice(1));
    PlateStartY = fix(RectangleOfChoice(2));
    PlateWidth  = fix(RectangleOfChoice(3));
    PlateHeight = fix(RectangleOfChoice(4));

    if PlateWidth >= PlateHeight*3 && PlateExtent >= 0.7
        im2 = imcrop(I, RectangleOfChoice);
        figure, imshow(im2);
    end
end

Plates all have white backgrounds. Currently,I use the rectangles' ratio of width to height to select candidate regions for output. This gives the plate rectangle in addition to several other irrelevant ones in the case of a white car. What method can I use to get only one output: the license plate? Also, I don't find a plate at all when I run the code on a black car. Maybe that's because the car's color is the same as the plate edge. Are there any alternatives to edge detection to avoid this problem?


Solution

  • Try this !!!

    I = imread('http://8pic.ir/images/88146564605446812704.jpg');
    im=rgb2gray(I);
    sigma=1;
    f=zeros(128,128);
    f(32:96,32:96)=255;
    [g3, t3]=edge(im, 'canny', [0.04 0.10], sigma);
    se=strel('rectangle', [1 1]);
    BWimage=imerode(g3,se);
    gg = imclearborder(BWimage,8);
    bw = bwareaopen(gg,200);
    gg1 = imclearborder(bw,26);
    imshow(gg1);
    
    %Dilation
    Id = imdilate(gg1, strel('diamond', 1));
    imshow(Id);
    
    %Fill
    If = imfill(Id, 'holes');
    imshow(If);
    
    %Find Plate
    [lab, n] = bwlabel(If);
    
    regions = regionprops(lab, 'All');
    regionsCount = size(regions, 1) ;
    
    for i = 1:regionsCount
        region = regions(i);
        RectangleOfChoice = region.BoundingBox;
        PlateExtent = region.Extent;
    
        PlateStartX = fix(RectangleOfChoice(1));
        PlateStartY = fix(RectangleOfChoice(2));
        PlateWidth  = fix(RectangleOfChoice(3));
        PlateHeight = fix(RectangleOfChoice(4));
    
       if PlateWidth >= PlateHeight*1 && PlateExtent >= 0.7
            im2 = imcrop(I, RectangleOfChoice);
            %figure, imshow(I);
            figure, imshow(im2);
        end
    end