python中的Opencv折线函数抛出异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17241830/
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 00:50:43  来源:igfitidea点击:

Opencv polylines function in python throws exception

pythonopencvnumpypoints

提问by Ashok

I'm trying to draw an arbitrary quadrilateral over an image using the polylines function in opencv. When I do I get the following error

我正在尝试使用 opencv 中的折线函数在图像上绘制任意四边形。当我这样做时,我收到以下错误

OpenCV Error: Assertion failed (p.checkVector(2, CV_32S) >= 0) in polylines, file /tmp/buildd/ros-fuerte-opencv2-2.4.2-1precise-20130312-1306/modules/core/src/d rawing.cpp, line 2065

OpenCV 错误:折线中的断言失败(p.checkVector(2, CV_32S) >= 0),文件 /tmp/buildd/ros-fuerte-opencv2-2.4.2-1precise-20130312-1306/modules/core/src/d rawing.cpp,第 2065 行

I call the function as like so,

我像这样调用函数,

cv2.polylines(img, points, 1, (255,255,255))

Where points is as numpy array as shown below (The image size is 1280x960):

其中点是如下所示的numpy数组(图像大小为1280x960):

[[910 641]
 [206 632]
 [696 488]
 [458 485]]

and img is just a normal image that I'm able to imshow. Currently I'm just drawing lines between these points myself, but I'm looking for a more elegant solution.

和 img 只是我能够显示的普通图像。目前我只是自己在这些点之间画线,但我正在寻找更优雅的解决方案。

How should I correct this error?

我应该如何纠正这个错误?

采纳答案by Vanuan

The problem in my case was that numpy.arraycreated int64-bit numbers by default. So I had to explicitly convert it to int32:

我的问题是默认情况下numpy.array创建了int64-bit 数字。所以我必须明确地将其转换为int32

points = np.array([[910, 641], [206, 632], [696, 488], [458, 485]])
# points.dtype => 'int64'
cv2.polylines(img, np.int32([points]), 1, (255,255,255))

(Looks like a bug in cv2 python binding, it should've verified dtype)

(看起来像是 cv2 python 绑定中的一个错误,应该已经验证过dtype

回答by jabaldonedo

This function is not enough well documented and the error are also not very useful. In any case, cv2.polylinesexpects a list of points, just change your line to this:

这个函数没有足够的文档记录,错误也不是很有用。在任何情况下,都cv2.polylines需要一个点列表,只需将您的行更改为:

import cv2
import numpy as np

img = np.zeros((768, 1024, 3), dtype='uint8')

points = np.array([[910, 641], [206, 632], [696, 488], [458, 485]])
cv2.polylines(img, [points], 1, (255,255,255))

winname = 'example'
cv2.namedWindow(winname)
cv2.imshow(winname, img)
cv2.waitKey()
cv2.destroyWindow(winname)

The example above will print the following image (rescaled):

上面的示例将打印以下图像(重新缩放):

enter image description here

在此处输入图片说明

回答by Abid Rahman K

the error says your array should be of dimension 2. So reshape the array as follows:

错误说你的数组应该是维度 2。所以按如下方式重塑数组:

points = points.reshape(-1,1,2)

Then it works fine.

然后它工作正常。

Also, answer provided by jabaldonedo also works fine for me.

另外,jabaldonedo 提供的答案对我来说也很好用。

回答by xm L

pts = np.array([[40,300],[54,378],[60,420],[30,333]],np.int32) 
pts = pts.reshape((-1,1,2))
img = cv2.polylines(img,pts,True,(125,215,145),1)

The official documentation to provide explanation,need reshape

官方文档提供解释,需要reshape

回答by Ranjeet Singh

Replace cv2.fillPoly( im, np.int32(points))with cv2.fillPoly( im, np.int32([points])). It will work.

替换cv2.fillPoly( im, np.int32(points))cv2.fillPoly( im, np.int32([points]))。它会起作用。

回答by Aakarshan

I also faced the same problem The solution is to make a array of 1 row, 2 columns and - 1 depth, - 1 means unknown dimensions so numpy will assign convenient depth to the array. If you make array of more than 1 row and 2 columns it will show error.

我也遇到了同样的问题 解决方案是制作一个 1 行 2 列和 - 1 深度的数组, - 1 表示未知维度,因此 numpy 将为数组分配方便的深度。如果您制作超过 1 行和 2 列的数组,它将显示错误。

Also this error can come when the array you created is not of type int32

当您创建的数组不是 int32 类型时,也会出现此错误

Vertices = np.array([[36,86] ,[73,73], [87,87]], dtype=np.int32)