Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 802
Ofir A. Avatar asked Apr 24 '11 13:04

Ofir A.


2 Answers

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('http://i.stack.imgur.com/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_edgeshough_transformlines_overlayed_image_aligned

like image 96
Amro Avatar answered Oct 12 '22 13:10

Amro


In Mathematica, using Edge Detection and Hough Transform:

enter image description here

like image 40
Dr. belisarius Avatar answered Oct 12 '22 11:10

Dr. belisarius