14

Edit: original title "convert numpy array to cvmat" was a mistake - OpenCV's less than useful error message and my not reading the docs!

With OpenCV 2, IPython now uses NumPy arrays by default.

cvimage = cv2.imread("image.png") #using OpenCV 2
type(cvimage)
Out: numpy.ndarray  #dtype is uint8

pltimage = plt.imread("image.png")  #using Matplotlib
type(pltimage)
Out: numpy.ndarray   #dtype is float

plt.imshow(cvimage)  # works great

cv2.imshow(cvimage)
TypeError: Required argument 'mat' (pos 2) not found

Since cv2 uses NumPy arrays by default, there is no longer any cv::Mat constructor and NumPy has no functions to convert to a cv::Mat array.

Any ideas?

1
  • 1
    Yes, this has already been answered - you're actually not missing the second argument, but the first. However, I came here with another issue: not every numpy.ndarray of type uint8 can be displayed. I made a slice, to get a single channel of an image, like red = frame[:,:,0], and then got an exception in imshow. I suspect it's because of sparsity. red = frame[:,:,0].copy() fixed that. Jan 2, 2015 at 14:39

3 Answers 3

20

The function has the following docstring: imshow(winname, mat) -> None. You can see the doc string by typing cv2.imshow.__doc__ in the interpreter.

Try cv2.imshow('Image', cvimage).

tl;dr : In original question, first argument of "window name" was missing. "imshow" takes two parameters and only one was supplied.

4
  • 2
    When I do cvimage = cv2.imread('image.png') followed by cv2.imshow('Image', cvimage), no errors are returned and the image is displayed.
    – zarthur
    Mar 28, 2012 at 18:55
  • Without importing numpy, they type returned is numpy.ndarray.
    – zarthur
    Mar 28, 2012 at 19:10
  • 1
    you're missing a required arg
    – Neon22
    Mar 30, 2012 at 11:20
  • 1
    As Neon22 said, you are missing the first argument to the imshow function which is the name of the window to display the image in. imshow works fine with images represented as numpy arrays. Aug 13, 2012 at 7:10
15

The question technically asks how to convert a NumPy Array (analogous to CV2 array) into a Mat object (CV). For anyone who is interested, this can be done by:

mat_array = cv.fromarray(numpy_array)

where mat_array is a Mat object, and numpy_array is a NumPy array or image. I would suggest staying away from older CV structures where possible. Numpy arrays offer much better performance than implemenations in native Python

2
  • 12
    This is for cv not cv2
    – G M
    Apr 8, 2016 at 10:43
  • 1
    How should we do this in C++ and for cv2?
    – Mona Jalal
    Jun 13, 2017 at 1:16
3

Mat object was needed because C/C++ lacked a standard/native implementation of matrices.

However, numpy's array is a perfect replacement for that functionality. Hence, the cv2 module accepts numpy.arrays wherever a matrix is indicated in the docs.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.