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?