Я пытаюсь сохранить в текстовый файл результат калибровки моей камеры, заданный как mapx и mapy, полученный из следующего скрипта:
mapx, mapy = cv2.initUndistortRectifyMap(mtx, dist, None, newcameramtx, (w, h), cv2.CV_16SC2)
Это две матрицы с соответствующей формой: (480, 640, 2)
и (480, 640)
Функция сохранения работает правильно, и она выглядит следующим образом:
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')
После сохранения файл выглядит следующим образом (пример mapy):
# Array shape: (480, 640)
372
170
992
823
621
452
282
113
935
....
86
193
# New slice
274
203
131
92
21
1006
935
....
Я читаю это обратно с помощью этой функции:
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)
Все швы работают правильно, но когда я использую эти карты для функции искажения
# undistort
undistorted_img = cv2.remap(img, mapx, mapy, cv2.INTER_LINEAR)
Я получаю следующееошибка:
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'
Я пытался ответить на вопрос здесь , но мне не удалось понять ошибку, какая-нибудь помощь?