0

I was trying to draw contours in an image using Python OpenCV. My code is the following:

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)

I am getting an error while compiling this code. The error is the following:

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

How can I avoid this error?

2
  • This is a Q&A site. Thanks in advance are superfluous, but including a question is not. there is an icon {} above the text when you edit. It allows you to format code in a readable way, please use it when posting. Commented Jan 3, 2017 at 7:31
  • Well it is hard for me to see what is wrong exactly. But the error is an assertion error the assertion is size.width>0 && size.height>0. So in order to "imshow" an image you need an image whose width and height are greater than 0. This could be the result of opencv is unable to read train.jpg or you are not using one of the other characters correctly. Commented Jan 3, 2017 at 8:02

1 Answer 1

1

The problem should be that cv2.drawContours (and in general all opencv "drawing" functions in python) have output equal to None. Try this way:

cv2.drawContours(a, contours, -1, (128, 255, 0), 1)
cv2.imshow('abs', a)
cv2.waitKey(0) 
Sign up to request clarification or add additional context in comments.

1 Comment

@Favas you're welcome. Please consider to accept the answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.