【问题标题】:Opencv polylines function in python throws exceptionpython中的Opencv折线函数抛出异常
【发布时间】:2013-06-18 23:22:21
【问题描述】:

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

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 行

我这样调用函数,

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

其中points为numpy数组,如下图(图片大小为1280x960):

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

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

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

【问题讨论】:

  • cv2.polylines(img, np.reshape(p, (1, *p), 1, (255,255,255))

标签: python opencv numpy points


【解决方案1】:
import cv2
import numpy as np

sz, sh, of = 1000, 500, 100

# Create an Empty image with white background
im = 255 * np.ones(shape=[sz, sz, 3], dtype=np.uint8)

# Draw shapes
im = cv2.polylines(
    img=im,
    pts=[np.int32([
        [of, of], 
        [sh, of + of], 
        [sz - of, of],
        [sz-of-of,sh],
        [sz-of,sz-of],
        [sh,sz-of-of],
        [of,sz-of],
        [of+of,sh]])],
    isClosed=True,
    color=(128, 0, 200),
    thickness=30,
    lineType=cv2.LINE_AA,  # Anti-Aliased
)

cv2.imwrite("polylines.jpg", im)

【讨论】:

    【解决方案2】:

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

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

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:
        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)
        

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

        【讨论】:

          【解决方案5】:

          我的问题是 numpy.array 默认创建了 int64 位数。所以我不得不将其显式转换为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

          【讨论】:

          • 你也可以使用np.array(..., dtype=np.int32)
          • 这个工作的原因不是因为类型转换,而是因为您将点转换为点数组。第二个 c++ arg 是数组的输入数组
          • 其实我说得太早了。类型也很重要。如果您不传入 32 位 int,则会在折线中检查 checkVector(2, CV_32S)。
          【解决方案6】:

          错误说你的数组应该是维度 2。所以重新调整数组如下:

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

          然后它工作正常。

          另外,jabaldonedo 提供的答案对我也很有效。

          【讨论】:

            【解决方案7】:

            这个函数的文档记录不够好,错误也不是很有用。无论如何,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)
            

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

            【讨论】:

            • 我试过了,但我仍然得到同样的错误。这就是点的参数现在的样子:[array([[902, 636], [222, 625], [458, 485], [696, 488]])]
            • 我已经更新了代码,你可以看到代码运行良好。
            • 你给出的示例程序给了我同样的错误。 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/drawing.cpp, line 2065可能是我的版本有问题!!
            • 为了使它工作,我必须将 [points] 转换为 int32:cv2.polylines(img, np.int32([points]), 1, (255,255,255))
            • [points] 也为我解决了这个问题,我的points 已经在int32 中了。
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-03-15
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2022-07-27
            • 2018-05-04
            相关资源
            最近更新 更多