我想通过在java中使用opencv的 getPerspectiveTransform()方法在图像中选择四个点来直接拍摄图像。我知道可以在python中使用opencv来完成它: getPerspectiveTransform。如果有人用它来实现图像拉直..请帮助。
答案 0 :(得分:4)
有透视变换可用于实现四到四转换。在 OpenCV 中,主要有两种方法可以实现getPerspectiveTransformation()和warpPerspective()方法。
以下是实现图像矫直的示例代码:
首先在源图像中获得四个四角线点
Mat srcImage = Imgcodecs.imread("input.png");
Mat destImage = new Mat(500, 700, srcImage.type());
Mat src = new MatOfPoint2f(new Point(x1, y1), new Point(x2, y2), new Point(x3, y3), new Point(x4, y4));
Mat dst = new MatOfPoint2f(new Point(0, 0), new Point(destImage.width() - 1, 0), new Point(destImage.width() - 1, destImage.height() - 1), new Point(0, destImage.height() - 1));
获得转型矩阵
Mat transform = Imgproc.getPerspectiveTransform(src, dst);
Imgproc.warpPerspective(srcImage, destImage, transform, destImage.size());
你会变得挺直的形象。
答案 1 :(得分:0)
试试这个:
Mat mat=Highgui.imread("inputImage.jpg");
Mat src_mat=new Mat(4,1,CvType.CV_32FC2);
Mat dst_mat=new Mat(4,1,CvType.CV_32FC2);
// Keypoint location on source image
src_mat.put(0,0,407.0,74.0,1606.0,74.0,420.0,2589.0,1698.0,2589.0);
// Desired keypoint location after straigting
dst_mat.put(0,0,0.0,0.0,1600.0,0.0, 0.0,2500.0,1600.0,2500.0);
Mat perspectiveTransform=Imgproc.getPerspectiveTransform(src_mat, dst_mat);
Mat dst=mat.clone();
Imgproc.warpPerspective(mat, dst, perspectiveTransform, new Size(1600,2500));
Highgui.imwrite("resultImage.jpg", dst);
原帖: Android OpenCV getPerspectiveTransform and warpPerspective