MATLAB 中的 Houghlines [英] Houghlines in MATLAB

查看:12
本文介绍了MATLAB 中的 Houghlines的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 自动按逆时针顺序(或者是相反的方向!)对它们进行排序.上面显示的数字表示顺序.

最后,我们使用函数cp2tform 得到变换矩阵,我们用它来对齐图像并提取平移、旋转和缩放.

以下为完整代码:

%% # 第一步:读取并准备图片%#(因为你只提供了一个,我通过旋转第一个创建了另一个).I1 = imread('http://i.stack.imgur.com/Se6zX.jpg');I1 = rgb2gray( imcrop(I1, [85 35 445 345]) );%# 去掉白色边框I2 = imrotate(I1, -10, '双线性', 'crop');%# 旋转 10 度创建第二个%% # Step 2:检测交叉符号端点(以相同的顺序排序)p1 = getCross(I1);p2 = getCross(I2);%% # 第三步:进行图像配准%# 使用每个控制点的 4 个控制点查找将 I2 映射到 I1 的转换t = cp2tform(p2,p1,'仿射');%# 将 I2 转换为与 I1 对齐II2 = imtransform(I2, t, 'XData',[1 size(I1,2)], 'YData',[1 size(I1,1)]);%# 阴谋图(菜单",无")子图(131),imshow(I1),标题('I1')子图(132),imshow(I2),标题('I2')子图(133),imshow(II2),标题('I2(对齐)')%# 恢复仿射变换参数(平移、旋转、缩放)ss = t.tdata.Tinv(2,1);sc = t.tdata.Tinv(1,1);tx = t.tdata.Tinv(3,1);ty = t.tdata.Tinv(3,2);比例 = sqrt(ss*ss + sc*sc)旋转 = atan2(ss,sc)*180/pi翻译 = [tx ty]

这是提取线端点的函数:

功能点 = getCross(I)%# 获取边缘(只需通过阈值处理)I = imfilter(I, fspecial('高斯', [7 7], 1), '对称');BW = imclearborder(~im2bw(I, 0.5));%# 霍夫变换[H,T,R] = 霍夫(BW);%# 检测峰值P = houghpeaks(H, 2);%# 检测线线= houghlines(BW,T,R,P);%# 逆时针排序二维点点 = [vertcat(lines.point1);vertcat(lines.point2)];idx = convhull(points(:,1), points(:,2));点=点(idx(1:end-1),:);结尾

结果:

比例=1.0025旋转 =-9.7041翻译 =32.5270 -38.5021

旋转恢复为几乎 10 度(有一些不可避免的错误),缩放实际上是 1(意味着没有缩放).请注意,上例中有一个平移分量,因为没有围绕十字符号的中心进行旋转).

After detecting the lines in an image using Hough lines, how can I use it to calculate the change in angle (rotation) of the lines of a reference image?

解决方案

Note to readers: This is a follow-up question, refer to these for background:


The process is similar to what I showed before. Below I am using the images from your previous question (since you provided only one, I created the other by rotating the first by 10 degrees).

We start by detecting the lines for the two images. We do this with the help of the Hough transform functions. This what it looks like applied to both images:

Next, we want to perform image registration using the line endpoints as control-points. First, we make sure the points correspond to each other in the two images. I do this by computing the convex hull using convhull which automatically sorts them in counterclockwise-order (or is it in the opposite direction!). The numbers shown above indicate the order.

Finally, we use the function cp2tform to get the transformation matrix, which we use to align the images and extract the translation, rotation, and scaling.

The following is the complete code:

%% # Step 1: read and prepare images
%# (since you provided only one, I created the other by rotating the first).
I1 = imread('http://i.stack.imgur.com/Se6zX.jpg');
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]

And here's the function that extract the lines endpoints:

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

with the result:

scale =
    1.0025
rotation =
   -9.7041
translation =
   32.5270  -38.5021

The rotation is recovered as almost 10 degrees (with some inevitable error), and scaling is effectively 1 (meaning there was no zooming). Note that there was a translation component in the above example, because rotation was not performed around the center of the cross sign).

这篇关于MATLAB 中的 Houghlines的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆