An error occurred when Python calculated the camera response function and stored it in a txt file. The source code is as follows:
# 估计相机响应函数曲线
calibrateDebevec = cv2.createCalibrateDebevec()
responseDebevec = calibrateDebevec.process([img1, img2, img3], times=np.array([1, 2, 4], dtype=np.float32))
np.savetxt('response_curve.txt', responseDebevec)
Display error:
Traceback (most recent call last):
File "D:\HApp\Deep\Pycharm-Community\PycharmProjects\pythonProject17\RANDOM_test1111\matttt.py", line 27, in <module>
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
This error is because np.savetxt()
the function only accepts 1D or 2D arrays as input, but responseDebevec
a 3D array.
responseDebevec
is an array composed of the response values of each pixel under multiple exposure times, and its dimension is (image height, image width, number of exposure times).
To responseDebevec
save as a text file, you need to convert it to a 2D array. A can be converted to a 2D array using np.reshape()
the function responseDebevec
. Modify the code as follows:
# 将 responseDebevec 转换为2D数组
response_2d = responseDebevec.reshape(-1, responseDebevec.shape[-1])
# 将2D数组保存为文本文件
np.savetxt('response_curve.txt', response_2d)
The result is correct.
D:\HApp\Deep\Pycharm-Community\PycharmProjects\pythonProject17\RANDOM_test1111\matttt.py
Process finished with exit code 0
The txt file that needs to be stored is successfully obtained.