Challenge

FINAL INDEPENDENT PROJECT

AIM:- To write a program to detect the number plate of a car.

THEORY:-

The concept behind the plate detection is :-The camera of the ANPR system captures image of vehicle license plate and then the image is processed through multiple number of algorithms to provide an alpha numeric conversion of the image into a text format. ANPR system is used at many places like Petrol Pumps, Shopping Malls, Airports, highways, toll booths, Hotels, Hospitals, Parking lots, Defense & Military check points etc.

There are many image processing tools available for this Number plate detection, but here in this tutorial we will use MATLAB Image Processing to get the vehicle license plate number into the text format.

Matlab code:-

%Alphabets
A=imread('alpha/A.bmp');B=imread('alpha/B.bmp');C=imread('alpha/C.bmp');
D=imread('alpha/D.bmp');E=imread('alpha/E.bmp');F=imread('alpha/F.bmp');
G=imread('alpha/G.bmp');H=imread('alpha/H.bmp');I=imread('alpha/I.bmp');
J=imread('alpha/J.bmp');K=imread('alpha/K.bmp');L=imread('alpha/L.bmp');
M=imread('alpha/M.bmp');N=imread('alpha/N.bmp');O=imread('alpha/O.bmp');
P=imread('alpha/P.bmp');Q=imread('alpha/Q.bmp');R=imread('alpha/R.bmp');
S=imread('alpha/S.bmp');T=imread('alpha/T.bmp');U=imread('alpha/U.bmp');
V=imread('alpha/V.bmp');W=imread('alpha/W.bmp');X=imread('alpha/X.bmp');
Y=imread('alpha/Y.bmp');Z=imread('alpha/Z.bmp');

%Natural Numbers
one=imread('alpha/1.bmp');two=imread('alpha/2.bmp');
three=imread('alpha/3.bmp');four=imread('alpha/4.bmp');
five=imread('alpha/5.bmp'); six=imread('alpha/6.bmp');
seven=imread('alpha/7.bmp');eight=imread('alpha/8.bmp');
nine=imread('alpha/9.bmp'); zero=imread('alpha/0.bmp');

%Creating Array for Alphabets
letter=[A B C D E F G H I J K L M N O P Q R S T U V W X Y Z];

%Creating Array for Numbers
number=[one two three four five six seven eight nine zero];

NewTemplates=[letter number];
save ('NewTemplates','NewTemplates')
clear all

Here in the above code we are saving the images into a variable by using command ‘imread()’. This function is used to call the images from the folder or from any location of the PC into the MATLAB

LETTER DETECTION:-

Here we are creating the second code file named Letter_detection.m. Now, copy and paste the below code in that file and save the file in the project folder with name Letter_detection.

function letter=readLetter(snap)

load NewTemplates
snap=imresize(snap,[42 24]);
rec=[ ];

for n=1:length(NewTemplates)
    cor=corr2(NewTemplates{1,n},snap);
    rec=[rec cor];
end

ind=find(rec==max(rec));
display(find(rec==max(rec)));

% Alphabets listings.
if ind==1 || ind==2
    letter='A';
elseif ind==3 || ind==4
    letter='B';
elseif ind==5
    letter='C'
elseif ind==6 || ind==7
    letter='D';
elseif ind==8
    letter='E';
elseif ind==9
    letter='F';
elseif ind==10
    letter='G';
elseif ind==11
    letter='H';
elseif ind==12
    letter='I';
elseif ind==13
    letter='J';
elseif ind==14
    letter='K';
elseif ind==15
    letter='L';
elseif ind==16
    letter='M';
elseif ind==17
    letter='N';
elseif ind==18 || ind==19
    letter='O';
elseif ind==20 || ind==21
    letter='P';
elseif ind==22 || ind==23
    letter='Q';
elseif ind==24 || ind==25
    letter='R';
elseif ind==26
    letter='S';
elseif ind==27
    letter='T';
elseif ind==28
    letter='U';
elseif ind==29
    letter='V';
elseif ind==30
    letter='W';
elseif ind==31
    letter='X';
elseif ind==32
    letter='Y';
elseif ind==33
    letter='Z';
 %*-*-*-*-*
​
% Numerals listings.
elseif ind==34
    letter='1';
elseif ind==35
    letter='2';
elseif ind==36
    letter='3';
elseif ind==37 || ind==38
    letter='4';
elseif ind==39
    letter='5';
elseif ind==40 || ind==41 || ind==42
    letter='6';
elseif ind==43
    letter='7';
elseif ind==44 || ind==45
    letter='8';
elseif ind==46 || ind==47 || ind==48
    letter='9';
else
    letter='0';
end
end

Here, in the above code we have created a function named letter which gives us the alphanumeric output of the input image from class ‘alpha’ by using command ‘readLetter()’. And then load the saved templates by using command load ‘NewTemplates. After that, we have resized the input image so it can be compared with the template’s images by using the command ‘imresize(filename,size)’. Then for loop is used to correlates the input image with every image in the template to get the best match.

NUMBER PLATE DETECTION:-

Here is the third and final code file named Plate_detection.m copy and paste the below code in this file and save into the project folder.

close all;
clear all;

im = imread(' Number Plate Images/ image1.png');
imgray = rgb2gray(im);
imbin = imbinarize(imgray);
im = edge(imgray, 'prewitt');

%Below steps are to find location of number plate
Iprops=regionprops(im,'BoundingBox','Area', 'Image');
area = Iprops.Area;
count = numel(Iprops);
maxa= area;
boundingBox = Iprops.BoundingBox;
for i=1:count
   if maxa<Iprops(i).Area
       maxa=Iprops(i).Area;
      ​ boundingBox=Iprops(i).BoundingBox;
   end
end   

im = imcrop(imbin, boundingBox);
im = bwareaopen(~im, 500);
 [h, w] = size(im);

imshow(im);

Iprops=regionprops(im,'BoundingBox','Area', 'Image');
count = numel(Iprops);
noPlate=[];
for i=1:count
   ow = length(Iprops(i).Image(1,:));
   oh = length(Iprops(i).Image(:,1));
   if ow<(h/2) & oh>(h/3)
       letter=Letter_detection(Iprops(i).Image);
       noPlate=[noPlate letter]
   end
end
close all;
clear all;

im = imread(' Number Plate Images/ image1.png');
imgray = rgb2gray(im);
imbin = imbinarize(imgray);
im = edge(imgray, 'prewitt');

%Below steps are to find location of number plate
Iprops=regionprops(im,'BoundingBox','Area', 'Image');
area = Iprops.Area;
count = numel(Iprops);
maxa= area;
boundingBox = Iprops.BoundingBox;
for i=1:count
   if maxa<Iprops(i).Area
       maxa=Iprops(i).Area;
      ​ boundingBox=Iprops(i).BoundingBox;
   end
end   

im = imcrop(imbin, boundingBox);
im = bwareaopen(~im, 500);
 [h, w] = size(im);

imshow(im);

Iprops=regionprops(im,'BoundingBox','Area', 'Image');
count = numel(Iprops);
noPlate=[];
for i=1:count
   ow = length(Iprops(i).Image(1,:));
   oh = length(Iprops(i).Image(:,1));
   if ow<(h/2) & oh>(h/3)
       letter=Letter_detection(Iprops(i).Image);
       noPlate=[noPlate letter]
   end
end

BASIC COMMANDS:-

imread() – This command is used to open the image into the MATLAB from the target folder.

rgb2gray() –This command is used to convert the RGB image into grayscale format.

imbinarize() – This command is used to Binarize 2-D grayscale image or simply we can say it converts the image into black and white format.

edge() – This command is used to detect the edges in the image, by using various methods like Roberts, Sobel, Prewitt and many others.

regionprops() – This command is used to measure properties of image region.

numel() – This command is used to calculate the number of array elements.

imcrop() – This command is used to crop the image in the entered size.

bwareaopen() – This command is used to remove small objects from binary image.

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.