2010-01-14 20 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弧度)該行在第一位。

我希望有幫助。在網絡上有很好的霍夫變換報道,維基百科是一個很好的開始。