Ofir A.
Ofir A.

Reputation: 3162

How to align image - Matlab

I need to know how to align an image in Matlab for further work.

for example I have the next license plate image and I want to recognize all the digits.

enter image description here

my program works for straight images so, I need to align the image and then preform the optical recognition system.

The method should be as much as universal that fits for all kinds of plates and in all kinds of angles.

EDIT: I tried to do this with Hough Transform but I didn't Succeed. anybody can help me do to this?

any help will be greatly appreciated.

Upvotes: 12

Views: 9660

Answers (4)

Amro
Amro

Reputation: 124563

The solution was first hinted at by @AruniRC in the comments, then implemented by @belisarius in Mathematica. The following is my interpretation in MATLAB.

The idea is basically the same: detect edges using Canny method, find prominent lines using Hough Transform, compute line angles, finally perform a Shearing Transform to align the image.

%# read and crop image
I = imread('https://i.sstatic.net/CJHaA.png');
I = I(:,1:end-3,:);     %# remove small white band on the side

%# egde detection
BW = edge(rgb2gray(I), 'canny');

%# hough transform
[H T R] = hough(BW);
P  = houghpeaks(H, 4, 'threshold',ceil(0.75*max(H(:))));
lines = houghlines(BW, T, R, P);

%# shearing transforma
slopes = vertcat(lines.point2) - vertcat(lines.point1);
slopes = slopes(:,2) ./ slopes(:,1);
TFORM = maketform('affine', [1 -slopes(1) 0 ; 0 1 0 ; 0 0 1]);
II = imtransform(I, TFORM);

Now lets see the results

%# show edges
figure, imshow(BW)

%# show accumlation matrix and peaks
figure, imshow(imadjust(mat2gray(H)), [], 'XData',T, 'YData',R, 'InitialMagnification','fit')
xlabel('\theta (degrees)'), ylabel('\rho'), colormap(hot), colorbar
hold on, plot(T(P(:,2)), R(P(:,1)), 'gs', 'LineWidth',2), hold off
axis on, axis normal

%# show image with lines overlayed, and the aligned/rotated image
figure
subplot(121), imshow(I), hold on
for k = 1:length(lines)
    xy = [lines(k).point1; lines(k).point2];
    plot(xy(:,1), xy(:,2), 'g.-', 'LineWidth',2);
end, hold off
subplot(122), imshow(II)

canny_edges hough_transform lines_overlayed_image_aligned

Upvotes: 15

Dr. belisarius
Dr. belisarius

Reputation: 61016

In Mathematica, using Edge Detection and Hough Transform:

enter image description here

Upvotes: 6

Niki
Niki

Reputation: 15867

If all the images have a dark background like that one, you could binarize the image, fit lines to the top or bottom of the bright area and calculate an affine projection matrix from the line gradient.

Upvotes: 0

user492238
user492238

Reputation: 4084

If you are using some kind of machine learning toolbox for text recognition, try to learn from ALL plates - not only aligned ones. Recognition results should be equally well if you transform the plate or dont, since by transforming, no new informations according to the true number will enhance the image.

Upvotes: 3

Related Questions