使用来自 OpenMV 相机的图像的 OpenCV imwrite 方法的问题

Problems using OpenCV imwrite method with image from OpenMV camera

我正在尝试使用 OpenCV 的拼接算法拼接从我的 OpenMV H7 相机拍摄的一些图像。我 运行 遇到无法写入或读取这些图像的问题,这让我认为存在一些兼容性问题。

更确切地说,我在使用方法 (cv2.imwrite) 本身时遇到了这个错误:

  File "main_script_test.py", line 141, in <module>
    cv2.imwrite("/Documents/Cam/Images/image_" + str(images_To_Be_Taken), img)
TypeError: Expected Ptr<cv::UMat> for argument 'img'

我一直在想,也许有一种方法可以将图像转换为 NumPy 数组以使其兼容,但我不太确定。

有什么建议吗?

OpenCV's imwrite 需要一个 Mat 对象,它是一个“n 维密集数组 class”。传入图像数据的 numpy 数组代替 Mat 对象,至少在 imwrite() 的情况下,将产生正确的结果。

来自上面链接的文档:

So, the data layout in Mat is compatible with the majority of dense array types from the standard toolkits and SDKs, such as Numpy (ndarray), Win32 (independent device bitmaps), and others, that is, with any array that uses steps (or strides) to compute the position of a pixel. Due to this compatibility, it is possible to make a Mat header for user-allocated data and process it in-place using OpenCV functions.

对于您的代码:

cv2.imwrite("/Documents/Cam/Images/image_" + str(images_To_Be_Taken), np.asarray(img))