My goal is to transfer a coordinate in perspective from a known rectangle (for example a 800 by 600 screen) to a quadrangle that is skewed/rotated. To do so i found this, which was extremely helpful: Transforming captured co-ordinates into screen co-ordinates
I guess there are more solutions to the problem. 1 by making triangles out of your quadrangle and apply some mathematical function which i could not solve yet.
OR using the H-matrix which comes out of the formula A=USVt. which seemed nice because once you have the correct H-matrix you can transfer any coordinate pretty easy as explained in the link above. So I thought I'll go for that one!
After some struggling i was able to do it in matlab (I tested the link above) and it worked :) But now in objective-C?!
I wrote the code below which starts with the A of (A=USVt). to calculate Vt which is my first next step to get to the H-matrix i should use a similar function to the matlab [U S V]=SVD(A); function. So i found here: Calling MATLAB's built-in LAPACK/BLAS routines that to do this you can include the accelerate.h framework and then are able to use the C-LAPACK equivalent dgesvd_(...);
b.t.w. i use the same coordinates as in Transforming captured co-ordinates into screen co-ordinates to make it more easy to test.
SOLVED IT
The problem i was having was the way multidimensional array A was calculated and translated into a one-dimensional array. Because Lapacks input is always one-dimensional it seems :)
I also included the way array A is calculated. WP = Webcam and SC = 800*600 screen.
float WP[4][3];
WP[0][0] = 98;
WP[0][1] = 86;
WP[0][2] = 1;
WP[1][0] = 119;
WP[1][1] = 416;
WP[1][2] = 1;
WP[2][0] = 583;
WP[2][1] = 80;
WP[2][2] = 1;
WP[3][0] = 569;
WP[3][1] = 409;
WP[3][2] = 1;
float SC[4][3];
SC[0][0] = 0;
SC[0][1] = 0;
SC[0][2] = 1;
SC[1][0] = 799;
SC[1][1] = 0;
SC[1][2] = 1;
SC[2][0] = 0;
SC[2][1] = 599;
SC[2][2] = 1;
SC[3][0] = 799;
SC[3][1] = 599;
SC[3][2] = 1;
float A[9][8];
int i=0;
int j=0;
float X[3], x, y;
for (i=0; i<4; i++) {
X[0] = WP[i][0];
X[1] = WP[i][1];
X[2] = WP[i][2];
x = SC[i][0]; y = SC[i][1];
A[0][j] = 0; A[1][j] = 0; A[2][j] = 0; A[3][j] = -X[0]; A[4][j] = -X[1];
A[5][j] = -1; A[6][j] = y*X[0]; A[7][j] = y*X[1]; A[8][j] = y;
j++;
A[0][j] = X[0]; A[1][j] = X[1]; A[2][j] = 1; A[3][j] = 0; A[4][j] = 0;
A[5][j] = 0; A[6][j] = (-x)*X[0]; A[7][j] = (-x)*X[1]; A[8][j] = (-x);
j++;
}
int m = 8;
int n = 9;
int p = (m < n ? m : n);
double B[m*n];
//Recalculate multidimentional A to one-dimentional array B for LAPACK
int k = 0;
for (i=0; i<n; i++) {
for (j=0; j<m; j++) {
B[k]=A[i][j];
// NSLog(@"%f", A[i][j]);
k++;
}
}
double U[m*m];
double VT[n*n];
double S[p * 1];
int info=0;
double work[5*m];
int lwork = 5*m;
//do the actual computation
dgesvd_("N","A", &m, &n, B, &m, S, U, &m, VT, &n, work, &lwork, &info);
for (int i=0; i<(n*n); i++) {
NSLog(@"%f", VT[i]);
}
// NEXT STEP IN MATLAB, have to still translate this to C-language
// H = transpose(reshape(V(:,end),[3 3]));
// H = H/H(3,3);
then in matlab we take the last column of Vt and reshape it in 3x3 matrix. and transpose this so it becomes H.
0.0001
0.0099
-0.8647
0.0054
-0.0003
-0.5021
-0.0000
-0.0000
0.0045
Vt_reshaped = reshape(V(:,end),[3 3]);
H = transpose(Vt_reshaped)
SOLVED!!
See Question&Answers more detail:
os