【问题标题】:Houghlines in MATLABMATLAB 中的 Houghlines
【发布时间】:2011-01-04 23:47:00
【问题描述】:

使用Hough lines检测图像中的线条后,如何使用它来计算参考图像线条的角度变化(旋转)?

【问题讨论】:

    标签: matlab image-processing computer-vision hough-transform


    【解决方案1】:

    读者须知:这是一个后续问题,背景参考这些:


    这个过程与我之前展示的类似。下面我使用the images from your previous question(因为你只提供了一个,我通过将第一个旋转10度来创建另一个)。

    我们首先检测两个图像的线条。我们在Hough transform functions 的帮助下做到这一点。这看起来适用于两个图像:

    接下来,我们要使用线端点作为控制点来执行图像配准。首先,我们确保两幅图像中的点相互对应。我通过使用 convhull 计算凸包来做到这一点,它会自动按逆时针顺序(或者是相反的方向!)对它们进行排序。上面显示的数字表示顺序。

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

    以下是完整代码:

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

    这是提取线端点的函数:

    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
    

    结果:

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

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

    【讨论】:

    • 你好,想问一下翻译的数字是什么意思?移动的像素数?我试图放大到像素级别并计算移动的像素,但它不相符。谢谢。非常感谢您的帮助。
    • 完成图像配准部分后,我们最终得到仿射变换矩阵,从中提取旋转和平移。现在正如我最后提到的,翻译一定是在创建第二张图像的过程中发生的,因为旋转不是相对于十字符号中心的。你是否尝试将这个过程应用于你的实际图像?此外,在这种情况下,我们最终只对我们成功恢复的旋转部分感兴趣(-9.7 度,几乎 10)!!
    • 你好,我确实在我的实际图像上尝试过这个过程。由于我的图像也被旋转,因此它不在十字标志的中心,我还需要翻译它。谢谢!
    【解决方案2】:

    我不确定霍夫变换的MATLAB 实现是什么,但线的方向将与您习惯的角度成直角(90 度或 pi/2 弧度)首先确定线路。

    我希望这会有所帮助。网络上有大量关于霍夫变换的报道,维基百科是一个很好的起点。

    【讨论】:

      猜你喜欢
      • 2015-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-26
      • 2017-03-24
      • 1970-01-01
      相关资源
      最近更新 更多