Getting zero ROIs from getOptimalNewCameraMatrix

asked 2017-04-19 22:10:29 -0600

boydcheung gravatar image

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),
    if ret:

        for j,c in enumerate(corners):
            color = [255,0,0]
            #color[j%3] = 255



        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)

            # crop the image
            x,y,w,h = roi
            if x!=0:
                dst = dst[y:y+h, x:x+w]
edit retag flag offensive close merge delete


you will have to use the cv2.fisheye calibration and undistort methods.

>>> help(cv2.fisheye)
berak gravatar imageberak ( 2017-04-20 02:43:28 -0600 )edit

I am on and it seems there is no such attribute. Could there be any walkaround?

boydcheung gravatar imageboydcheung ( 2017-04-20 05:09:23 -0600 )edit

bad luck, then, - outdated 2.4 does not have any fisheye (or omni) functionality

berak gravatar imageberak ( 2017-04-20 05:17:27 -0600 )edit