如何在使用OpenCV warpPerspective时显示整个图像 [英] How to show the whole image when using OpenCV warpPerspective

查看:7762
本文介绍了如何在使用OpenCV warpPerspective时显示整个图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里有2张测试图片。我的问题是,如何将第一个图像中的正方形映射到第二个图像中的四边形,而不裁剪图像。

I have 2 test images here. My is question is, how to map the square in first image to the quadrilateral in the second image without cropping the image.

图像1:

Image 1:

图片2:

Image 2:

这里是我当前使用openCV warpPerspective函数的代码。

Here is my current code using openCV warpPerspective function.

import cv2
import numpy as np

img1_square_corners = np.float32([[253,211], [563,211], [563,519],[253,519]])
img2_quad_corners = np.float32([[234,197], [520,169], [715,483], [81,472]])

h, mask = cv2.findHomography(img1_square_corners, img2_quad_corners)
im = cv2.imread("image1.png")
out = cv2.warpPerspective(im, h, (800,800))
cv2.imwrite("result.png", out)

结果:

如您所见,由于dsize = 800,800)参数在warpPerspective函数中,我不能得到图像1的完整视图。如果我调整dsize,正方形将无法正确映射。是否有任何方法调整输出图像的大小,以便我可以获得图像1的完整图片?

As you can see, because of dsize=(800,800) parameter in the warpPerspective function, I can't get full view of image 1. If I adjust the dsize, the square won't map properly. Is there any way to resize the output image so that I can get whole picture of image 1?

推荐答案

意识到输出图像可能非常大。我很快写了下面的Python代码,但即使一个3000 x 3000的图像不能适应输出,它只是由于变换太大了。虽然,这里是我的代码,我希望它会对你有用。

Yes, but you should realise that the output image might be very large. I quickly wrote the following Python code, but even a 3000 x 3000 image could not fit the output, it is just way too big due to the transformation. Although, here is my code, I hope it will be of use to you.

import cv2
import numpy as np
import cv           #the old cv interface

img1_square_corners = np.float32([[253,211], [563,211], [563,519],[253,519]])
img2_quad_corners = np.float32([[234,197], [520,169], [715,483], [81,472]])

h, mask = cv2.findHomography(img1_square_corners, img2_quad_corners)
im = cv2.imread("image1.png")

在这里创建一个输出图像,一个示例。

Create an output image here, I used (3000, 3000) as an example.

out_2 = cv.fromarray(np.zeros((3000,3000,3),np.uint8))

通过使用旧的 cv 直接写到输出,所以它不会被裁剪。我试过这个使用 cv2 界面,但由于某种原因,它没有工作...也许有人可以阐明一些光吗?

By using the old cv interface, I wrote directly to the output, and so it does not get cropped. I tried this using the cv2 interface, but for some reason it did not work... Maybe someone can shed some light on that?

cv.WarpPerspective(cv.fromarray(im), out_2, cv.fromarray(h))
cv.ShowImage("test", out_2)
cv.SaveImage("result.png", out_2)
cv2.waitKey()

无论如何,这给了一个非常大的图像,其中包含您的原始图像1,扭曲。如果您将输出图像指定为足够大,则整个图像将可见。 (这可能非常大!)

Anyway, this gives a very large image, that contains your original image 1, warped. The entire image will be visible if you specify the output image to be large enough. (Which might be very large indeed!)

我希望这段代码可以帮助你。

I hope that this code may help you.

这篇关于如何在使用OpenCV warpPerspective时显示整个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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