【问题标题】:Problems with a perspective homography透视单应性问题
【发布时间】:2013-01-17 20:50:51
【问题描述】:

我正在尝试使用 OpenCV 和 Python 中的透视扭曲来校正图像。我知道相机的方向(X、Y、Z 角度)参考需要翘曲的平面。我知道最简单的方法是根据已知点计算单应矩阵,但是当该信息不可用时,我正在尝试做同样的事情。我正在使用的代码创建了一个旋转矩阵,然后结合了平移矩阵和内在矩阵。目前,该代码仅适用于对 z 轴的操作。对 x 和 y 轴的任何操作都会导致图像出现奇怪的失真。我的代码基于以下帖子底部的答案:Perspective Warping in OpenCV based on know camera orientation

附件是标准单应性方法的原始图像和扭曲图像。

from numpy import *
import cv

x = float(0)
y = float(5)
z = float(0)
f = 1

im = cv.LoadImage("Homography_test.jpg")
cv.NamedWindow("Distorted")
cv.NamedWindow("undistorted")

h, w = cv.GetSize(im)

x1 = x * (pi / 180)
y1 = y * (pi / 180)
z1 = z * (pi / 180)

# Create a rotation matrix
R_array = array([[x1], [y1], [z1]])
R_Vec = cv.fromarray(R_array)
R = cv.CreateMat(3, 3, cv.CV_64FC1)

cv.Rodrigues2(R_Vec, R)

#Create and combine with translation matrix
Trans_Mat = array([[[1], [0], [-w/2]],
                    [[0], [1], [-h/2]],
                    [[0], [0], [1]]])

Trans_Mat2 = cv.fromarray(Trans_Mat)
R_T_Mat = dot(R, Trans_Mat2)

#Create and combine with camera matrix
Intrinsic_Mat = array([[[f], [0], [w/2]],
                       [[0], [f], [h/2]],
                       [[0], [0], [1]]])

Int_Mat = cv.fromarray(Intrinsic_Mat)
H = dot(Int_Mat, R_T_Mat)
H2 = cv.fromarray(H)

persp = cv.CreateImage(cv.GetSize(im), cv.IPL_DEPTH_8U, 3)
cv.WarpPerspective(im, persp, H2)

cv.ShowImage("Distorted", im)
cv.ShowImage("undistorted", persp)

cv.WaitKey(0)
cv.DestroyWindow("Distorted")
cv.DestroyWindow("undistorted")

【问题讨论】:

    标签: python image-processing opencv perspectivecamera


    【解决方案1】:

    您似乎错过了我的解决方案中的第 4 步,即“将图像沿 z 轴向下移动”,我曾经这样做过。

    //4
    trans(2,2) += image.rows;
    

    我随意选择了 image.rows,它为我在示例中使用的旋转提供了很好的比例感。由于您没有这样做,因此您的 z 坐标固定为 1。我想这是您绕 x 和 y 旋转时失真的原因,本质上是因为图像绕 x 或 y 旋转时离相机非常近透视失真是巨大的。沿 z 轴的平移越大,失真应​​该出现的越小。如果您不希望图像在沿 z 轴移动时缩小,只需增加焦距即可。您可能会注意到,在我的示例中,我将焦距设置为 image.rows。

    【讨论】:

    • 谢谢锤子。在 Z 轴上移动图像有点效果,但调整焦距效果更好。
    猜你喜欢
    • 2018-11-14
    • 1970-01-01
    • 1970-01-01
    • 2016-11-19
    • 2016-12-21
    • 2018-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多