透视在OpenCV中翘曲基于知道相机方向 [英] Perspective Warping in OpenCV based on know camera orientation

查看:256
本文介绍了透视在OpenCV中翘曲基于知道相机方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据相机的已知方向从图像中删除透视失真的项目。我的想法是,我可以基于相机的已知X,Y和Z方向创建一个旋转矩阵。我可以通过WarpPerspective方法将这些矩阵应用到图像。

I am working on a project which attempts to remove the perspective distortion from an image based on the known orientation of the camera. My thinking is that I can create a rotational matrix based on the known X, Y, and Z orientations of the camera. I can then apply those matrices to the image via the WarpPerspective method.

在我的脚本(用Python编写)中,我创建了三个旋转矩阵, 。我已经到了一个点,我被困在两个问题。首先,当我将每个单独的矩阵加载到WarpPerspective方法中时,它似乎不能正常工作。每当我在一个轴上扭曲图像时,它似乎显着地覆盖了图像。

In my script (written in Python) I have created three rotational matrices, each based on an orientation angle. I have gotten to a point where I am stuck on two issues. First, when I load each individual matrix into the WarpPerspective method, it doesn't seem to be working correctly. Whenever I warp an image on one axis it appears to significantly overwarp the image. The contents of the image are only recognizable if I limit the orientation angle to around 1 degree or less.

其次,我如何将三个旋转矩阵组合成一个矩阵被加载到WarpPerspective方法中。我可以导入一个3x3旋转矩阵到该方法,或者我必须创建一个4x4投影矩阵。下面是我正在编写的代码。

Secondly, how do I combine the three rotational matrices into a single matrix to be loaded into the WarpPerspective method. Can I import a 3x3 rotational matrix into that method, or do I have to create a 4x4 projective matrix. Below is the code that I am working on.

感谢您的帮助。

CR

from numpy import *
import cv

#Sets angle of camera and converts to radians
x =  -14 * (pi/180)
y = 20 * (pi/180)
z =  15 * (pi/180)

#Creates the Rotational Matrices
rX = array([[1, 0, 0], [0, cos(x), -sin(x)], [0, sin(x), cos(x)]])
rY = array([[cos(y), 0, -sin(y)], [0, 1, 0], [sin(y), 0, cos(y)]])
rZ = array([[cos(z), sin(z), 0], [-sin(z), cos(z), 0], [0, 0, 1]])

#Converts to CVMat format
X = cv.fromarray(rX)
Y = cv.fromarray(rY)
Z = cv.fromarray(rZ)

#Imports image file and creates destination filespace
im = cv.LoadImage("reference_image.jpg")
dst = cv.CreateImage(cv.GetSize(im), cv.IPL_DEPTH_8U, 3)

#Warps Image
cv.WarpPerspective(im, dst, X)

#Display
cv.NamedWindow("distorted")
cv.ShowImage("distorted", im)
cv.NamedWindow("corrected")
cv.ShowImage("corrected", dst)
cv.WaitKey(0)
cv.DestroyWindow("distorted")
cv.DestroyWindow("corrected")


推荐答案

只知道旋转是不够的除非您的图像是使用远心镜头或长焦镜头(在这种情况下,图像几乎是正交的,没有透视失真)。

Just knowing the rotation is not enough unless your images are taken either using a telecentric lens, or with a telephoto lens with very long focal (in which cases the images are nearly orthographic, and there is no perspective distortion).

此外,这不是必要的。 True,您可以通过校准相机来撤消图像中一个平面的透视缩短(即估计内部和外部参数以形成完整的相机投影矩阵)。

Besides, it's not necessary. True, you can undo the perspective foreshortening of one plane in the image by calibrating the camera (i.e. estimating the intrinsic and extrinsic parameters to form the full camera projection matrix).

但是,如果你能在图像中识别一个四边形,它是一个真实世界正方形(或具有已知宽度/高度比的矩形)的图像,你可以更简单地实现相同的结果。如果你能做到这一点,你可以简单地计算单位矩阵,将正方形(矩形)映射到四边形,然后使用其逆矩形扭曲。

But you achieve the same result much more simply if you can identify in the image a quadrangle which is the image of a real-world square (or rectangle with known width/height ratio). If you can do that, you can trivially compute the homography matrix that maps the square (rectangle) to the quadrangle, then warp using its inverse.

这篇关于透视在OpenCV中翘曲基于知道相机方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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