J.Doe
J.Doe

Reputation: 39

Python3 - OpenCV & cv2.error: (-215) Unable to show captured image

I am trying to using opencv-3.3.0, cv2 & python3.5.

But, I can't seem to show image I have captured.

What am I missing?

Code:

import numpy as np
import cv2

img=cv2.imread("F:/Train/sreen.png")
cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

The Error:

OpenCV Error: Assertion failed (size.width>0 && size.height>0) in cv::imshow, file D:\Build\OpenCV\opencv-3.3.0\moules\highgui\src\window.cpp, line 333 
Traceback (most recent call last):
File "F:\IQ_option\OpenCV\run.py", line 5, in <module>
    cv2.imshow('image',img)
cv2.error: D:\Build\OpenCV\opencv-3.3.0\modules\highgui\src\window.cpp:333: error: (-215) size.width>0 && size.height>0 in function cv::imshow

The image:

My Code.

Upvotes: 2

Views: 63218

Answers (6)

CodeHunter
CodeHunter

Reputation: 2082

The only reason for this to not work is that the file you are trying here isn't loaded properly in the imread command. The loaded image is non existent due to which it says that size.width and size.height > 0.

You can check if either the file is present in your specified location or not. Also, you might need to use double \\ while providing the full path of the .png file you want to display. Also, it might be that .png files are not supported here. You may try any other .jpg image file instead and retry. An example would be like this:

img = cv2.imread('C:\\Users\\fakepath\\Pictures\\Messi.jpg', cv2.IMREAD_COLOR)

Worked for me.

Upvotes: 8

Abinet KenoreGetachew
Abinet KenoreGetachew

Reputation: 149

Make sure your file path is correct. I misspelled the file(img = cv2.imread('Images/ab.png',0) instead of img = cv2.imread('Images/abi.png',0)) and I was getting the same error:

.......cv2.error: OpenCV(4.1.0) /Users/travis/build/skvark/opencv-python/opencv/modules/core/src/matrix.cpp:757: error: (-215:Assertion failed) dims <= 2 && step[0] > 0 in function 'locateROI'.

It may be this much simple.

Upvotes: -1

Eyup Hasan Akbaba
Eyup Hasan Akbaba

Reputation: 1

i got the same mistake.I tried a lot of things to solve the problem.But afterwards i have realised that problem was about the path. (enter image description hereDo not use path which has foreign letters)

Upvotes: 0

I previously encountered the same problem. Then I realized that my image file path is misspelled. In my code, an image file was named "note.jpg" and in my directory, it was "note.jpg.jpg" and instead of "j.jpg" I wrote it as "j.png". After recorrecting them, everything went well.

Upvotes: 1

Sevak Kazarian
Sevak Kazarian

Reputation: 1

If you are working in an environment like jupyter with ipython then you have to check if :

  1. If the image path is correct
  2. If the image is not empty

If you have a correct path and your image is not empty then you have maybe installed many times OpenCv (i had the same problem) and notebook is been confused. The solution is simple.

  • Find where does your notebook store its downloads(for me it was C:/Users/MyUser/AppData/Roaming/Python/Python36....)
  • Delete cv2 file and everything related with opencv
  • Open a terminal using the one built-in in your notebook 1

-Install opencv-python from scratch by typing : pip install opencv-python -If its needed add --user at the end of the command so you can act like an admin - Reboot your notebook and it should be fine. I had the same problem and it worked fine for me

Upvotes: 0

user11455554
user11455554

Reputation: 11

import os
os.environ['OPENCV_IO_MAX_IMAGE_PIXELS']=str(2**64)
import cv2 

Before importing cv2, you should set environ to solve the error. It works for me.

Upvotes: 1

Related Questions