如何使用相机矩阵使图像不失真?

时间:2019-06-13 12:48:32

标签: python linux opencv raspberry-pi camera-calibration

极端python noob在这里。我正在尝试在树莓派上校准相机以纠正失真。我已经成功生成了相机矩阵和失真系数,但是现在很难用它们来生成未失真的图像。

这里有代码。 image0是我要取消扭曲的图片:

import numpy as np
import cv2
import os

# Define Camera Matrix
mtx =  np.array([[2.19605891e+03, 0, 1.01476828e+03],
                 [0, 2.21128295e+03, 6.35922558e+02],
                 [0, 0, 1]])

# Define distortion coefficients
dist = np.array([-1.10335088e-01, 8.29479393, 9.69372290e-04, 
                 -3.88984964e-03, -8.36301163e+01])

# Getting the new optimal camera matrix
img = cv2.imread('image0.jpg')
h, w = img.shape[:2]
newcameramtx, roi = cv2.getOptimalNewCameraMatrix(mtx,dist,(w,h),1, 
                    (w,h))

# Checking to make sure the new camera materix was properly generated
print(newcameramtx)

# Undistorting
dst = cv2.undistort(img, mtx, dist, None, newcameramtx)

# Cropping the image
x,y,w,h = roi
dst = dst[y:+y+h, x:x+w]
cv2.imwrite('calibresult.png',dst)

代码没有给我任何错误,但是每当我尝试打开所谓的未失真图像时,它就会告诉我: Image file '/home/pi/calibresult.png' contains no data

我不知道为什么无法正确生成图像。

1 个答案:

答案 0 :(得分:0)

检查文件image0.jpg是否确实存在,是否不存在image71.jpg或其他文件名。