我正在为OpenCV 3校准我的相机。目前我正在关注教程和文档,但我遇到了一个问题。我的代码结束让我裁剪图像,以消除可见的扭曲(如下图所示)
但是,当我尝试这个时,我有时会得到错误
libpng warning: Image width is zero in IHDR
libpng warning: Image height is zero in IHDR
libpng error: Invalid IHDR data
它不会阻止程序运行,但它会保存没有像素的图像。我假设裁剪功能正在裁剪整个图像,这种情况大约发生在80%左右。
import numpy as np
import cv2
import glob
import time
thyme = ('1')
# termination criteria
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
# prepare object points, like (0,0,0), (1,0,0), (2,0,0) ....,(6,5,0)
objp = np.zeros((6*9,3), np.float32)
objp[:,:2] = np.mgrid[0:6,0:9].T.reshape(-1,2)
# Arrays to store object points and image points from all the images.
objpoints = [] # 3d point in real world space
imgpoints = [] # 2d points in image plane.
images = glob.glob('*.jpg')
for fname in images:
img = cv2.imread(fname)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
# Find the chess board corners
ret, corners = cv2.findChessboardCorners(gray, (6,9),None)
# If found, add object points, image points (after refining them)
if ret == True:
objpoints.append(objp)
ticks = time.time()
thyme = str(ticks)
corners2 = cv2.cornerSubPix(gray,corners,(11,11),(-1,-1),criteria)
imgpoints.append(corners2)
# Draw and display the corners
img = cv2.drawChessboardCorners(img, (6,9), corners2,ret)
ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1],None,None)
img = cv2.imread('left1.jpg')
h, w = img.shape[:2]
newcameramtx, roi=cv2.getOptimalNewCameraMatrix(mtx,dist,(w,h),1,(w,h))
# undistort
dst = cv2.undistort(img, mtx, dist, None, newcameramtx)
# crop the image
x,y,w,h = roi
print('1')
dst = dst[y:y+h, x:x+w]
print('2')
cv2.imwrite('calibresult' + thyme + '.png',dst)
print('3')
cv2.imshow('img',img)
cv2.waitKey(500)
cv2.destroyAllWindows()
包含打印语句,以便我可以查看图像是否抛出错误或是否清除。
编辑:这是我的错误
1
2
libpng warning: Image width is zero in IHDR
libpng warning: Image height is zero in IHDR
libpng error: Invalid IHDR data
3
1
2
libpng warning: Image width is zero in IHDR
libpng warning: Image height is zero in IHDR
libpng error: Invalid IHDR data
3
1
2
libpng warning: Image width is zero in IHDR
libpng warning: Image height is zero in IHDR
libpng error: Invalid IHDR data
3
答案 0 :(得分:0)
解决此问题的方法是在dist = np.array([-0.13615181, 0.53005398, 0, 0, 0]) # no translation
ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1],None,None)