首页 > 解决方案 > 如何使用我的相机矩阵使图像不失真?

问题描述

极端蟒蛇菜鸟在这里。我正在尝试校准我的树莓派上的相机以纠正失真。我已经成功地生成了我的相机矩阵和失真系数,但现在在实际使用它们来生成未失真的图像时遇到了麻烦。

这是代码。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.

我不知道为什么图像没有正确生成。

标签: pythonlinuxopencvraspberry-picamera-calibration

解决方案


推荐阅读