[英]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! 编辑:原始标题“将numpy数组转换为cvmat”是一个错误-OpenCV的不到有用的错误消息,我不阅读文档!
With OpenCV 2, IPython now uses NumPy arrays by default. 在OpenCV 2中,默认情况下, IPython现在使用NumPy数组。
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. 由于默认情况下
cv2
使用NumPy数组,因此不再有任何cv :: Mat构造函数,并且NumPy没有函数可以转换为cv::Mat
数组。
Any ideas? 有任何想法吗?
The function has the following docstring: imshow(winname, mat) -> None
. 该函数具有以下文档字符串:
imshow(winname, mat) -> None
。 You can see the doc string by typing cv2.imshow.__doc__
in the interpreter. 您可以通过在解释器中输入
cv2.imshow.__doc__
来查看文档字符串。
Try cv2.imshow('Image', cvimage)
. 尝试
cv2.imshow('Image', cvimage)
。
tl;dr : In original question, first argument of "window name" was missing. tl; dr:在原始问题中,缺少“窗口名称”的第一个参数。 "imshow" takes two parameters and only one was supplied.
“ imshow”有两个参数,仅提供了一个。
The question technically asks how to convert a NumPy Array (analogous to CV2 array) into a Mat object (CV). 从技术上讲,该问题询问如何将NumPy数组(类似于CV2数组)转换为Mat对象(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. 其中mat_array是Mat对象,而numpy_array是NumPy数组或图像。 I would suggest staying away from older CV structures where possible.
我建议尽可能远离旧的简历结构。 Numpy arrays offer much better performance than implemenations in native Python
Numpy数组提供的性能比本机Python中的实现好得多
Mat
object was needed because C/C++ lacked a standard/native implementation of matrices. Mat
对象是必需的,因为C / C ++缺少矩阵的标准/本机实现。
However, numpy
's array
is a perfect replacement for that functionality. 但是,
numpy
的array
是该功能的完美替代。 Hence, the cv2
module accepts numpy.array
s wherever a matrix is indicated in the docs. 因此,
cv2
模块在文档中指示矩阵的任何地方都接受numpy.array
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.