[英]Python - OpenCv Camera Calibration - Saving map matrix to file and read them back
我正在尝试将我的相机校准结果存储到一个 txt 文件中,该结果旨在作为从以下脚本中检索的 mapx 和 mapy:
mapx, mapy = cv2.initUndistortRectifyMap(mtx, dist, None, newcameramtx, (w, h), cv2.CV_16SC2)
这是两个具有各自形状的矩阵: (480, 640, 2)
和(480, 640)
保存 function 工作正常,它是以下一个:
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')
文件存储后显示如下(地图示例):
# Array shape: (480, 640)
372
170
992
823
621
452
282
113
935
....
86
193
# New slice
274
203
131
92
21
1006
935
....
我用这个 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)
一切接缝工作正常,但是当我使用这些地图进行不失真 function
# 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'
我试着按照这里的问题,但我没能理解错误,有什么帮助吗?
正如错误消息所说,您重新映射的输入需要具有正确的类型。 当您使用CV_16SC2
时,对应的 numpy 类型为np.int16
。 假设所有值都被正确读取,您可以使用mapx.astype(np.int16)
和mapy.astype(np.int16)
来转换 arrays。
您应该考虑保存相机矩阵而不是变换贴图。 如果您想稍后在initUndistortRectifyMap()
中使用不同的选项(例如,如果您决定更改图像分辨率)或想要计算其他需要知道内在参数的东西,这将很有用。 相机矩阵也比变换图小很多。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.