2010-01-14 19 views

回答

3

注意,以飨读者:这是一个后续问题,请参阅以下背景:


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

我们从检测两张图像的行开始。我们在Houghtransformfunctions的帮助下做到这一点。这是什么样子应用到图片:

Images with detected lines and points order

接下来,我们要使用线路端点作为控制点进行图像配准。首先,我们确保两个图像中的点相互对应。我这样做是通过使用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 

与结果:

Resulting aligned image

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

旋转恢复为接近10度(有一些不可避免的错误),并且缩放实际上是1(意味着没有缩放)。请注意,在上例中有翻译组件,因为旋转不是在十字中心附近执行的)。

+0

您好, 想问一下翻译的数字是什么意思?像素数已移动?我试图放大到像素级别,并计算移动的像素,但它不符合。谢谢。非常感谢你的帮助。 – Veronica

+0

完成图像配准后,我们​​最终得到仿射变换矩阵,从中提取旋转和平移。现在正如我在最后提到的那样,翻译必须发生在创建第二个图像的过程中,因为旋转不是与十字标记中心相关的。您是否尝试将此过程应用于您的实际图像?此外,在这种情况下,我们最终只对我们成功恢复的旋转部分感兴趣(-9.7度,几乎10)! – Amro

+0

你好,我确实在我的实际图片上尝试了这个过程。由于我的图像也是旋转的,因此它不在十字标记的中心,我还需要翻译它。谢谢! – Veronica

0

我不确定什么MATLAB Hough变换的实现是,但线的方向将简单地与您用来识别的角度成直角(90度或pi/2弧度)该行在第一位。

我希望有帮助。在网络上有很好的霍夫变换报道,维基百科是一个很好的开始。