python中的Opencv折线函数引发异常 [英] Opencv polylines function in python throws exception

查看:317
本文介绍了python中的Opencv折线函数引发异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用opencv中的折线功能在图像上绘制任意四边形.当我收到以下错误消息

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

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

我这样调用函数

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

点是如下所示的numpy数组(图像尺寸为1280x960):

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

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

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

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.

我应该如何纠正此错误?

How should I correct this error?

推荐答案

我的问题是numpy.array默认情况下会创建int64位数字.所以我必须将其显式转换为int32:

The problem in my case was that numpy.array created int64-bit numbers by default. So I had to explicitly convert it to 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))

(看起来像cv2 python绑定中的错误,应该已经验证了dtype)

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

这篇关于python中的Opencv折线函数引发异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆