Python OpenCV TypeError:参数“src”的预期 cv::UMat - 这是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/54249728/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 20:27:32  来源:igfitidea点击:

OpenCV TypeError: Expected cv::UMat for argument 'src' - What is this?

pythonopencvtypeerrorsrc

提问by autonocat

Disclaimer: huge openCV noob

免责声明:巨大的 openCV 菜鸟

Traceback (most recent call last):

File "lanes2.py", line 22, in

canny = canny(lane_image)

File "lanes2.py", line 5, in canny

gray = cv2.cvtColor(imgUMat, cv2.COLOR_RGB2GRAY)

TypeError: Expected cv::UMat for argument 'src'

回溯(最近一次调用最后一次):

文件“lanes2.py”,第 22 行,在

canny = canny(lane_image)

文件“lanes2.py”,第 5 行,canny

gray = cv2.cvtColor(imgUMat, cv2.COLOR_RGB2GRAY)

类型错误:参数“src”的预期 cv::UMat

What exactly is 'src' referring to?

“src”究竟指的是什么?

回答by Varun Mathur

srcis the first argument to cv2.cvtColor.

src是 的第一个参数cv2.cvtColor

The error you are getting is because it is not the right form. cv2.Umat()is functionally equivalent to np.float32(), so your last line of code should read:

你得到的错误是因为它不是正确的形式。cv2.Umat()在功能上等同于np.float32(),因此您的最后一行代码应为:

gray = (np.float32(imgUMat), cv2.COLOR_RGB2GRAY)

回答by Nuzhny

gray = cv2.cvtColor(cv2.UMat(imgUMat), cv2.COLOR_RGB2GRAY)

UMat is a part of the Transparent API (TAPI)than help to write one code for the CPU and OpenCL implementations.

UMat 是透明 API (TAPI)的一部分,有助于为 CPU 和 OpenCL 实现编写代码。

回答by Avi Avidan

The following can be used from numpy:

可以从以下使用numpy

import numpy as np 
image = np.array(image)

回答by Maximilian

Not your code is the problem this is perfectly fine:

不是你的代码是问题,这完全没问题:

gray = cv2.cvtColor(imgUMat, cv2.COLOR_RGB2GRAY)

The problem is that imgUMat is Noneso you probably made a mistake when loading your image:

问题是 imgUMat 是None这样,您可能在加载图像时犯了一个错误:

imgUMat = cv2.imread("your_image.jpg")

I suspect you just entered the wrong image path.

我怀疑您刚刚输入了错误的图像路径。

回答by Daweo

Is cannyyour own function? Do you use Cannyfrom OpenCV inside it? If yes check if you feed suitable argument for Canny- first Cannyargument should meet following criteria:

canny你自己的功能吗?你在里面使用OpenCV 的Canny吗?如果是,请检查您是否提供了合适的参数Canny- 第一个Canny参数应满足以下条件:

  • type: <type 'numpy.ndarray'>
  • dtype: dtype('uint8')
  • being single channel or simplyfing: grayscale,that is 2D array, i.e. its shapeshould be 2-tupleof ints (tuplecontaining exactly 2 integers)
  • 类型: <type 'numpy.ndarray'>
  • 数据类型: dtype('uint8')
  • 为单信道或simplyfing:灰度,即2D阵列,即,其shape应该是2-tupleintS(tuple正好含有2个整数)

You can check it by printing respectively

可以分别打印查看

type(variable_name)
variable_name.dtype
variable_name.shape

Replace variable_namewith name of variable you feed as first argument to Canny.

替换variable_name为您作为第一个参数提供的变量名称Canny

回答by Hazarapet Tunanyan

This is a general error, which throws sometimes, when you have mismatchbetween the types of the data you use. E.g I tried to resize the image with opencv, it gave the same error. Hereis a discussion about it.

这是一个一般性错误,有时会在您使用的数据类型不匹配时引发。例如,我尝试使用 opencv 调整图像大小,它给出了相同的错误。是关于它的讨论。

回答by Oysiyl

Sometimes I have this error when videostreamfrom imutilspackage doesn't recognize frame or give an empty frame. In that case, solution will be figuring out why you have such a bad frame or use a standard VideoCapture(0) method from opencv2

有时,当来自imutils包的视频流无法识别帧或给出空帧时,我会遇到此错误。在这种情况下,解决方案将找出为什么您有如此糟糕的帧或使用来自 opencv2 的标准 VideoCapture(0) 方法

回答by Amar Sagat

Just add this at start: image = cv2.imread(image)

只需在开始时添加: image = cv2.imread(image)

回答by Geo

I got round thid by writing/reading to a file. I guessed cv.imread would put it into the format it needed. This code for anki Vector SDK program but you get the idea.

我通过写入/读取文件来解决问题。我猜 cv.imread 会把它变成它需要的格式。这段代码用于 anki Vector SDK 程序,但您明白了。

tmpImage = robot.camera.latest_image.raw_image.save('temp.png')
pilImage = cv.imread('temp.png')

tmpImage = robots.camera.latest_image.raw_image.save('temp.png')
pilImage = cv.imread('temp.png')

回答by Oliver Zendel

Some dtype are not supported by specific OpenCV functions. For example inputs of dtype np.uint32 create this error. Try to convert the input to a supported dtype (e.g. np.int32 or np.float32)

特定的 OpenCV 函数不支持某些 dtype。例如 dtype np.uint32 的输入会创建此错误。尝试将输入转换为支持的 dtype(例如 np.int32 或 np.float32)