![]() |
MATLAB中的Houghlines
使用[URL="http://en.wikipedia.org/wiki/Hough_transform"]霍夫线[/URL]检测图像中的[URL="http://en.wikipedia.org/wiki/Hough_transform"]线后[/URL] ,如何使用它来计算参考图像线的角度(旋转)变化?
[B]回答:[/B] [B]读者注意:[/B]这是一个后续问题,请参考以下内容: [LIST][*] [URL="https://stackoverflow.com/questions/1975206/how-to-select-maximum-intensity-in-hough-transform-in-matlab/1975261#1975261"]如何在MATLAB中的霍夫变换中选择最大强度?[/URL][*] [URL="https://stackoverflow.com/questions/2062760/calculating-displacement-moved-in-matlab"]计算在MATLAB中移动的位移[/URL][/LIST]该过程类似于我之前展示的过程。在下面,我使用的[URL="http://i182.photobucket.com/albums/x11/veronicafmy/FYP/pict1.jpg"]是上一个问题的图像[/URL] (由于您只提供了一个,所以我将第一个旋转10度来创建另一个图像)。 我们首先检测两个图像的线条。我们借助[URL="http://www.mathworks.com/help/images/ref/hough.html"]Hough[/URL] [URL="http://www.mathworks.com/help/images/ref/houghlines.html"]变换[/URL] [URL="http://www.mathworks.com/help/images/ref/houghpeaks.html"]功能来完成此操作[/URL] 。这看起来适用于两个图像: [IMG]https://i.stack.imgur.com/cINnK.png[/IMG] 接下来,我们要使用线端点作为控制点执行图像配准。首先,我们确保两个图像中的点彼此对应。我通过使用[B][URL="http://www.mathworks.com/help/matlab/ref/convhull.html"]convhull[/URL][/B]计算凸包来做到这一点, [B][URL="http://www.mathworks.com/help/matlab/ref/convhull.html"]convhull[/URL][/B]自动将它们按逆时针顺序排序(或者是相反方向!)。上面显示的数字指示顺序。 最后,我们使用[B][URL="http://www.mathworks.com/help/images/ref/cp2tform.html"]cp2tform[/URL][/B]函数获取转换矩阵,该矩阵用于对齐图像并提取平移,旋转和缩放。 以下是完整的代码: %% # Step 1: read and prepare images %# (since you provided only one, I created the other by rotating the first). I1 = imread('[url]http://i.stack.imgur.com/Se6zX.jpg');[/url] I1 = rgb2gray( imcrop(I1, [85 35 445 345]) ); %# Get rid of white border I2 = imrotate(I1, -10, 'bilinear', 'crop'); %# Create 2nd by rotating 10 degrees %% # Step 2: detect the cross sign endpoints (sorted in same order) p1 = getCross(I1); p2 = getCross(I2); %% # Step 3: perform Image Registration %# Find transformation that maps I2 to I1 using the 4 control points for each t = cp2tform(p2,p1,'affine'); %# Transform I2 to be aligned with I1 II2 = imtransform(I2, t, 'XData',[1 size(I1,2)], 'YData',[1 size(I1,1)]); %# Plot figure('menu','none') subplot(131), imshow(I1), title('I1') subplot(132), imshow(I2), title('I2') subplot(133), imshow(II2), title('I2 (aligned)') %# Recover affine transformation params (translation, rotation, scale) ss = t.tdata.Tinv(2,1); sc = t.tdata.Tinv(1,1); tx = t.tdata.Tinv(3,1); ty = t.tdata.Tinv(3,2); scale = sqrt(ss*ss + sc*sc) rotation = atan2(ss,sc)*180/pi translation = [tx ty] 这是提取线端点的函数: function points = getCross(I) %# Get edges (simply by thresholding) I = imfilter(I, fspecial('gaussian', [7 7], 1), 'symmetric'); BW = imclearborder(~im2bw(I, 0.5)); %# Hough transform [H,T,R] = hough(BW); %# Detect peaks P = houghpeaks(H, 2); %# Detect lines lines = houghlines(BW, T, R, P); %# Sort 2D points in counterclockwise order points = [vertcat(lines.point1); vertcat(lines.point2)]; idx = convhull(points(:,1), points(:,2)); points = points(idx(1:end-1),:); end 结果: [IMG]https://i.stack.imgur.com/PoT9N.png[/IMG] scale = 1.0025 rotation = -9.7041 translation = 32.5270 -38.5021 旋转几乎恢复了10度(有一些不可避免的误差),缩放比例实际上是1(表示没有缩放)。请注意,在上面的示例中有一个平移组件,因为未围绕十字符号的中心执行旋转。 [url=https://stackoverflow.com/questions/2062826]更多&回答...[/url] |
所有时间均为北京时间。现在的时间是 05:08。 |
Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.