gpt4 book ai didi

python - Python显示用于查找轮廓的OpenCV断言错误

转载 作者:行者123 更新时间:2023-12-02 17:43:09 25 4
gpt4 key购买 nike

我试图使用Python OpenCV在图像中绘制轮廓。我的代码如下:

import numpy as np
import cv2
import Image
a = cv2.imread('train.jpg')
b = cv2.cvtColor(a, cv2.COLOR_BGR2GRAY)
ret, c = cv2.threshold(b, 127, 255, cv2.THRESH_BINARY)
contours, h = cv2.findContours(c, 1, 2)
d = cv2.drawContours(a, contours, -1, (128, 255, 0), 1)
cv2.imshow('abs', d)
cv2.waitKey(0)

编译此代码时出现错误。错误如下:
OpenCV Error: Assertion failed (size.width>0 && size.height>0) in imshow, file /home/travis/miniconda/conda-bld/work/opencv-2.4.11/modules/highgui/src/window.cpp, line 261
Traceback (most recent call last):
File "shape.py", line 9, in <module>
cv2.imshow('abs',d)
cv2.error: /home/travis/miniconda/conda-bld/work/opencv-2.4.11/modules/highgui/src/window.cpp:261: error: (-215) size.width>0 && size.height>0 in function imshow

如何避免此错误?

最佳答案

问题应该是cv2.drawContours(以及python中的所有opencv“绘图”函数)的输出等于“无”。尝试这种方式:

cv2.drawContours(a, contours, -1, (128, 255, 0), 1)
cv2.imshow('abs', a)
cv2.waitKey(0)

关于python - Python显示用于查找轮廓的OpenCV断言错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41438251/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com