I am trying to run a simple program that reads an image from OpenCV. However, I am getting this error:
error: ......\\modules\\highgui\\src\\window.cpp:281:
import numpy as np
import cv2
img=cv2.imread('E:\itsme\Camera\pic.jpg',10)
cv2.imshow('image',img)
cv2.waitkey(0)
cv2.destroyallwindows()
just add fill the full directory of your picture in string.
Make sure you have given the correct path of image. This error comes only when you have given wrong path.
This error occurs when you are trying to show an empty image. The image was probably not loaded correctly and could be due to a non-existent image, incorrect file path, or incorrect file extension when using cv2.imread()
. You can check if the image was loaded properly by printing out its shape
image = cv2.imread('picture.png')
print(image.shape)
You will get something like this which returns a tuple of the number of rows, columns, and channels
(400, 391, 3)
If OpenCV was unable to load the image, it will throw this error when trying to print the shape
AttributeError: 'NoneType' object has no attribute 'shape'
my problem was the same while i found the endswitch of jpg - it was jpEg. )
I got the same error on Windows. However, none of answers helps me. I realised that the issue is single backslash (\
) instead of double backslashes (\\
) on image path. For the difference, see the link.
Problematic scenario:
import cv2
mypath = "D:\temp\temp.png"
img = cv2.imread(mypath, 1)
cv2.imshow('test', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Traceback (most recent call last):
File "<input>", line 1, in <module>
cv2.error: OpenCV(4.4.0) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-2b5g8ysb\opencv\modules\highgui\src\window.cpp:376: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'cv::imshow'
Fixed scenario:
import cv2
mypath = "D:\\temp\\temp.png"
img = cv2.imread(mypath, 1)
cv2.imshow('test', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
I hope it helps others who have similar experiences.
This error shows when either,
We can check it by using 'print(img)
' command after 'img = cv2.imread('C:\\Utilisateurs\\Zeineb\\Bureau\\image.jpg',0)
' if output is 'None' then either path or name of the image is wrong. If output is matrix then image read is successful.
In code 'cv2.waitKey(0)
' zero shows the millisecond for which image will appear on the screen so it should be greater than zero something like 5000.