MATLAB 中的粗糙线

发布于 2024-08-17 15:25:43 字数 133 浏览 7 评论 0原文

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青春有你 2024-08-24 15:25:43

读者注意事项:这是一个后续问题,请参阅以下内容了解背景:


该过程与我之前展示的类似。下面我使用您上一个问题中的图像(因为你只提供了一个,我通过将第一个旋转 10 度创建了另一个)。

我们首先检测两个图像的线条。我们在 Hough 变换 函数。这看起来就像应用于两个图像:

具有检测到的线和点顺序的图像

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

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

以下是完整的代码:

%% # Step 1: read and prepare images
%# (since you provided only one, I created the other by rotating the first).
I1 = imread('https://i.sstatic.net/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(意味着没有缩放)。请注意,上面的示例中有一个平移组件,因为没有围绕十字标志的中心执行旋转。

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:

Images with detected lines and points order

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('https://i.sstatic.net/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:

Resulting aligned image

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).

小耗子 2024-08-24 15:25:43

我不确定霍夫变换的 MATLAB 实现是什么,但线的方向将简单地与您最初用来识别直线的角度成直角(90 度或 pi/2 弧度)。

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

I am not sure what the MATLAB implementation of the Hough transform is, but the orientation of the line will be simply be at a right angle (90 degrees or pi/2 radians) to the angle you've used to identify the line in the first place.

I hope that helps. There's decent coverage of Hough transforms on the web and Wikipedia is a good place to start.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文