【问题标题】:OpenCV: What are the parameters for the WarpPerspective function?OpenCV:WarpPerspective 函数的参数是什么?
【发布时间】:2014-11-04 04:02:57
【问题描述】:

我正在用 Java 构建一个使用 OpenCV 的应用程序。我之前没用过这个库,Java 的实现有点缺乏文档。我需要撤消透视扭曲图像以使其平整。我需要将梯形转换为矩形。基本上我需要拉伸两条平行边中较短的一条以匹配较长平行边的长度。我知道我需要计算单应性并使用 WarpPerspective 函数,但我不知道如何构造这个命令。我知道什么是单应性,但我不知道如何声明单应性,也不知道要向 warpPerspective 提供什么。让我强调一下,我理解理论,我只需要学习代码中的文本语法。

为了举例,假设我的源梯形在 (0,20)、(0,80)、(200,0) 和 (200,100) 处有角。然后我想以矩形“拉伸”结束(0,0),(0,100),(200,0)和(200,200)。谁能提供代码示例?图片为 Mat 形式。

【问题讨论】:

    标签: java opencv image-processing homography


    【解决方案1】:

    Source Image

    Destination Image

    java代码

    corners.add(new Point(215,90));
    corners.add(new Point(470,90));
    corners.add(new Point(540,240));
    corners.add(new Point(150,240));
    double maxWidth = 540 - 150 ;//BottomRight.x - BottomLeft.x
    double maxHeight = 240 - 90 ; //BottomRight.y - TopLeft.y
    
    target.add(new Point(0,0));
    target.add(new Point(maxWidth-1,0));
    target.add(new Point(0,maxHeight-1));
    target.add(new Point(maxWidth-1,maxHeight-1));
    trans=Imgproc.getPerspectiveTransform(Converters.vector_Point2f_to_Mat(corners), Converters.vector_Point2f_to_Mat(target));
    
    Imgproc.warpPerspective(img, proj, trans, new Size(maxWidth,maxHeight));
    
    orignal.updateFrame(Utils.fromMatToBufferedImage(img));
    

    proj 是目标 Mat

    【讨论】:

      【解决方案2】:

      我不知道我是否可以在 Java 中为您提供帮助,但在 C ++ 中,解决方案非常简单:

      // Matrix for getPerspectiveTransform()
      Mat H( 2, 4, CV_32FC1 );
      //Input and Output Image;
      Mat input, output;
      input = imread( "example.jpg", 1 );
      
      std::vector<cv::Point2f> trapezoid;
      std::vector<cv::Point2f> rectangular;
      
      trapezoid.push_back(cv::Point2f(0,20));
      trapezoid.push_back(cv::Point2f(0,80));
      trapezoid.push_back(cv::Point2f(200,0));
      trapezoid.push_back(cv::Point2f(200,100));
      
      rectangular.push_back(cv::Point2f(0,0));
      rectangular.push_back(cv::Point2f(0,100));
      rectangular.push_back(cv::Point2f(0,200));
      rectangular.push_back(cv::Point2f(200,200));
      
      H = getPerspectiveTransform( trapezoid, rectangular );
      
      warpPerspective( input, output, H,output.size() );
      

      在 java 中,您可以看到这两个链接,我认为它们完美地回答了您关于 warpPerspective 函数的所有问题:

      http://docs.oracle.com/cd/E17802_01/products/products/java-media/jai/forDevelopers/jai-apidocs/javax/media/jai/WarpPerspective.html

      http://docs.oracle.com/cd/E17802_01/products/products/java-media/jai/forDevelopers/jai-apidocs/javax/media/jai/PerspectiveTransform.html

      希望对你有帮助!

      【讨论】:

        猜你喜欢
        • 2018-12-25
        • 1970-01-01
        • 2014-08-18
        • 1970-01-01
        • 2017-10-19
        • 2018-10-20
        • 1970-01-01
        • 2022-11-11
        • 2011-08-27
        相关资源
        最近更新 更多