I am using the following code in Matlab R2012a to extract a license plate from a car's photo.
The task is to remove only the plate but I also get many irrelevant images cropped as plates. The code should be able to extract plates disregarding plate dimensions and position. I have to test it on 25 car photos. Currently it only works on some of them(with additional segments extracted of course). How can I correct my code the achieve the goal mentioned?
clc
clear
close all
%Open Image
I = imread('train_plates/25.jpg');
figure, imshow(I);
%Gray Image
Ig = rgb2gray(I);
figure,
subplot(2,2,1), imshow(Ig);
% %Enhancement
Ih = histeq(Ig);
subplot(2,2,2), imshow(Ih);
figure,
subplot(1,2,1), imhist(Ig);
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,4);
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);
imwrite(im2,'outPlate.jpg');
end
end