如何通过在图像中选择四个点来使图像变直? [英] How to straight an image by selecting four points in an Image?
问题描述
我想通过使用Java中的opencv的 getPerspectiveTransform()方法在图像中选择四个点来使图像变直.我知道可以在python中使用opencv来完成:
有透视图转换,可用于实现四到四转换.在 OpenCV 中,主要有两种方法来实现getPerspectiveTransformation()和warpPerspective()方法.
以下是实现图像矫正的示例代码:
首先在源图像中获得四个四边形点
Mat srcImage = Imgcodecs.imread("input.png");Mat destImage =新Mat(500,700,srcImage.type());Mat src =新的MatOfPoint2f(新的Point(x1,y1),新的Point(x2,y2),新的Point(x3,y3),新的Point(x4,y4));Mat dst =新的MatOfPoint2f(新Point(0,0),新Point(destImage.width()-1,0),新Point(destImage.width()-1,destImage.height()-1),新Point(0,destImage.height()-1));
获取转换metrix
Mat transform = Imgproc.getPerspectiveTransform(src,dst);Imgproc.warpPerspective(srcImage,destImage,transform,destImage.size());
您将获得变形的拉直图像.
我已经在这张图片上测试了此代码.
,结果图像为
I want to straight an image by selecting four points in the image using getPerspectiveTransform() method of opencv in java. I know it can be done using with opencv in python: getPerspectiveTransform. If anyone used this to achieve image straightening ..please help.
There is perspective transformation which can be used to achieve quad to quad conversion. In OpenCV, there are mainly two methods to achieve getPerspectiveTransformation() and warpPerspective() method.
Here is a sample code to achieve Image Straightening:
First get four quadrilinear points in source image
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));
Getting transformation metrix
Mat transform = Imgproc.getPerspectiveTransform(src, dst);
Imgproc.warpPerspective(srcImage, destImage, transform, destImage.size());
you will get transformed straighten image.
I have tested this code on this image.
and The resulted image is
这篇关于如何通过在图像中选择四个点来使图像变直?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!