python - 如何使用我的相机矩阵使图像不失真?
问题描述
极端蟒蛇菜鸟在这里。我正在尝试校准我的树莓派上的相机以纠正失真。我已经成功地生成了我的相机矩阵和失真系数,但现在在实际使用它们来生成未失真的图像时遇到了麻烦。
这是代码。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
.
我不知道为什么图像没有正确生成。
解决方案
推荐阅读
- amazon-web-services - 使用 hcl2 创建 ami 时出现 Packer 错误:“查询 AMI 时出错:InvalidAMIID.Malformed:ID 无效:”
- racket - 试图制作一个检查列表排序的函数
- docker - 在 Docker/Jenkins 构建中使用 ESLint
- azure - 如何使用 ARM 模板在 Azure Data Lake 容器内创建文件夹?
- continuous-integration - Sonarqube - 在社区版中对开发分支与主分支运行扫描
- python - ImportError:无法从“aiohttp.helpers”导入名称“CeilTimeout”
- php - GRPC php 已安装,在 ini 中启用并在终端中工作,但 phpinfo 未显示启用
- javascript - Datatables Jquery Bootstrap 5在行中创建动态下拉列表并将数据发布到php文件
- php - 如何防止我的购物车菜单在为空时显示为零?
- node.js - 如何使用进程 ID 将数据传输到实时 nodejs 应用程序?