如何在使用OpenCV的Python中更改K矩阵和畸变系数来模拟桶形畸变

2024-10-01 19:17:56 发布

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

我有相机的固有参数,以及失真系数,我知道如何计算出枪管的变形。-主要来自以下博客:

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矩阵的逆矩阵,或者转置的K矩阵,以及用-1乘以d向量来模拟桶形失真。在

我通过:

^{pr2}$

或者通过以下方式进行反转:

K = np.linalg.inv(K)

然而,这只给了我一个黑色的形象。如果我不反转/转置它,我只得到一个负的径向分辨率,但是我需要一个正的径向失真


Tags: imageimportimgnewasnpplt矩阵

热门问题