How to decide the best threshold to convert RGB image to Binary

Hi,
my project is to make an automatic plate recognition system my code consists of 2 parts: 1- extracts the the plate from the recorded image. 2- extracts the numbers from the plate , my problem is to find the best threshold for the recorded image in which the numbers of plate is clear so i can identify it so here is my code:
tic
clc;
clear all;
% the user enters the dimension of the plate (i.e: the distance from camera)
U=input('enter The Width of the Plate(in integer value):');
T=input('enter The Height of the plate(only integers value):');
tic
I = imread ('82.JPG');
imagen=I;
% searching for the best threshold to convert my RGB image to Binary
% i found by experiment that Binary images that have clear %numbers ,its threshold lies in the range of 0.3-->0,7
imagen1 =~im2bw(imagen,0.3);
imagen1=imclearborder(imagen1,26);
imagen1 = bwareaopen(imagen1,30);
lp1=lpcropv(imagen1,U,T); % lpCrop is a function give it a binary image %and returns the plate only by finding the maximum edge density
imagen1=lp1;
stats1 = regionprops(imagen1);
figure; imshow(imagen1);
imagen2 =~im2bw(imagen,0.4);
imagen2=imclearborder(imagen2,26);
imagen2 = bwareaopen(imagen2,30);
lp2=lpcropv(imagen2,U,T);% lpCrop is a function give it a binary % image and returns the plate only by finding the maximum edge density
imagen2=lp2;
stats2 = regionprops(imagen2);
figure; imshow(imagen2);
imagen3 =~im2bw(imagen,0.5);
imagen3=imclearborder(imagen3,26);
imagen3 = bwareaopen(imagen3,30);
lp3=lpcropv(imagen3,U,T);% lpCrop is a function give it a binary % image and returns the plate only by finding the maximum edge density
imagen3=lp3;
stats3 = regionprops(imagen3);
figure; imshow(imagen3);
imagen4 =~im2bw(imagen,0.6);
imagen4=imclearborder(imagen4,26);
imagen4 = bwareaopen(imagen4,30);
lp4=lpcropv(imagen4,U,T);% lpCrop is a function give it a binary % image and returns the plate only by finding the maximum edge density
imagen4=lp4;
stats4 = regionprops(imagen4);
figure; imshow(imagen4);
imagen5 =~im2bw(imagen,0.7);
imagen5=imclearborder(imagen5,26);
imagen5 = bwareaopen(imagen5,30);
lp5=lpcropv(imagen5,U,T);% lpCrop is a function give it a binary % image and returns the plate only by finding the maximum edge density
imagen5=lp5;
stats5 = regionprops(imagen5);
figure; imshow(imagen5);
% here i want to make a set of tests to know which image from images that generated above in which its numbers are clear
% here this what i use to pass the plate image to extract the numbers
imagen=I;
figure; imshow(imagen);
stats = regionprops(imagen);
for index=1:length(stats)
if stats(index).Area >= ceil((75/250)*U) && stats(index).Area < ceil((950/250)*U) && stats(index).BoundingBox(3) < ceil((90/250)*U) && stats(index).BoundingBox(3) >ceil((5/250)*U)
x = ceil(stats(index).BoundingBox(1));
y= ceil(stats(index).BoundingBox(2));
widthX = floor(stats(index).BoundingBox(3)-1);
widthY = floor(stats(index).BoundingBox(4)-1);
if y-ceil((15/100)*T)<1
w=1;
else
w=y-ceil((15/100)*T);
end
if y+widthY+ceil((20/100)*T)>T+1
ww=T+1;
else
ww=y+widthY+ceil((20/100)*T);
end
if x-ceil((7/250)*U)<1
k=1;
else
k=x-ceil((7/250)*U);
end
if x+widthX+ceil((7/250)*U)>U+1
kk=U+1;
else
kk=x+widthX+c*eil((7/250)*U);
end
subimage(index) = {imagen(w:ww,k:kk,:)};
g=cell2mat(subimage(index));
figure; imshow(g);
end
end
toc

Answers (1)

This code is of no use without your image. It may or may not work well with your image. Even if it does work well with your image, or can be modified to work well, there is no telling whether it will work well with any other images, say for images of license plates of different sizes, orientations, lighting, and on different colored cars. For more robust algorithms that work (at least in some situations that have been published in articles), see VisionBib: http://iris.usc.edu/Vision-Notes/bibliography/motion-f726.html#License%20Plate%20Recognition,%20Extraction,%20Analysis
To upload an image, try tinypic.com or one of the others listed here: http://www.mathworks.com/matlabcentral/answers/7924-where-can-i-upload-images-and-files-for-use-on-matlab-answers Be sure to upload the image, not a screenshot, a .gz file, a tar file, a .mat file, or a .fig file. Make sure I can get to it without signing up, clicking several times, or any other annoyances.

7 Comments

*thanks for your quick reply here is 2 different images (my project is on Arabic license plates) *
and here is my function code to extract the plate in binary format (that depends on my problem the threshold):
function [lp] = lpcropv(image,t,p)
%LPCROP Crops the license plate from the candidate that covers the true
%license plate
% LP = LPCROP(image)
% where image is the image the license plate has to be extracted from
% This method outputs a smaller image that only holds the license
% plate
%detect the edges in the image
edges = edge(image,'sobel','Horizontal');
%initialize variables
record = 0;
u = 1;
o = 1;
%look for the area of 20 x 110 pixels with the largest edge density
for i=1:1:1024-t
for j=1:1:576-p
maskedimage = edges(j:j+p,i:i+t);
totaalwittepixels = sum(maskedimage(:));
if (totaalwittepixels > record)
record = totaalwittepixels;
u = j;
o = i;
else
end;
end;
end;
%make the output image
if u+p>576
k=576;
else
k=u+p;
end
if o+t>1024
kk=1024;
else
kk=o+t;
end
lp = image(u:k,o:kk);
How robust does this need to be? Do you need it just to work on this style of plate on red cars (easy), or do you need it to work on different color cars with plates of different sizes (harder)? We don't want to spend time getting it to work on this image if it's totally no good for any other images.
i want to work on any car with any color but on the same distance the distance between the car and camera will be fixed
Is your algorithm that robust? If not, then there are lots of published algorithms that are probably better. Have you researched them yet?
this is my code , i did it only with my little knowledge about image processing using matlab but i know a lot in image processing but only theories (i.e: the concept of VQ, different classifiers NN, SVM.... etc) but i know a little in matlab to implement these concepts in code , so if u have any simple algorithm i can write its code its okay if not, i want only one answer how to make the code above to detect the best threshold for my car picture thanks in advance
what is the use of " stats = regionprops(imagen); " i mean what output it returns us
regionprops returns a structure array. Each structure in the array is a structure with the members being the various measurements you asked it to make. You can turn it into a table, which you might find more convenient, with struct2table(), if you have MATLAB Release R2013b. See Steve's latest blog on the topic: http://blogs.mathworks.com/steve/2014/02/04/regionprops-tables-and-struct2table/

Sign in to comment.

Categories

Find more on Scripts in Help Center and File Exchange

Asked:

on 1 Jul 2012

Edited:

on 2 Mar 2014

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!