Ask Your Question

Revision history [back]

Getting zero ROIs from getOptimalNewCameraMatrix

Hi folks, I am attempting to do some calibration on my fisheye PiCamera, now stuck on this func getOptimalNewCameraMatrix, the code works when fisheye lens removed, but fails when it is mounted. I've tuned alpha value between [0,1], but with no avail. Could anyone give me a clue?

import numpy as np
import cv2
import glob
import matplotlib.pyplot as plt
import shutil
import os

CORNER_COL = 7    #6
CORNER_ROW = 6    #9

def bgr2rgb(img):
    return cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

images = ['/Users/bo/Desktop/calibrate3/20170408-003521_0.00_0.00_0.00_True-13.jpg']

# 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((CORNER_COL*CORNER_ROW,3), np.float32)
objp[:,:2] = np.mgrid[0:CORNER_COL,0:CORNER_ROW].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

for i,fname in enumerate(images):
    img = cv2.imread(fname)
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    img = bgr2rgb(img)

    # Find the chess board corners
    ret, corners = cv2.findChessboardCorners(gray, (CORNER_COL,CORNER_ROW), cv2.cv.CV_CALIB_CB_ADAPTIVE_THRESH)
    if ret:
        cv2.cornerSubPix(gray,corners,(11,11),(-1,-1),criteria)

        for j,c in enumerate(corners):
            color = [255,0,0]
            #color[j%3] = 255
            cv2.circle(img,(int(c[0][0]),int(c[0][1])),3,color,-1,8)

        plt.imshow(img)
        plt.show()

        objpoints.append(objp)

        imgpoints.append(corners)
        ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1],None,None)
        if ret:
            print mtx
            print dist

            h, w = img.shape[:2]
            ret, roi=cv2.getOptimalNewCameraMatrix(mtx,dist,(w,h),1,(w,h))
            print ret, roi

            dst = cv2.undistort(img, mtx, dist, None, ret)#newcameramtx)
            plt.imshow(dst)
            plt.show()

            # crop the image
            x,y,w,h = roi
            if x!=0:
                dst = dst[y:y+h, x:x+w]
                plt.imshow(dst)
                plt.show()