1

I'm trying to store into a txt file the result of my camera calibration intended as mapx and mapy retrived from the following script:

mapx, mapy = cv2.initUndistortRectifyMap(mtx, dist, None, newcameramtx, (w, h), cv2.CV_16SC2)

Those are two matrix with respective shape: (480, 640, 2) and (480, 640)

The save function is working properly and it is the following one:

def write(filename, data):
    with open(filename, 'w') as outfile:
        outfile.write('# Array shape: {0}\n'.format(data.shape))

        for data_slice in data:
            np.savetxt(outfile, data_slice, fmt='%-7.0f')
            outfile.write('# New slice\n')

Once the file is stored appears like this (mapy example):

# Array shape: (480, 640)
372    
170    
992    
823    
621    
452    
282    
113    
935
....
86     
193    
# New slice
274    
203    
131    
92     
21     
1006   
935
....

I'm reading it back with this function:

shapex = (480, 640, 2)
shapey = (480, 640)

file_mapx = "mapx.txt"
file_mapy = "mapy.txt"

mapx = np.loadtxt(file_mapx) if os.path.exists(file_mapx) else None
mapy = np.loadtxt(file_mapy) if os.path.exists(file_mapy) else None  


if mapx is not None:
    print("Reshape mapx")
    mapx = mapx.reshape(shapex)
if mapy is not None:
    print("Reshape mapy")
    mapy = mapy.reshape(shapey)

Everything seams working correctly but when I use those maps for the undistortion function

# undistort
undistorted_img = cv2.remap(img, mapx, mapy, cv2.INTER_LINEAR)

I get the following error:

undistorted_img = cv2.remap(img, mapx, mapy, cv2.INTER_LINEAR)
cv2.error: OpenCV(4.1.1) /home/myuser/opencv/modules/imgproc/src/imgwarp.cpp:1815: error: (-215:Assertion failed) ((map1.type() == CV_32FC2 || map1.type() == CV_16SC2) && map2.empty()) || (map1.type() == CV_32FC1 && map2.type() == CV_32FC1) in function 'remap'

I tried following the question here but I didn't manage to understand the mistake, any help?

sebasth
  • 959
  • 10
  • 21
Lorenzo Sciuto
  • 1,655
  • 3
  • 27
  • 57

1 Answers1

1

As the error message says, your input to remap needs to have correct type. When you are using CV_16SC2, the corresponding numpy type is np.int16. Assuming all the values are read correctly, you can use mapx.astype(np.int16) and mapy.astype(np.int16) to convert the arrays.

You should consider saving the camera matrix instead of transformation maps. It will be useful if you want to use different options with initUndistortRectifyMap() later (for example if you decide to change the image resolution) or want to calculate something else which requires knowing the intrinsic parameters. Camera matrices are also a lot smaller than the transformation maps.

sebasth
  • 959
  • 10
  • 21
  • Thank you. I'm saving the transformation maps cause I need a really fast rectification of the images and the remap function only is fast enough but not combined with the initUndistortRectifyMap which will cause lack on performance. – Lorenzo Sciuto Nov 04 '19 at 14:57