Convert NumPy array to cvMat cv2

匿名 (未验证) 提交于 2019-12-03 02:48:02

问题:

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:

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).



回答2:

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



回答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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!