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

标签 python opencv

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

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

关于python - 尝试使用 opencv fillPoly 将多边形添加到图像上时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58878425/

相关文章:

opencv - 在WinCE07平台上移植OpenCV

opencv - 错误:找不到符号CV_32FC3

python - 如何在 PyQt 中将 QPointf 转换为 QPoint

python - Flask-reST 少使用分页或获得完整响应

python - matplotlib 中的最后一个图形标记未完全显示

python - 逐像素二维辐射校准

android - 对 `cv::fastFree(void*)' 的 undefined reference

python :如何删除 '$' ?

python - Plotly:如何为 create_displot 图添加均值和标准差?

c++ - 将 OpenCV 灰度 Mat 转换为 Caffe blob