OpenCV校准相机裁剪错误

Posted

技术标签:

【中文标题】OpenCV校准相机裁剪错误【英文标题】:OpenCV Calibrating Camera cropping error 【发布时间】:2015-12-31 18:20:32 【问题描述】:

我正在为 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

【问题讨论】:

使用np.uint8作为你的数组数据类型:dst = np.array(dst,dtype=np.uint8) 这不会改变任何东西,我将它放在dst = dst[y:y+h, x:x+w] 的上方和下方,它的作用完全相同@Kasramvd 什么分配给 roi 变量? 【参考方案1】:

解决这个问题的方法是在ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1],None,None)行下面添加dist = np.array([-0.13615181, 0.53005398, 0, 0, 0]) # no translation

【讨论】:

以上是关于OpenCV校准相机裁剪错误的主要内容,如果未能解决你的问题,请参考以下文章

OpenCV 相机校准

Python OpenCV 立体相机校准阵列错误:TypeError:不支持 imagePoints1 数据类型 = 17

为啥在 OpenCv 中调整图像大小会降低相机校准的重投影误差?

OpenCV 2.3 相机校准

相机校准 OpenCV 相机作为输入

用于 OpenCV 的 Iphone 6 相机校准