2011-09-19 20 views
4

我有兩個圖像。一個是原始的,另一個是旋轉的。如何在MATLAB中測量圖像的旋轉?

Original Image

現在,我需要發現,圖像旋轉的角度。到現在爲止,我想過要發現每種顏色的質心(因爲我將使用的每個圖像都有顏色的正方形),並用它來發現圖像旋轉了多少,但是我失敗了。

我使用這個發現的重心和圖像在高廣場顏色:

i = rgb2gray(img); 
bw = im2bw(i,0.01); 
s = regionprops(bw,'Centroid'); 
centroids = cat(1, s.Centroid); 
colors = impixel(img,centroids(1),centroids(2)); 
top = max(centroids); 
topcolor = impixel(img,top(1),top(2)); 
+1

你有沒有想過使用普魯克分析?MATLAB提供'procrustes'函數,維基百科對這個方法有很好的寫法。 –

回答

3

您可以使用CP2TFORM函數檢測圖像和旋轉版本中的一個彩色矩形的角點,並將這些角點用作控制點來推斷兩個圖像之間的轉換(如在圖像配準中)。然後,我們可以從仿射變換矩陣計算旋轉角度:

下面是一個例子代碼:

%# read first image (indexed color image) 
[I1 map1] = imread('http://i.stack.imgur.com/LwuW3.png'); 

%# constructed rotated image 
deg = -15; 
I2 = imrotate(I1, deg, 'bilinear', 'crop'); 

%# find blue rectangle 
BW1 = (I1==2); 
BW2 = imrotate(BW1, deg, 'bilinear', 'crop'); 

%# detect corners in both 
p1 = corner(BW1, 'QualityLevel',0.5); 
p2 = corner(BW2, 'QualityLevel',0.5); 

%# sort corners coordinates in a consistent way (counter-clockwise) 
p1 = sortrows(p1,[2 1]); 
p2 = sortrows(p2,[2 1]); 
idx = convhull(p1(:,1), p1(:,2)); p1 = p1(idx(1:end-1),:); 
idx = convhull(p2(:,1), p2(:,2)); p2 = p2(idx(1:end-1),:); 

%# make sure we have the same number of corner points 
sz = min(size(p1,1),size(p2,1)); 
p1 = p1(1:sz,:); p2 = p2(1:sz,:); 

%# infer transformation from corner points 
t = cp2tform(p2,p1,'nonreflective similarity'); %# 'affine' 

%# rotate image to match the other 
II2 = imtransform(I2, t, 'XData',[1 size(I1,2)], 'YData',[1 size(I1,1)]); 

%# 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); 
translation = [tx ty]; 
scale = sqrt(ss*ss + sc*sc); 
rotation = atan2(ss,sc)*180/pi; 

%# plot the results 
subplot(311), imshow(I1,map1), title('I1') 
hold on, plot(p1(:,1),p1(:,2),'go') 
subplot(312), imshow(I2,map1), title('I2') 
hold on, plot(p2(:,1),p2(:,2),'go') 
subplot(313), imshow(II2,map1) 
title(sprintf('recovered angle = %g',rotation)) 

screenshot

1

如果你能確定只對應一個組件更容易爲顏色:

  1. 計算每個圖像的質心
  2. 計算每個圖像的質心(x和y)的均值。這是每個圖像的「中心」
  3. 獲取每個圖像
  4. 紅色分量色心(在你的例子)減去重心的平均值從紅色分量色心每幅圖像的每個圖像
  5. 計算4)中計算的每個矢量的ArcTan2,並減去角度。那是你的結果。

如果每個顏色都有多個圖形,則需要計算所有可能的旋轉組合,然後選擇與其他可能旋轉相兼容的圖形。

我可以張貼在Mathematica的代碼,如果你認爲它是有用的。

1

我會採取一個變種上面提到的方法:

% Crude binarization method to knock out background and retain foreground 
% features. Note one looses the cube in the middle 
im = im > 1 

Enter image description here

然後我會得到二維自相關:

acf = normxcorr2(im, im); 

Enter image description here

從這個結果,人們可以很容易地檢測到峯,a隨着旋轉進入自相關函數(ACF)域,人們可以通過匹配來自旋轉圖像的原始ACF和ACF之間的峯值來確定旋轉,例如使用所謂的Hungarian algorithm