Arguments to cv2::imshow


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
Out: numpy.ndarray  #dtype is uint8

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

plt.imshow(cvimage)  # works great

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/17/2019 5:35:05 PM

Accepted Answer

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.

1/5/2019 10:43:54 AM

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

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow