Have you ever wonder that how an ANPR (Automatic Number Plate Recognition) system works? Let me tell you the concept behind it, 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.

First, let me brief you about the concept we are using for detecting number plates. There are three programs or ‘.m’ files for this project.

• Template Creation (template_creation.m)– This is used to call the saved images of alphanumerics and then save them as a new template in MATLAB memory.
• Letter Detection(Letter_detection.m) – Reads the characters from the input image and find the highest matched corresponding alphanumeric.
• Plate Detection(Plate_detection.m) – Process the image and then call the above two m-files to detect the number.

Now, we will learn about how to code these m-files and what you have to do before start coding. After going through this tutorial, you can find all the code files and working explanation video at the end of this project.

### Template Creation

First create a folder for the project (my folder name is Number Plate Detection) to save and store the files. We have stored the binary images of all the alphabets and numbers in the sub-folder named as ‘alpha’. Now, open the Editor window in the MATLAB, as shown in the below image,

If you are not familiar with the basic terminology of MATLAB I suggest you to check the linked tutorial. Check the video given at the end of this project.

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. Let’s take an example from the above code:

`A=imread('alpha/A.bmp');`

Where A is the variable, and in ‘alpha/A.bmp’‘alpha’ is the folder name and ‘A.bmp’ is the file name.

Then create a matrix of ‘letter’ and ‘number’ and save it in variable ‘NewTemplates’ by using command ‘save(filename,variables)’.

```%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```

Now start coding Letter_detection.m, in a new editor window.

### Letter Detection

Here we are creating the second code file namedLetter_detection.m

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.

A matrix ‘rec’ is created to record the value of correlation for each alphanumeric template with the characters template from the input image, as shown in the below code,

`cor=corr2(NewTemplates{1,n},snap);`

Then ‘find()’ command is used to find the index which corresponds to the highest matched character. Then according to that index, corresponding character is printed using‘if-else’ statement.

Now, after completing with this open a new editor window to start code for the main program.

### 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.

Basic commands used in above code are mentioned below:

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.

By using the above commands in the code, we are calling the input image and converting it into the grayscale. Then the grayscale is converted into the binary image, and the edge of the binary images is detected by the Prewitt method.

Then the below code is used to detect the location of the number plate in the entire input image,

After that crop the number plate and remove the small objects from the binary image by using command ‘imcrop()’and ‘bwareaopen()’ respectively.

Then, the below code is used to process that cropped license plate image and to display the detected number in the image and text format (in the command window).

```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```

### Working of Vehicle License Plate Number Detection System using MATLAB

In the template_creation.m file we have design the code to save all the binary images of alphanumerics into a directory or file named as ‘NewTemplates’. Then that directory is called in the Letter_detection.m as you can see in the below

Then in the Plate_detection.m code file the Letter_detection.m code file is called when we process the image as shown in image below,

Now, click on the ‘RUN’ button to run the .m file

MATLAB may take few seconds to respond, wait until it shows busy message in the lower left corner as shown below,

As the program start you will get the number plate image popup and the number in the command window. The output for my image will look like the image given below;