简体   繁体   English

如何对齐图像 - Matlab

[英]How to align image - Matlab

I need to know how to align an image in Matlab for further work.我需要知道如何在 Matlab 中对齐图像以进行进一步的工作。

for example I have the next license plate image and I want to recognize all the digits.例如,我有下一个车牌图像,我想识别所有数字。

在此处输入图片说明

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.任何帮助将不胜感激。

The solution was first hinted at by @AruniRC in the comments, then implemented by @belisarius in Mathematica.该解决方案首先由@AruniRC在评论中暗示,然后由@belisarius在 Mathematica 中实现。 The following is my interpretation in MATLAB.以下是我在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.思路基本相同:使用 Canny 方法检测边缘,使用 Hough 变换找到突出的线条,计算线条角度,最后执行剪切变换以对齐图像。

%# 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

In Mathematica, using Edge Detection and Hough Transform:在 Mathematica 中,使用边缘检测和霍夫变换:

在此处输入图片说明

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.如果你变换盘子或不变换盘子,识别结果应该同样好,因为通过变换,没有根据真实数字的新信息来增强图像。

如果所有图像都有这样的暗背景,您可以对图像进行二值化,将线条拟合到明亮区域的顶部或底部,并根据线条梯度计算仿射投影矩阵。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM