[Solved]OpenCV error: (-215:Assertion failed), read and display an image [closed]
Hey everyone,
previously I was able to use the OpenCV but after I did system restore, I am not able to use it anymore. I have Ubuntu 18.04 installed on WSL and working on ROS. There are two versions of OpenCV installed:
- 4.3.0-dev (when I check the version via python: >>>print cv2.__version__)
- 3.2.0 (this is when I check with: $ pkg-config --modversion opencv)
when I run this:
$ rosrun read_image show_image.py
I get this error:
read an image from file
create a window holder for the image
display the image
Traceback (most recent call last):
File "/home/candas/catkin_ws/src/read_image/src/show_image.py", line 17, in <module>
cv2.imshow("Image",img) # show the image, name of the display
cv2.error: OpenCV(4.3.0-dev) /home/candas/opencv_build/opencv/modules/highgui/src/window.cpp:376: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'imshow'
The code I am trying to run is to read and display the image:
#!/usr/bin/env python
#import numpy: the data structure that will handle an image
import numpy as np # stores all the pixels of image
#import openCV
import cv2
print 'read an image from file'
img = cv2.imread("images/apple-touch-icon-144x144-precomposed.png")
print 'create a window holder for the image'
cv2.namedWindow("Image",cv2.WINDOW_NORMAL) # name of the window, resize
print 'display the image'
cv2.imshow("Image",img) # show the image, name of the display
Is it because, there are different versions installed? Or the way I define the path? Or CMakeLists.txt? I would be glad if you could help. Thanks
your image was not loaded, try an absolute path, and (ALWAYS !!) check imread() output for None
how on earth would we know, what this does ?
sorry for not being clear. It runs the python code given below.
^^ sure, but how would we know how it starts python, from where, or what read_image does.
for a change, just cd to the folder that has the "images/apple-touch-icon-144x144-precomposed.png" in it, and run
python myscript.py
(that is -- shortcut ROS !)It is raspberry pi or pc?
Didn't you added
cv2. waitKey()
?@berak There are packages in ROS and read_image is the package I created which includes the file show_image.py. So, the codes are written in this show_image.py file and when I call rosrun as above, it runs this python file. I did change the file path as you said and added
cv2.waitKey()
as @supra56 said. Now it works, thanks a lot.The code: