尝试使用 opencv fillPoly 将多边形添加到图像时出错

Error while trying to add polygon onto an image using opencv's fillPolly

我正在尝试使用以下代码向图像添加三角形多边形

import cv2
import numpy as np

// image here is a dummy image say 700*400 pixel one
triangle = np.array([(100, 20), (500, 20), (350, 180)], dtype=np.int32)
mask_image = np.zeros_like(image)
cv2.fillPoly(mask_image, triangle, 255)

我提到了其他一些答案,他们说 np.array 将使用 dtype int64 创建元素,因此我明确更改为 int32。仍然没有运气,我得到了同样的错误。这是错误,

Traceback (most recent call last):

  File "<ipython-input-1-a13585bb05c1>", line 1, in <module>
    runfile('/home/niranjrajasekaran/Documents/personal/selfcar/lane_finder.py', wdir='/home/niranjrajasekaran/Documents/personal/selfcar')

  File "/home/niranjrajasekaran/.virtualenvs/selfcar/lib/python3.7/site-packages/spyder_kernels/customize/spydercustomize.py", line 827, in runfile
    execfile(filename, namespace)

  File "/home/niranjrajasekaran/.virtualenvs/selfcar/lib/python3.7/site-packages/spyder_kernels/customize/spydercustomize.py", line 110, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "/home/niranjrajasekaran/Documents/personal/selfcar/lane_finder.py", line 37, in <module>
    roi = region_of_interest(canny_image)

  File "/home/niranjrajasekaran/Documents/personal/selfcar/lane_finder.py", line 30, in region_of_interest
    cv2.fillPoly(mask_image, triangle, 255)

error: OpenCV(4.1.1) /io/opencv/modules/imgproc/src/drawing.cpp:2403: error: (-215:Assertion failed) p.checkVector(2, CV_32S) >= 0 in function 'fillPoly'

注意:我使用的是 OpenCV 4.1.1 和 python 3.6.3

是我的错,我没有将 [] 添加到导致此错误的 numpy 数组

triangle = np.array([ [(100, 20), (500, 20), (350, 180)] ], dtype=np.int32)

注意:即使转换 dtype=np.int32 也不需要