0

I am getting an error while running the following code.

import cv2
import numpy as np
img = cv2.imread('messi.jpg',0)
img = cv2.line(img,(0,0),(50,50),(255,0,0),5)
cv2.imshow("image",img)
cv2.waitKey(0)
cv2.destroyAllWindows()

The error says:

OpenCV Error: Assertion failed (size.width>0 && size.height>0) in unknown function, file ......\src\opencv\modules\highgui\src\window.cpp, line 261

Traceback (most recent call last):

File "F:\Computer programming\scripts\OpenCv\1.py", line 6, in cv2.imshow("image",img) cv2.error: ......\src\opencv\modules\highgui\src\window.cpp:261: error: (-215) size.width>0 && size.height>0

If I remove the line:

img = cv2.line(img,(0,0),(50,50),(255,0,0),5)

the script works.

1
  • The message says your img is empty. Your file 'messi.jpg' is not in your working directory. You likely need to specify a proper path to it. Commented Dec 31, 2022 at 0:28

1 Answer 1

4

Its because cv2.line returns None and you are assigning that to your img variable. So when you get to the next line and try to show the image, there is no image to be shown.

Replace img = cv2.line(img,(0,0),(50,50),(255,0,0),5) with cv2.line(img,(0,0),(50,50),(255,0,0),5)

Read more about cv2.circle here.

Sign up to request clarification or add additional context in comments.

1 Comment

The tutorial should be corrected Opencv drawing options

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.