Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
How to extract plate from black boundaries?

Subject: How to extract plate from black boundaries?

From: Martin Skodacek

Date: 27 Feb, 2011 14:25:37

Message: 1 of 12

Hello everybody,
I have a image of a license place such like this: http://i52.tinypic.com/2uxz41k.jpg
I how how to get area of license plate, but this image is unusable for further OCR analysis.
The problem is that on each photo from camera, cars are driving different and plate is visible in different angle. So I can´t just set, that delete 2 pixel rows on top, 5 pixel rows in left, etc. I have to extract this plate more sophisticately. :)

Can anyone help me how to extract only white area of picture with text from this image?
I tries imdilate extraction and regionprops selection but without any success. I searched n it on this forum but withour success. Can someone please help/show part of code? I would be grateful. Thank you very much.
Martin

Subject: How to extract plate from black boundaries?

From: ImageAnalyst

Date: 27 Feb, 2011 14:41:35

Message: 2 of 12

On Feb 27, 9:25 am, "Martin Skodacek" <skoda...@post.cz> wrote:
> Hello everybody,
> I have a image of a license place such like this:http://i52.tinypic.com/2uxz41k.jpg
> I how how to get area of license plate, but this image is unusable for further OCR analysis.
> The problem is that on each photo from camera, cars are driving different and plate is visible in different angle. So I can´t just set, that delete 2 pixel rows on top, 5 pixel rows in left, etc. I have to extract this plate more sophisticately. :)
>
> Can anyone help me how to extract only white area of picture with text from this image?
> I tries imdilate extraction and regionprops selection but without any success. I searched n it on this forum but withour success. Can someone please help/show part of code? I would be grateful. Thank you very much.
> Martin

--------------------------------------------------------------------
YOu need to say more exactly what you mean. Otherwise all I'll say is
this

whitePixels = imageArray > 200; % Or whatever threshold you want.

areaInPixels = sum(sum(whitePixels));

Maybe you want to turn the outer black pixels to white also? If so,
then use imclearborder and some logical operations. SOmething like
(untested)
blackPixels = imageArray < 200;
noOuterBorder = imclearborder(blackPixels );
outerBorder = blackPixels -noOuterBorder;
% Set to white
imageArray(outerBorder) = 255;

Subject: How to extract plate from black boundaries?

From: Martin Skodacek

Date: 2 Mar, 2011 20:16:21

Message: 3 of 12

I don´t understand your idea. But thank ImageAnalyst anyway. I am thinking to to use BW = roicolor(Image,128,255); which gives me a a BW image and then write a cycle which defines me a position of whote plate. then I can use imcrop or other cut fcn to get the plate. Than resize and do OCR.
Martin

Does anybody have an idea how search for rectangle in cycle? thank you.
> --------------------------------------------------------------------
> YOu need to say more exactly what you mean. Otherwise all I'll say is
> this
>
> whitePixels = imageArray > 200; % Or whatever threshold you want.
>
> areaInPixels = sum(sum(whitePixels));
>
> Maybe you want to turn the outer black pixels to white also? If so,
> then use imclearborder and some logical operations. SOmething like
> (untested)
> blackPixels = imageArray < 200;
> noOuterBorder = imclearborder(blackPixels );
> outerBorder = blackPixels -noOuterBorder;
> % Set to white
> imageArray(outerBorder) = 255;

Subject: How to extract plate from black boundaries?

From: ImageAnalyst

Date: 2 Mar, 2011 21:43:58

Message: 4 of 12

Why don't you try this code and see if it does what you want. It
extracts just the letters. Letters are white and everything else is
black. It should be easy to do OCR on it then.

% IMPORTANT: The newsreader may break long lines into multiple lines.
% Be sure to join any long lines that got split into multiple single
lines.
% These can be found by the red lines on the left side of your
% text editor, which indicate syntax errors, or else just run the
% code and it will stop at the split lines with an error.

clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
fontSize = 20;

% Change the current folder to the folder of this m-file.
if(~isdeployed)
cd(fileparts(which(mfilename)));
end

% Check that user has the Image Processing Toolbox installed.
hasIPT = license('test', 'image_toolbox');
if ~hasIPT
% User does not have the toolbox installed.
message = sprintf('Sorry, but you do not seem to have the Image
Processing Toolbox.\nDo you want to try to continue anyway?');
reply = questdlg(message, 'Toolbox missing', 'Yes', 'No', 'Yes');
if strcmpi(reply, 'No')
% User said No, so exit.
return;
end
end

% Read in a standard MATLAB color demo image.
folder = 'C:\Documents and Settings\user\My Documents\Downloads\';
baseFileName = 'xxyyyy.png';
fullFileName = fullfile(folder, baseFileName);
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows columns numberOfColorBands] = size(rgbImage);
% Display the original color image.
subplot(2, 3, 1);
imshow(rgbImage, []);
title('Original color Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Position', get(0,'Screensize'));

grayImage = rgb2gray(rgbImage);
% Display the grayscale image.
subplot(2, 3, 2);
imshow(rgbImage, []);
title('Gray Scale Image', 'FontSize', fontSize);

% Let's compute and display the histogram.
[pixelCount grayLevels] = imhist(grayImage);
subplot(2, 3, 3);
bar(pixelCount);
title('Histogram of grayscale image');
xlim([0 grayLevels(end)]); % Scale x axis manually.

% Binarize the image
binaryImage = grayImage < 200;
% Display the image.
subplot(2, 3, 4);
imshow(binaryImage, []);
title('Binary Image', 'FontSize', fontSize);


binaryImage = imclearborder(binaryImage);
% Display the image.
subplot(2, 3, 5);
imshow(binaryImage, []);
title('Binary Image with no border', 'FontSize', fontSize);

Subject: How to extract plate from black boundaries?

From: Martin Skodacek

Date: 3 Mar, 2011 18:54:05

Message: 5 of 12

Yes, this is exactly what I meant, now I understand principle and can write my own implementation. Thank you.

As I test it I have som problem with few of plates. Their quality is many times bad, both when plates are dirty, borders can´t be cleaned. Some of photos i sent you on email to ImageAnalyst, I don´t want to put them online because of law for information safety :/.

I tried to use grayImage = roicolor(rgbImage,128,255); it helped but only a little bit :(.
Maybe there will be some ideas.
Thanks to everybody, specially to ImageAnalyst.
Martin

Subject: How to extract plate from black boundaries?

From: ImageAnalyst

Date: 3 Mar, 2011 19:14:18

Message: 6 of 12

On Mar 3, 1:54 pm, "Martin Skodacek" <skoda...@post.cz> wrote:
> Yes, this is exactly what I meant, now I understand principle and can write my own implementation. Thank you.
>
> As I test it I have som problem with few of plates. Their quality is many times bad, both when plates are dirty, borders can´t be cleaned. Some of photos i sent you on email to ImageAnalyst, I don´t want to put them online because of law for information safety :/.
>
> I tried to use grayImage = roicolor(rgbImage,128,255); it helped but only a little bit :(.
> Maybe there will be some ideas.
> Thanks to everybody, specially to ImageAnalyst.
> Martin

---------------------------------------------------------
Martin :
I can't take any attachments or reply from this e-mail -- it's just a
temporary spam catcher account.
Alter your images to make them not proprietary and then post them
somewhere if you want. Some sites allow you to give a password to use
before downloading. You might be able to fix them by adjusting the
threshold, and maybe using morphological processes like imerode() or
imdilate(), or just cropping to known, fixed coordinates.
-ImageAnalyst

Subject: How to extract plate from black boundaries?

From: Martin Skodacek

Date: 8 Mar, 2011 19:38:20

Message: 7 of 12

Here is a photo for illustration http://www.uschovna.cz/download/BAV9TV26KY7HPEZH/4MZ7LNP783

I understand that this needs special, very sensitive filtering.
It heeds time to playing with. Thanks for advices.
Martin

Subject: How to extract plate from black boundaries?

From: ImageAnalyst

Date: 8 Mar, 2011 20:03:24

Message: 8 of 12

Martin:
You just need to adjust the threshold. This image is not so "perfect"
as your last one. A threshold of 116 works for this one, instead of
200 like the last one. You may have to come up with a pre-
binarization step to clean up the images. Maybe try a bilateral
filter, or simply a median filter. Then maybe try some automated
threshold picking algorithm, like graythresh() or the triangle method
to pick a threshold.

http://www.mathworks.com/matlabcentral/fileexchange/28047-gray-image-thresholding-using-the-triangle-method

Subject: How to extract plate from black boundaries?

From: Martin Skodacek

Date: 14 Mar, 2011 19:06:03

Message: 9 of 12

Hello,

Triangle treshold is perfect way how to adjust the treshold. I wahs thinking whole weekend how to effectively and independently prepare plate for ocr. what am I thinking is: copy the original output plate with black boundaries (as was in attachments over), find plate using triangle_th, find position of white plate, than crop plate from the original output.

Does exisit some algorithm how to find white rectangle in black boundaries?

In some algorithm over you showed some algorithm like
--------------------------------------------------------------------
YOu need to say more exactly what you mean. Otherwise all I'll say is
this

whitePixels = imageArray > 200; % Or whatever threshold you want.

areaInPixels = sum(sum(whitePixels));

Maybe you want to turn the outer black pixels to white also? If so,
then use imclearborder and some logical operations. SOmething like
(untested)
blackPixels = imageArray < 200;
noOuterBorder = imclearborder(blackPixels );
outerBorder = blackPixels -noOuterBorder;
% Set to white
imageArray(outerBorder) = 255;

Can you help me make this running. otherwise I tested that this could be both a good way how to test it.

Thank you for your help. Martin

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <86b2a77c-6ac3-4951-b96d-739db2969de3@b13g2000prf.googlegroups.com>...
> Martin:
> You just need to adjust the threshold. This image is not so "perfect"
> as your last one. A threshold of 116 works for this one, instead of
> 200 like the last one. You may have to come up with a pre-
> binarization step to clean up the images. Maybe try a bilateral
> filter, or simply a median filter. Then maybe try some automated
> threshold picking algorithm, like graythresh() or the triangle method
> to pick a threshold.
>
> http://www.mathworks.com/matlabcentral/fileexchange/28047-gray-image-thresholding-using-the-triangle-method

Subject: How to extract plate from black boundaries?

From: ImageAnalyst

Date: 14 Mar, 2011 20:13:53

Message: 10 of 12

Martin:
I'm not sure what you're asking. Once you use some function (such as
graythresh() or that triangle thresholding metho, which is good for
skewed histograms) to determine what the threshold should be (instead
of being 200 all the time) then the rest of the code should work
fine. How can I help you?
ImageAnalyst

Subject: How to extract plate from black boundaries?

From: Martin Skodacek

Date: 17 Mar, 2011 17:44:05

Message: 11 of 12

Hello,

I understand, Triangle thesholding is fine to use. What I want to do is to use triangle tresholding to to fine the treshold, then

LPrecs = imread('Plates\Plate02.bmp');

LPrecs_filtered = medfilt2(LPrecs);
figure; imshow(LPrecs_filtered); axis off; title(' Median filter');

[lehisto x]=imhist(LPrecs_filtered);
[level]=triangle_th(lehisto,256);
I_bw=im2bw(LPrecs_filtered,level);
figure; imshow(I_bw); axis off; title('Triangle method');

then I need to delete black boundaries because using triangle I can nicely define the location of plate. So wanted the output will be just white plate with numbers.
What I need is some algorithm how to cut the white plate from LPrecs using the knowledge from triangle tresholding. Hope you understand so from image 27x148 I get image 109 (the white plate).
Thank you for your help. Martin


ImageAnalyst <imageanalyst@mailinator.com> wrote in message <37034e7b-5783-4f77-9391-3738152df96a@11g2000prf.googlegroups.com>...
> Martin:
> I'm not sure what you're asking. Once you use some function (such as
> graythresh() or that triangle thresholding metho, which is good for
> skewed histograms) to determine what the threshold should be (instead
> of being 200 all the time) then the rest of the code should work
> fine. How can I help you?
> ImageAnalyst

Subject: How to extract plate from black boundaries?

From: ImageAnalyst

Date: 17 Mar, 2011 19:24:42

Message: 12 of 12

I gave you some code that (with slight modification) should "whiten"
the black surround of the white plate. If you want to crop out the
license plate, you will have to turn it to align with the raster
lines. If you don't then you may be chopping off some of the letters
- imagine if your plate was oriented at 45 degrees - how would you
crop that. Once it's rotated, you can use imcrop(). You can use
imrotate to rotate it but first you have to figure out the angle.
There are several ways to do this, but probably the easiest is to use
the orientation you can get from regionprops. So then you should do
this
1. figure out angle from orientation property of regionprops
2. whiten the black boundary using code that I already gave you
3. shift the image so that the center of the plate is at the center of
the image.
4. call imrotate() to rotate about the center pixel.
5. figure out crop box coordinates. You may have to think about this
a few minutes (and I do mean just a few since it's very basic 10th
grade trigonometry)
6. call imcrop()
Each of these steps is fairly simple.
Good luck,
ImageAnalyst

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us