使用“侧滚”、“俯仰”和“y”扭曲图像

2024-05-26 14:21:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在Python中使用OpenCV来纠正航空图像中的图像失真。我有横摇,俯仰和偏航的数据。我知道我需要创建一个扭曲矩阵,并将该矩阵应用于我的原始坐标点,以创建图像的输出点。我可以影响图像的移动方式,但我觉得有一个错误,因为只有那些值似乎适用于非常小的值。在

以下是我当前的代码:

warp_mat = np.array([[math.cos(theta)*math.cos(psy), math.cos(phi)*math.sin(psy)+math.sin(phi)*math.sin(theta)*math.cos(psy), math.sin(phi)*math.sin(psy)-math.cos(phi)*math.sin(theta)*math.cos(psy)],\
                    [-1*math.cos(theta)*math.sin(psy), math.cos(phi)*math.cos(psy)-math.sin(phi)*math.sin(theta)*math.sin(psy), math.sin(phi)*math.cos(psy)+math.cos(phi)*math.sin(theta)*math.sin(psy)],\
                    [math.sin(theta), -1*math.sin(phi)*math.cos(theta), math.cos(phi)*math.cos(theta)]], dtype='float32')


srcPts = np.array([[-2064, 1161, 1],\
                  [2064, 1161, 1],\
                  [2064, -1161, 1],\
                  [-2064, -1161, 1]], dtype='float32')

dstPts = np.empty(shape = (4,3), dtype='float32')


dstPts[0][0] = srcPts[0][0] * warp_mat[0][0] + srcPts[0][1] * warp_mat[1][0] + srcPts[0][2] * warp_mat[2][0];
dstPts[0][1] = srcPts[0][0] * warp_mat[0][1] + srcPts[0][1] * warp_mat[1][1] + srcPts[0][2] * warp_mat[2][1];
dstPts[0][2] = srcPts[0][0] * warp_mat[0][2] + srcPts[0][1] * warp_mat[1][2] + srcPts[0][2] * warp_mat[2][2];

dstPts[1][0] = srcPts[1][0] * warp_mat[0][0] + srcPts[1][1] * warp_mat[1][0] + srcPts[1][2] * warp_mat[2][0];
dstPts[1][1] = srcPts[1][0] * warp_mat[0][1] + srcPts[1][1] * warp_mat[1][1] +     srcPts[1][2] * warp_mat[2][1];
dstPts[1][2] = srcPts[1][0] * warp_mat[0][2] + srcPts[1][1] * warp_mat[1][2] + srcPts[1][2] * warp_mat[2][2];

dstPts[2][0] = srcPts[2][0] * warp_mat[0][0] + srcPts[2][1] * warp_mat[1][0] + srcPts[2][2] * warp_mat[2][0];
dstPts[2][1] = srcPts[2][0] * warp_mat[0][1] + srcPts[2][1] * warp_mat[1][1] + srcPts[2][2] * warp_mat[2][1];
dstPts[2][2] = srcPts[2][0] * warp_mat[0][2] + srcPts[2][1] * warp_mat[1][2] + srcPts[2][2] * warp_mat[2][2];

dstPts[3][0] = srcPts[3][0] * warp_mat[0][0] + srcPts[3][1] * warp_mat[1][0] + srcPts[3][2] * warp_mat[2][0];
dstPts[3][1] = srcPts[3][0] * warp_mat[0][1] + srcPts[3][1] * warp_mat[1][1] + srcPts[3][2] * warp_mat[2][1];
dstPts[3][2] = srcPts[3][0] * warp_mat[0][2] + srcPts[3][1] * warp_mat[1][2] + srcPts[3][2] * warp_mat[2][2];


dstPts[0][0] = dstPts[0][0] / dstPts[0][2];
dstPts[0][1] = dstPts[0][1] / dstPts[0][2];
dstPts[0][2] = dstPts[0][2] / dstPts[0][2];

dstPts[1][0] = dstPts[1][0] / dstPts[1][2];
dstPts[1][1] = dstPts[1][1] / dstPts[1][2];
dstPts[1][2] = dstPts[1][2] / dstPts[1][2];

dstPts[2][0] = dstPts[2][0] / dstPts[2][2];
dstPts[2][1] = dstPts[2][1] / dstPts[2][2];
dstPts[2][2] = dstPts[2][2] / dstPts[2][2];

dstPts[3][0] = dstPts[3][0] / dstPts[3][2];
dstPts[3][1] = dstPts[3][1] / dstPts[3][2];
dstPts[3][2] = dstPts[3][2] / dstPts[3][2];



srcPts2 = np.array([[srcPts[0][0],srcPts[0][1]],\
                   [srcPts[1][0],srcPts[1][1]],\
                   [srcPts[2][0],srcPts[2][1]],\
                   [srcPts[3][0],srcPts[3][1]]], dtype='float32')

dstPts2 = np.array([[dstPts[0][0],dstPts[0][1]],\
                   [dstPts[1][0],dstPts[1][1]],\
                   [dstPts[2][0],dstPts[2][1]],\
                   [dstPts[3][0],dstPts[3][1]]], dtype='float32')


transMatrix = cv.getPerspectiveTransform(srcPts2, dstPts2)


dst = cv.warpPerspective(imgFile,transMatrix,(4128,2322) ,borderMode = cv.BORDER_CONSTANT,borderValue = 0)

Tags: 图像npmathsincosarrayphidtype
2条回答

在代码的开头,通过投影四个点来计算扭曲矩阵,然后使用getPerspectiveTransform()来找出转换矩阵。这应该行得通,但它比必要的复杂得多。如果知道横摇、俯仰和偏航角,就可以直接计算变换矩阵。看看http://image2measure.net/files/calib3Dto2D.cpp中的BirdsEyeView()函数。它就是这样做的。
我不得不换行
Mat R = RX * RY * RZ;
Mat R = RZ * RX * RY; 为了让它得到正确的转换。在

f是以像素为单位的焦距。
如果rh是图像的水平分辨率,oh是相机的水平打开角度
f=(rh/2)/棕褐色(oh/2)
如果不想缩放图像,请为Distance选择相同的值,选择比f更大的值来放大图像,或者选择较小的值来缩小图像。在

代码BirdsEyeView()对我有用,但我不知道为什么要交换横摇和俯仰角。当我改变“alpha”时,图像在音调上是扭曲的,当我改变“beta”时,图像在滚动中扭曲。所以,我改变了我的旋转矩阵,如下所示。在

另外,RY有一个信号错误。你可以在:http://en.wikipedia.org/wiki/Rotation_matrix 我想这就是阿德里安把乘法顺序从R=RX*RY*RZ改为R=RZ*RX*RY的原因

我使用的旋转矩阵:

    Mat RX = (Mat_<double>(4, 4) <<
        1,          0,           0, 0,
        0, cos(beta), -sin(beta), 0,
        0, sin(beta),  cos(beta), 0,
        0,          0,           0, 1);

    Mat RY = (Mat_<double>(4, 4) <<
         cos(alpha), 0,  sin(alpha), 0,
                  0, 1,           0, 0,
        -sin(alpha), 0,  cos(alpha), 0,
                  0, 0,           0, 1);

    Mat RZ = (Mat_<double>(4, 4) <<
        cos(gamma), -sin(gamma), 0, 0,
        sin(gamma),  cos(gamma), 0, 0,
        0,          0,           1, 0,
        0,          0,           0, 1);

问候

相关问题 更多 >

    热门问题