如何在OpenCV的帮助下改变K矩阵以及失真系数来模拟python中的桶形失真

时间:2017-09-18 12:12:54

标签: python opencv intrinsics distortion

我有相机的内在参数,以及失真系数,我知道如何计算桶形失真。 - 主要来自这篇博文:

Barrel distortion calculation

然而,现在我希望像相机本身那样添加桶形失真。

用于校正桶形失真的代码如下:

import numpy as np
import cv2
from matplotlib import pyplot as plt

# Define camera matrix K
K = np.array([[1.051e+03,0,0],
             [0, 1.0845e+03,0],
             [964.4480,544.2625,1.]])
#Matrix was written in matlab style, hence it has to be transposed ... 
K = K.transpose()



# Define distortion coefficients d
d = np.array([0.0719,-0.0833,0.0013,-6.1840e-04,0])
# Read an example image and acquire its size

img = cv2.imread("grid.png")
h, w = img.shape[:2]

# Generate new camera matrix from parameters
newcameramatrix, roi = cv2.getOptimalNewCameraMatrix(K, d, (w,h), 0)

# Generate look-up tables for remapping the camera image
mapx, mapy = cv2.initUndistortRectifyMap(K, d, None, newcameramatrix, (w, h), 5)

# Remap the original image to a new image
newimg = cv2.remap(img, mapx, mapy, cv2.INTER_LINEAR)

# Display old and new image
fig, (oldimg_ax, newimg_ax) = plt.subplots(1, 2)
oldimg_ax.imshow(img)
oldimg_ax.set_title('Original image')
newimg_ax.imshow(newimg)
newimg_ax.set_title('Unwarped image')
plt.show()

我试图通过使用K-Matrix的倒数或转置的K矩阵来模拟桶形失真,以及将d向量与-1相乘。

我通过以下方式转置它:

K = K.transpose()

或通过以下方式将其反转:

K = np.linalg.inv(K)

但是,这给了我一个黑色的图像。如果我没有倒置/转置它我只是得到负径向分辨率,但我需要一个正径向畸变

0 个答案:

没有答案