Here is the output for the code below:
Start of test
This homography should be the 3x3 matrix taking src -> dst:
1.000000 -0.000000 10.000000
-0.000000 1.000000 10.000000
-0.000000 -0.000000 1.000000
Output point #0 is ( 52.000000, 52.000000)
Output point #1 is ( 10.000000, 10.000000)
Output point #2 is (110.000000, 10.000000)
Output point #3 is (110.000000, 110.000000)
End of test
Here is the code I used to produce that output:
printf("Start of test\n");
CvMat* src_points=0;
CvMat* dst_points=0;
CvMat* homography=0;
if (src_points == 0)
src_points = cvCreateMat( 4 /* rows */, 2 /* cols */, CV_32FC1 /* floating point, one channel */ );
if (dst_points == 0)
dst_points = cvCreateMat( 4 /* rows */, 2 /* cols */, CV_32FC1 /* floating point, one channel */ );
// this next matrix, on the other hand, does need to be 3x3
if (homography == 0)
homography = cvCreateMat( 3 /* rows */, 3 /* cols */, CV_32FC1 /* floating point, one channel */ );
// the bounds-checking version of getting and setting elements are called
// cvGetReal2D and cvSetReal2D (also available for arbitrary elements without the "Real")
// However, the calls cvmSet and cvmGet are faster because they do not do bounds checking
// and are in-line. So, if you know the bounds, they're probably the thing to use...
cvmSet( src_points, 0, 0, 0.0 );
cvmSet( src_points, 0, 1, 0.0 );
cvmSet( src_points, 1, 0, 100.0 );
cvmSet( src_points, 1, 1, 0.0 );
cvmSet( src_points, 2, 0, 0.0 );
cvmSet( src_points, 2, 1, 100.0 );
cvmSet( src_points, 3, 0, 100.0 ); // point three is (100,100)
cvmSet( src_points, 3, 1, 100.0 );
cvmSet( dst_points, 0, 0, 10.0 ); // add 10 each time
cvmSet( dst_points, 0, 1, 10.0 ); //
cvmSet( dst_points, 1, 0, 110.0 ); //
cvmSet( dst_points, 1, 1, 10.0 );
cvmSet( dst_points, 2, 0, 10.0 ); //
cvmSet( dst_points, 2, 1, 110.0 );
double cornerValue = 110.0;
cvmSet( dst_points, 3, 0, cornerValue ); // point three is (cornerValue,cornerValue)
cvmSet( dst_points, 3, 1, cornerValue );
// call the find homography function
cvFindHomography( src_points, dst_points, homography );
// print out the result
printf("This homography should be the 3x3 matrix taking src -> dst:\n");
for (int row=0 ; row<3 ; ++row) {
for (int col=0 ; col<3 ; ++col) {
printf(" %10.6lf ", cvmGet( homography, row, col ) );
}
printf("\n");
}
printf("\n");
CvMat* test_src_points=0;
CvMat* test_dst_points=0;
int NUMPOINTS = 4;
if (test_src_points == 0)
test_src_points = cvCreateMat( NUMPOINTS /* rows */, 1 /* cols */, CV_32FC2 /* floating point, two channels */ );
if (test_dst_points == 0)
test_dst_points = cvCreateMat( NUMPOINTS /* rows */, 1 /* cols */, CV_32FC2 /* floating point, two channels */ );
// let's change the points in test_src_points just to be sure things work
cvSet2D( test_src_points, 0, 0, cvScalar( 42.0, 42.0 ) );
cvSet2D( test_src_points, 1, 0, cvScalar( 0.0, 0.0 ) );
cvSet2D( test_src_points, 2, 0, cvScalar( 100.0, 0.0 ) );
cvSet2D( test_src_points, 3, 0, cvScalar( 100.0, 100.0 ) );
// try our transform function...
cvPerspectiveTransform( test_src_points, test_dst_points, homography );
// now, let's see our destination points...
for (int pointIndex = 0 ; pointIndex < NUMPOINTS ; ++pointIndex) {
CvScalar result = cvGet2D( test_dst_points, pointIndex, 0 );
printf("Output point #%d is (%10.6f, %10.6f)\n", pointIndex, result.val[0], result.val[1]);
}
printf("End of test\n");