Problems using OpenCV imwrite method with image from OpenMV camera

182 views Asked by At

I am trying to stitch some images taken from my OpenMV H7 camera using OpenCV's stitching algorithm. I ran into the problem that I cannot write or read these images, which made me think that there are some compatibility issues.

To be more exact, I got this error when using the method (cv2.imwrite) itself:

  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'

I have been thinking that maybe there is a way I can turn the image into a NumPy array to make it compatible, but I am not quite sure.

Any suggestions?

1

There are 1 answers

2
PeptideWitch On BEST ANSWER

OpenCV's imwrite is expecting a Mat object, which is an "n-dimensional dense array class". Passing in a numpy array of the image data in place of Mat objects, in the case of imwrite() at least, will yield the correct result.

From the documentation linked above:

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.

For your code:

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