OpenCV:WarpPerspective功能的参数是什么?

时间:2014-09-10 13:19:23

标签: java opencv image-processing homography

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

为了举例,让我们说我的源梯形在(0,20),(0,80),(200,0)和(200,100)处有角。然后我想最终得到一个矩形的"伸展"到(0,0),(0,100),(200,0)和(200,200)。任何人都可以提供代码示例吗?图像采用Mat形式。

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

我不知道我是否能用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

我希望这可以帮到你!