OpenCV:WarpPerspective功能的参数是什么? [英] OpenCV: What are the parameters for the WarpPerspective function?

查看:3847
本文介绍了OpenCV:WarpPerspective功能的参数是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm building an application in Java that uses OpenCV. I haven't used the library before, and the Java implementation is a bit lacking in documentation. I need to undo a perspective warped image to make it squared up. I need to transform a trapezoid to a rectangle. Basically I need to stretch the shorter of the two parallel sides to match the length of the longer parallel side. I know I need to compute homographies and use a WarpPerspective function, but I have no idea how to structure this command. I know what a homography is, but I don't know how to declare one and I don't know what to feed into warpPerspective. Let me emphasize, I understand the theory, I just need to learn the text syntax in code.

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

For the sake of example, let's say my source trapezoid has corners at (0,20), (0,80), (200,0), and (200,100). I then want to end up with a rectangular "stretch" to (0,0), (0,100),(200,0), and (200,200). Can anyone provide a code example? The images are in Mat form.

推荐答案

源图片

目标图片

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是目的地垫

这篇关于OpenCV:WarpPerspective功能的参数是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆