Characters on plate are in different Indian languages, as well as in English. Due to variations in the representation of number plates, vehicle number plate extraction, character segmentation and recognition are crucial. We present the number plate extraction, character segmentation and recognition work, with english characters.
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. If you are new with MATLAB or image processing, then check our previous MATLAB projects:
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.
Now, copy and paste the below code intemplate_creation.m file, and save the file in the project folder (Number Plate Detection). All the files related to this project including image templates files can be downloaded from here. Also 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:
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)’.
Now start coding Letter_detection.m, in a new editor window.
Letter Detection
Here we are creating the second code file namedLetter_detection.m. Now, copy and paste the below code in that file and save the file in the project folder with name Letter_detection. This file can be downloaded from here, this attached zip files also contains other files related to this Number plate detection project.
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,
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. For quick start you can download all the code files with image templates from here.
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).
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;
Complete working of Vehicle License Number Plate Detection System is demonstrated in the Video below, and all the code files with image templates can be downloaded from here.
Also, check all MATLAB Projects here.
12 Comments
on 29 Mar 2012
http://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup
on 29 Mar 2012
It makes it easier for us if you tell us what error you see, on which line. We do not have your images, so we cannot run the program to see the results ourselves.
on 29 Mar 2012
You say 'I am getting an error and I am unable to remove it' - well apparently you did remove it from your question. We have no idea what the error is because you didn't include it.
on 31 Mar 2012
'??? Undefined function or variable 'y_cor1'.
lp=d(y_cor1:y_cor2,x_cor1:x_cor2);'
May be the actual problem may lie in the Neighbour rule loop.. because variable 'x' is not getting incremented (donno why 'x' is not satisfying any condition) and hence may satisfy 108th line
break;
and ends in break.. so the next 'if' loop of line 120 may not satisfy and the further inner loops line 126
if(sss<ss_prev && sss<0 && ca <=190 && ra<=60 && ca>=50 && ra >=15 && mean(mean(f1))<=220)
may not be working... may be it not entering the main 'if' loop of line 120 itself and hence that loop may not be working... I am not sure whether this is the reason or not.. plese help me in getting the output through this code as early as possible because i have vry less time for implementation.
on 31 Mar 2012
Input image can be any image of a number plate or licencse plate of a car.
on 31 Mar 2012
Can you learn how to use the debugger to set breakpoints and step through your code to find out why loops are not getting entered, variables aren't getting assigned and are undefined/empty? That is FAR more efficient than asking us to do that for you. You will discover problems with your code faster that way than posting here and waiting for us. This looks like an easy, straightforward debugging task to me.
on 1 Apr 2012
K Sir, thank you so much... i will try using debuggers.
on 4 Apr 2012
With respect....
But code is not clear, not segmented and not commented.
If you expect help, you need to provide clear and readable code.
on 4 Mar 2016
Hello sir .. Sir can you provide the image on which you run this code? The images that you tested for this code.
on 16 Jan 2017
Hello Sir can u help in color detection using matlab
on 16 Jan 2017
Hammad Sarwar: the person who posted this question has not been active for nearly 4 years, and will not be notified of your comment. You would be more likely to get a response if you started a new Question and described what you need to do.
on 16 Jan 2017
Hammad, see my File Exchange for several color segmentation demoshttp://www.mathworks.com/matlabcentral/fileexchange/?term=authorid%3A31862. In addition, try the Color Thresholder app on the Apps tab of the tool ribbon of MATLAB.
Sign in to comment.