2015-10-05 41 views
1

最近我一直試圖通過從4個檢測到的4個真值2D點生成單應矩陣3x3矩陣來完成我的項目的一個階段。 我已經嘗試了幾種不同的算法和幾種不同的SVD實現,仍然無法獲得好的結果。Homography算法不能按預期工作

我已經採取了Openframework's homography implementation(其中IIRC取自opencv)並且越來越接近...但仍然不正確的結果。我敢肯定,所有的矩陣用法都是正確的,但讓我弄亂了某處(甚至可能是最終的變換?)

這裏是我想匹配的點的圖像,src(matches)在左邊,dst(真相)在右邊。 (如果需要,我可以得到座標,但圖像大小約爲640x1000)。最右邊(白色着色)是轉換到dst/truth上的匹配,並且測試代碼中使用了相同的單應性使圖像變形。 enter image description here

注意:忽略opencl類型,這些都是正確使用的(爲了簡潔起見,這裏只是爲了簡潔起見)。 std :: Debug只是一個ostream。這是一個16 float數組,但我只是使用第一9

void gaussian_elimination(float *input, int n) 
 
{ 
 
\t // ported to c from pseudocode in 
 
\t // http://en.wikipedia.org/wiki/Gaussian_elimination 
 
\t 
 
\t float * A = input; 
 
\t int i = 0; 
 
\t int j = 0; 
 
\t int m = n-1; 
 
\t while (i < m && j < n){ 
 
\t \t // Find pivot in column j, starting in row i: 
 
\t \t int maxi = i; 
 
\t \t for(int k = i+1; k<m; k++){ 
 
\t \t \t if(fabs(A[k*n+j]) > fabs(A[maxi*n+j])){ 
 
\t \t \t \t maxi = k; 
 
\t \t \t } 
 
\t \t } 
 
\t \t if (A[maxi*n+j] != 0){ 
 
\t \t \t //swap rows i and maxi, but do not change the value of i 
 
\t \t \t if(i!=maxi) 
 
\t \t \t \t for(int k=0;k<n;k++){ 
 
\t \t \t \t \t float aux = A[i*n+k]; 
 
\t \t \t \t \t A[i*n+k]=A[maxi*n+k]; 
 
\t \t \t \t \t A[maxi*n+k]=aux; 
 
\t \t \t \t } 
 
\t \t \t //Now A[i,j] will contain the old value of A[maxi,j]. 
 
\t \t \t //divide each entry in row i by A[i,j] 
 
\t \t \t float A_ij=A[i*n+j]; 
 
\t \t \t for(int k=0;k<n;k++){ 
 
\t \t \t \t A[i*n+k]/=A_ij; 
 
\t \t \t } 
 
\t \t \t //Now A[i,j] will have the value 1. 
 
\t \t \t for(int u = i+1; u< m; u++){ 
 
\t \t \t \t //subtract A[u,j] * row i from row u 
 
\t \t \t \t float A_uj = A[u*n+j]; 
 
\t \t \t \t for(int k=0;k<n;k++){ 
 
\t \t \t \t \t A[u*n+k]-=A_uj*A[i*n+k]; 
 
\t \t \t \t } 
 
\t \t \t \t //Now A[u,j] will be 0, since A[u,j] - A[i,j] * A[u,j] = A[u,j] - 1 * A[u,j] = 0. 
 
\t \t \t } 
 
\t \t \t 
 
\t \t \t i++; 
 
\t \t } 
 
\t \t j++; 
 
\t } 
 
\t 
 
\t //back substitution 
 
\t for(int i=m-2;i>=0;i--){ 
 
\t \t for(int j=i+1;j<n-1;j++){ 
 
\t \t \t A[i*n+m]-=A[i*n+j]*A[j*n+m]; 
 
\t \t \t //A[i*n+j]=0; 
 
\t \t } 
 
\t } 
 
} 
 
            
 
            
 
            
 
cl_float16 of_findHomography(cl_float2 src[4], cl_float2 dst[4]) 
 
{ 
 
\t // create the equation system to be solved 
 
\t // 
 
\t // from: Multiple View Geometry in Computer Vision 2ed 
 
\t //  Hartley R. and Zisserman A. 
 
\t // 
 
\t // x' = xH 
 
\t // where H is the homography: a 3 by 3 matrix 
 
\t // that transformed to inhomogeneous coordinates for each point 
 
\t // gives the following equations for each point: 
 
\t // 
 
\t // x' * (h31*x + h32*y + h33) = h11*x + h12*y + h13 
 
\t // y' * (h31*x + h32*y + h33) = h21*x + h22*y + h23 
 
\t // 
 
\t // as the homography is scale independent we can let h33 be 1 (indeed any of the terms) 
 
\t // so for 4 points we have 8 equations for 8 terms to solve: h11 - h32 
 
\t // after ordering the terms it gives the following matrix 
 
\t // that can be solved with gaussian elimination: 
 
\t 
 
\t float P[8][9]={ 
 
\t \t {-src[0][0], -src[0][1], -1, 0, 0, 0, src[0][0]*dst[0][0], src[0][1]*dst[0][0], -dst[0][0] }, // h11 
 
\t \t { 0, 0, 0, -src[0][0], -src[0][1], -1, src[0][0]*dst[0][1], src[0][1]*dst[0][1], -dst[0][1] }, // h12 
 
\t \t 
 
\t \t {-src[1][0], -src[1][1], -1, 0, 0, 0, src[1][0]*dst[1][0], src[1][1]*dst[1][0], -dst[1][0] }, // h13 
 
\t \t { 0, 0, 0, -src[1][0], -src[1][1], -1, src[1][0]*dst[1][1], src[1][1]*dst[1][1], -dst[1][1] }, // h21 
 
\t \t 
 
\t \t {-src[2][0], -src[2][1], -1, 0, 0, 0, src[2][0]*dst[2][0], src[2][1]*dst[2][0], -dst[2][0] }, // h22 
 
\t \t { 0, 0, 0, -src[2][0], -src[2][1], -1, src[2][0]*dst[2][1], src[2][1]*dst[2][1], -dst[2][1] }, // h23 
 
\t \t 
 
\t \t {-src[3][0], -src[3][1], -1, 0, 0, 0, src[3][0]*dst[3][0], src[3][1]*dst[3][0], -dst[3][0] }, // h31 
 
\t \t { 0, 0, 0, -src[3][0], -src[3][1], -1, src[3][0]*dst[3][1], src[3][1]*dst[3][1], -dst[3][1] }, // h32 
 
\t }; 
 
\t 
 
\t gaussian_elimination(&P[0][0],9); 
 
/* 
 
\t // gaussian elimination gives the results of the equation system 
 
\t // in the last column of the original matrix. 
 
\t // opengl needs the transposed 4x4 matrix: 
 
\t float aux_H[]= 
 
\t { 
 
\t \t P[0][8],P[3][8],0,P[6][8], // h11 h21 0 h31 
 
\t \t P[1][8],P[4][8],0,P[7][8], // h12 h22 0 h32 
 
\t \t 0  ,  0,0,0,  // 0 0 0 0 
 
\t \t P[2][8],P[5][8],0,1 // h13 h23 0 h33 
 
\t }; 
 
*/ 
 
\t // \t non transposed 3x3 
 
\t cl_float16 Result; 
 
\t Result.s[0] = P[0][8]; 
 
\t Result.s[1] = P[1][8]; 
 
\t Result.s[2] = P[2][8]; 
 
\t 
 
\t Result.s[3] = P[3][8]; 
 
\t Result.s[4] = P[4][8]; 
 
\t Result.s[5] = P[5][8]; 
 
\t 
 
\t Result.s[6] = P[6][8]; 
 
\t Result.s[7] = P[7][8]; 
 
\t Result.s[8] = 1; 
 
\t //Result.s[8] = P[8][8]; 
 

 
\t 
 
\t // \t test 
 
\t for (int i=0; \t i<4; \t i++) 
 
\t { 
 
\t \t auto H = Result.s; 
 
\t \t float x = H[0]*src[i][0] + H[1]*src[i][1] + H[2]; 
 
\t \t float y = H[3]*src[i][0] + H[4]*src[i][1] + H[5]; 
 
\t \t float z = H[6]*src[i][0] + H[7]*src[i][1] + H[8]; 
 
\t \t 
 
\t \t x /= z; 
 
\t \t y /= z; 
 
\t \t 
 
\t \t float diffx = dst[i][0] - x; 
 
\t \t float diffy = dst[i][1] - y; 
 
\t \t std::Debug << "err src->dst #" << i << ": " << diffx << "," << diffy << std::endl; 
 
\t } 
 
\t for (int i=0; \t i<4; \t i++) 
 
\t { 
 
\t \t auto H = Result.s; 
 
\t \t float x = H[0]*dst[i][0] + H[1]*dst[i][1] + H[2]; 
 
\t \t float y = H[3]*dst[i][0] + H[4]*dst[i][1] + H[5]; 
 
\t \t float z = H[6]*dst[i][0] + H[7]*dst[i][1] + H[8]; 
 
\t \t 
 
\t \t x /= z; 
 
\t \t y /= z; 
 
\t \t 
 
\t \t float diffx = src[i][0] - x; 
 
\t \t float diffy = src[i][1] - y; 
 
\t \t std::Debug << "err src->dst #" << i << ": " << diffx << "," << diffy << std::endl; 
 
\t } 
 

 
\t 
 
\t return Result; 
 
}

從圖像輸出

ERR SRC-> DST#0:0.00195, 0.0132

ERR SRC-> DST#1:0,6.1e-05

ERR SRC-> DST#2:-0.00161,-8.9圖6e-05

ERR SRC-> DST#3:1.91e-06,0.000122

ERR dst-> SRC#0:2.31e + 03551

ERR dst-> SRC#1: - 3.34e + 03,-4.23e + 03

ERR dst-> SRC#2:1.07E + 03,1.25e + 04

ERR dst-> SRC#3:456771

m有什麼明顯的錯誤嗎? y矩陣變換代碼? 或者我將SVD結果放入矩陣中的行/列號錯誤? 也許整個算法不是我所需要的? (當然它應該會產生一個相當簡單,小巧的旋轉矩陣嗎?)

+0

如何使用cv :: findHomography? – Miki

+0

我試圖避免完全使用opencv爲一個單一的算法,這將是跨平臺,我也需要速度,所以要避免轉換到/從cv :: mat,並且這將進入opencl&gles計算內核(此代碼已經在內核中運行...只是不正​​確:) –

+0

我的下一個測試步驟將通過opencv運行一切,並通過 –

回答

0

我和往常一樣糟糕。這個實現似乎完美(迄今爲止),並且對於GPU並行處理來說非常棒。

我是在糟糕的座標餵養。渲染的點與輸入算法的點不同(新代碼vs舊代碼)。

未來的警告總是先生成數據,然後渲染,而不是渲染&用[曾經]相同的代碼生成。

+0

來調試差異/結果編輯:while循環對GPU不好 –