Python计算相机响应函数并存储至txt文件时发生错误,源代码如下:
# 估计相机响应函数曲线
calibrateDebevec = cv2.createCalibrateDebevec()
responseDebevec = calibrateDebevec.process([img1, img2, img3], times=np.array([1, 2, 4], dtype=np.float32))
np.savetxt('response_curve.txt', responseDebevec)
显示错误:
Traceback (most recent call last):
File "D:\HApp\Deep\Pycharm-Community\PycharmProjects\pythonProject17\RANDOM_test1111\matttt.py", line 27, in
np.savetxt('response_curve.txt', responseDebevec)
File "<__array_function__ internals>", line 180, in savetxt
File "D:\HApp\Deep\anaconda3\envs\python38_64\lib\site-packages\numpy\lib\npyio.py", line 1397, in savetxt
raise ValueError(
ValueError: Expected 1D or 2D array, got 3D array instead
这个错误是因为 np.savetxt()
函数只接受1D或2D数组作为输入,而 responseDebevec
是一个3D数组。
responseDebevec
是一个由多个曝光时间下的每个像素的响应值组成的数组,它的维度是(图像高度,图像宽度,曝光时间数目)。
要将 responseDebevec
保存为文本文件,你需要将其转换为2D数组。可以使用 np.reshape()
函数将 responseDebevec
转换为2D数组。修改代码如下:
# 将 responseDebevec 转换为2D数组
response_2d = responseDebevec.reshape(-1, responseDebevec.shape[-1])
# 将2D数组保存为文本文件
np.savetxt('response_curve.txt', response_2d)
结果正确。
D:\HApp\Deep\Pycharm-Community\PycharmProjects\pythonProject17\RANDOM_test1111\matttt.py
Process finished with exit code 0
成功获得所需存储的txt文件。