我正在为OpenCV 3校准相机。目前,我正在阅读教程和文档,遇到了问题。代码的末尾让我裁剪图像,以消除可见的失真(如下所示)python - OpenCV校准相机裁剪错误-LMLPHP
但是,当我尝试这样做时,有时会出现错误libpng warning: Image width is zero in IHDRlibpng warning: Image height is zero in IHDRlibpng 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()

包括打印语句,以便我可以查看图像是否抛出错误或是否干净。

编辑:这是我的错误
12libpng warning: Image width is zero in IHDRlibpng warning: Image height is zero in IHDRlibpng error: Invalid IHDR data312libpng warning: Image width is zero in IHDRlibpng warning: Image height is zero in IHDRlibpng error: Invalid IHDR data312libpng warning: Image width is zero in IHDRlibpng warning: Image height is zero in IHDRlibpng error: Invalid IHDR data3

最佳答案

解决此问题的方法是在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)

关于python - OpenCV校准相机裁剪错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34550238/

10-11 06:43