我已经尝试了一段时间从任意角度拍摄台球桌的图像,找到角落的四个坐标对(x,y),并旋转/扭曲图像,使得结果图像仅包含从鸟瞰的桌子。我正在使用JavaCV来完成这项任务。到目前为止,我已经编写了成功计算表格四角的代码,如下所示(我在四个角上编码了半径为25的红色cvCircles)。
找到我的四个角落后:
角1(右下)
X:3234 Y:1858
角2(左下)
X:0 Y:1801
角3(右上)
X:2722 Y:1069
角4(左上角)
X:523 Y:1030
// 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);
我得到的3x3同形矩阵是:
[0.0011688289,7.928632E-4,-1.4279466
-8.698455E-5,0.0049045905,-5.006235
-2.8205379E-5,0.0015696458,1.0]