I was trying to draw contours in an image using Python OpenCV. My code is the following:
JavaScript
x
11
11
1
import numpy as np
2
import cv2
3
import Image
4
a = cv2.imread('train.jpg')
5
b = cv2.cvtColor(a, cv2.COLOR_BGR2GRAY)
6
ret, c = cv2.threshold(b, 127, 255, cv2.THRESH_BINARY)
7
contours, h = cv2.findContours(c, 1, 2)
8
d = cv2.drawContours(a, contours, -1, (128, 255, 0), 1)
9
cv2.imshow('abs', d)
10
cv2.waitKey(0)
11
I am getting an error while compiling this code. The error is the following:
JavaScript
1
6
1
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
2
Traceback (most recent call last):
3
File "shape.py", line 9, in <module>
4
cv2.imshow('abs',d)
5
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
6
How can I avoid this error?
Advertisement
Answer
The problem should be that cv2.drawContours (and in general all opencv “drawing” functions in python) have output equal to None. Try this way:
JavaScript
1
4
1
cv2.drawContours(a, contours, -1, (128, 255, 0), 1)
2
cv2.imshow('abs', a)
3
cv2.waitKey(0)
4