0

我试图使用 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

我怎样才能避免这个错误?

4

1 回答 1

1

问题应该是 cv2.drawContours (以及通常 python 中的所有 opencv“绘图”函数)的输出等于 None。试试这个方法:

cv2.drawContours(a, contours, -1, (128, 255, 0), 1)
cv2.imshow('abs', a)
cv2.waitKey(0) 
于 2017-01-03T08:25:42.447 回答