ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

cvLoadImage fail in ROS.

asked 2012-04-08 20:05:07 -0600

yclin gravatar image

updated 2014-01-28 17:11:55 -0600

ngrennan gravatar image

Hi Everyone: I tried to load a static image in ROS: here is the part of code: #include <highgui.h> #include <cv.h> using namespace std; using namespace cv;

       int main (int argc, char** argv) {
       ...........................
       if( ! cvLoadImage("13_45_07-65.bmp") ) cout << "sorry" << endl;
      ............................
       }

 in the log file, I can find a " sorry ", which means the image is not successfully loaded.

 the code works well outside ros. I put it in ROS because I need the images to tell when to send geometry_msg.

 I use ROS electric and configure OpenCV-2.3.1 according to http://www.ros.org/wiki/opencv2.  
 OpenCV is used with ROS as a rosdep system dependency      

 I put the followings in the CMakeLists.txt of the package the code is in.
 "  find_package(OpenCV REQUIRED)

if(OpenCV_FOUND) message("====== Found ${OpenCV_VERSION} =======") message("=== ${OpenCV_LIBS}======") endif(OpenCV_FOUND) "

the cmake .. returns: ====== Found 2.3.1 ======= === opencv_gpu;opencv_contrib;opencv_legacy;opencv_objdetect;opencv_calib3d;opencv_features2d;opencv_video;opencv_highgui;opencv_ml;opencv_imgproc;opencv_flann;opencv_core======

I have no idea why this simple OpenCV command in my code cannot be executed in ROS. Anyone can help me? Thank you in advance.

Yucong

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
3

answered 2012-04-08 20:55:31 -0600

The program is probably executed with a different working directory than you expect, so the image file cannot be found. AFAIK rosrun keeps the current working directory, while roslaunch changes to some entirely different place. Neither changes the working directory to the package directory.

Current best practice is to use the $(find foo) directive in the launch file to substitute the absolute path to the package foo in a configuration value.

edit flag offensive delete link more

Comments

1

roslaunch is going (usually) going to run in ~/.ros, unless you change some environment variables.

Mac gravatar image Mac  ( 2012-04-09 08:05:23 -0600 )edit

I think it does, but I have never read any official specification about that, so you probably should not rely on it.

roehling gravatar image roehling  ( 2012-04-09 18:04:34 -0600 )edit

hello, I want to load one image in some folder (with cvLoadImage), What must I do to tell roslaunch to search in the correct folder? I try moving the image to ./ros and works very well but I want put in other folder. How?

pmarinplaza gravatar image pmarinplaza  ( 2012-10-15 04:30:23 -0600 )edit
1

answered 2012-04-09 22:21:52 -0600

Bence Magyar gravatar image

updated 2012-04-11 03:03:09 -0600

I would vote for the includes, and the C-style function.

#include <highgui.h> 
#include <cv.h>

With these I'm not sure that you'll get what you need. The right way to do the includes is

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

Those includes above will reference to the old legacy versions of opencv still available in some Ubuntu versions.

Also if you are developing a new code (and I can see that it's C++) please use the new C++ API to easily avoid problems like memory leaks and such. Too bad that 95% of the example materials that can be found (official&unofficial) on OpenCV are still advertising the old API. For example your line of code would look like this:

if( cv::imread("13_45_07-65.bmp").empty() )
    cout << "sorry" << endl;
edit flag offensive delete link more

Comments

Shouldn't this be 'if (cv::imread("13_45_07-65.bmp").empty())'? I am not sure what "!" does on cv::Mat...

Stephan gravatar image Stephan  ( 2012-04-10 01:00:43 -0600 )edit

fixed, thx

Bence Magyar gravatar image Bence Magyar  ( 2012-04-11 00:41:49 -0600 )edit

...no... you should print "yeah" istead of "sorry" when image loading succeeds ;-)

Stephan gravatar image Stephan  ( 2012-04-11 00:56:15 -0600 )edit

:) modified it in a pessimist way

Bence Magyar gravatar image Bence Magyar  ( 2012-04-11 03:03:55 -0600 )edit

Question Tools

Stats

Asked: 2012-04-08 20:05:07 -0600

Seen: 638 times

Last updated: Oct 15 '12