[Solved]OpenCV error: (-215:Assertion failed), read and display an image [closed]

asked 2020-05-20 10:40:55 -0600

cgenctor gravatar image

updated 2020-05-20 20:35:37 -0600

supra56 gravatar image

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:

  1. 4.3.0-dev (when I check the version via python: >>>print cv2.__version__)
  2. 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

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by supra56
close date 2020-05-20 20:35:13.680047

Comments

your image was not loaded, try an absolute path, and (ALWAYS !!) check imread() output for None

berak gravatar imageberak ( 2020-05-20 11:03:48 -0600 )edit

$ rosrun read_image show_image.py

how on earth would we know, what this does ?

berak gravatar imageberak ( 2020-05-20 11:04:56 -0600 )edit
1

sorry for not being clear. It runs the python code given below.

cgenctor gravatar imagecgenctor ( 2020-05-20 11:11:45 -0600 )edit

^^ 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 !)

berak gravatar imageberak ( 2020-05-20 11:15:43 -0600 )edit

It is raspberry pi or pc?

supra56 gravatar imagesupra56 ( 2020-05-20 11:47:55 -0600 )edit

Didn't you added cv2. waitKey()?

supra56 gravatar imagesupra56 ( 2020-05-20 11:58:28 -0600 )edit
1

@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.

cgenctor gravatar imagecgenctor ( 2020-05-20 15:15:40 -0600 )edit
1

The code:

#!/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'
path = '/home/candas/catkin_ws/src/read_image/images/apple-touch-icon-144x144-precomposed.png'
img = cv2.imread(path) # call method cv2 image read and path
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
print 'press a key inside the image to exit'
cv2.waitKey()
cgenctor gravatar imagecgenctor ( 2020-05-20 15:23:18 -0600 )edit