0

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?

4
  • Perhaps try converting the image with np.asarray(img) and see if that works Commented Jan 14, 2021 at 0:30
  • It worked! Thanks! Commented Jan 14, 2021 at 2:48
  • No worries :) I wrote up my comment as an answer with a bit more detail Commented Jan 14, 2021 at 4:02
  • I just saw the image and its a 1D array lol Commented Jan 14, 2021 at 4:04

1 Answer 1

0

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))
Sign up to request clarification or add additional context in comments.

2 Comments

It worked for viewing it, but now it's just a 1D array.
Looks like we need to reshape. You can call np.array(img).reshape((y, x)) on the array to convert it back to 2d, with y and x being the dimensions. I'd also suggest using np.array(img) instead of np.asarray(img) so the original img is copied. See if that works

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.