【问题标题】:How to straight an image by selecting four points in an Image?如何通过选择图像中的四个点来拉直图像?
【发布时间】:2016-07-03 11:04:22
【问题描述】:

我想通过使用 java 中 opencv 的 getPerspectiveTransform() 方法选择图像中的四个点来拉直图像。我知道可以在 python 中使用 opencv 来完成: getPerspectiveTransform。如果有人用它来实现图像拉直..请帮助。

【问题讨论】:

标签: java image opencv image-processing


【解决方案1】:

有透视变换可用于实现四边形到四边形的转换。在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());

你会得到变换的拉直图像。

我已经在这张图片上测试了这段代码。

结果图像是

【讨论】:

    【解决方案2】:

    试试这个:

    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-26
      • 2017-05-01
      • 1970-01-01
      相关资源
      最近更新 更多