pythonopencvopencv3.0calibrationfisheye

OpenCV - Fisheye Calibration Values (Reuse values saved in JSON file)


I have calibrated my fisheye cameras getting the new matrix and parameters. I have saved them in a JSON file. Now I have problems reusing the JSON file again to calibrate my camera.

Referred from https://github.com/mesutpiskin/opencv-fisheye-undistortion/blob/master/src/python/fisheye_calibration_undistortion.py

''''
I have realized the K has a different structure. e.g.
<class 'numpy.ndarray'>
[[ 2.01424373e+06  0.00000000e+00  6.50364504e+02]
 [ 0.00000000e+00  2.04712849e+06 -5.14192522e+02]
 [ 0.000000

00e+00  0.00000000e+00  1.00000000e+00]]
''''

Saving the values, 'K': np.asarray(K).tolist() (Need conversion back when reading???)

'''
data = {'dim1': dim1,'dim2':dim2,'dim3': dim3,'K': np.asarray(K).tolist(),'D':np.asarray(D).tolist(),'new_K':np.asarray(new_K).tolist(),'scaled_K':np.asarray(scaled_K).tolist(),'balance':balance}
                
import json
with open("fisheye_calibration_data.json", "w") as f:
    json.dump(data, f)    
'''

Reading the values from JSON (problem here)

'''
with open('fisheye_calibration_data.json') as json_file:

    data = json.load(json_file)
    dim1 = data["dim1"]
    dim2 = data["dim2"]
    dim3 = data["dim3"]
    K = data["K"]
    D = data["D"]
    new_K = data["new_K"]
    scaled_K = data["scaled_K"]
    balance = data["balance"]
    
new_K = cv2.fisheye.estimateNewCameraMatrixForUndistortRectify(scaled_K, D, dim2, np.eye(3), balance=balance)

map1, map2 = cv2.fisheye.initUndistortRectifyMap(scaled_K, D, np.eye(3),new_K, dim3, cv2.CV_16SC2)
    
undistorted_img = cv2.remap(frame, map1, map2,interpolation=cv2.INTER_LINEAR,borderMode=cv2.BORDER_CONSTANT) 
'''

Error obtained:

-----------------ERROR_----------------------
    new_K = cv2.fisheye.estimateNewCameraMatrixForUndistortRectify(scaled_K, D,dim2, np.eye(3), balance=balance)

TypeError: Expected cv::UMat for argument 'K'

Solution

  • dim1 = tuple(data["dim3"]) K = np.array(data["K"])