I have been trying for some time now to take an image of a pool table from an arbitrary angle, locate four coordinate pairs (x,y) of the corners, and rotate/warp the image such that the resulting image contains only the table from a bird's eye view. I am using JavaCV to accomplish this task. So far, I have written code which successfully calculates the four corners of the table as shown below (I have coded red cvCircles of radius 25 on the four corners).
After finding my four corners:
Corner 1 (Bottom Right)
X: 3234 Y: 1858
Corner 2 (Bottom Left)
X: 0 Y: 1801
Corner 3 (Top Right)
X: 2722 Y: 1069
Corner 4 (Top Left)
X: 523 Y: 1030
I attempted to use cvFindHomography and then cvWarpPerspectve to accomplish this. My code can be found below:
// Initialize Table Corners as Image Coordinates
float[] aImg = {
corners.get(0).x(), corners.get(0).y(), // BR X: 3234 Y: 1858
corners.get(1).x(), corners.get(1).y(), // BL X: 0 Y: 1801
corners.get(2).x(), corners.get(2).y(), // TR X: 2722 Y: 1069
corners.get(3).x(), corners.get(3).y() // TL X: 523 Y: 1030
};
// Initialize World Coordinates (Are these even correct? How do I determine these?)
float[] aWorld = {
0.0f, 0.0f,
0.0f, 1.0f,
1.0f, 0.0f,
1.0f,1.0f
};
// Create 3x3 Homography Matrix
CvMat homography = cvCreateMat(3, 3, opencv_core.CV_32FC1);
opencv_imgproc.cvGetPerspectiveTransform(aImg, aWorld, homography);
System.out.println(homography.toString());
// Create imgWarped and Warp Perspective
IplImage imgWarped = cvCreateImage(cvGetSize(img), 8, 3);
opencv_imgproc.cvWarpPerspective(img, imgWarped, homography, opencv_imgproc.CV_INTER_LINEAR, CvScalar.ZERO);
// Create Canvas and Display Image
CanvasFrame canvas = new CanvasFrame("Warped Image");
canvas.showImage(imgWarped);
canvas.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
The 3x3 Homography Matrix I'm obtaining is:
[ 0.0011688289, 7.928632E-4, -1.4279466
-8.698455E-5, 0.0049045905, -5.006235
-2.8205379E-5, 0.0015696458, 1.0 ]