python - 使用滚动,俯仰和偏航使图像变形

标签 python image opencv matrix pitch

我目前在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)

最佳答案

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

f是以像素为单位的焦距。
如果rh是图像的水平分辨率,哦是相机的水平开度
f =(rh / 2)/ tan(oh / 2)
如果您不希望缩放图像,请为“距离”选择相同的值,大于f的值可放大或减小图像。

关于python - 使用滚动,俯仰和偏航使图像变形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19470955/

相关文章:

python - 如何取消合并Excel工作表中的单元格?

Python无法制作网络服务器

python - gpiozero.exc.PinPWM不支持 : PWM is not supported on pin GPIO7 (Raspberry Pi 4B)

python - 打开 PyGTK 程序但不激活它

image - FFmpeg 图像到视频和流媒体

image - 为什么我需要图像占位符服务或库?

c++ - caffe2 Tensor<CUDAContext> 赋值、构造或复制

Opencv:背景减法:访问冲突

jquery - 等待图像替换,直到图像加载完毕

qt - 如何在带有opencv-cv::Scalar的QT-QColor之间转换?