Rosanswers logo

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.


Originally posted by yclin on ROS Answers with karma: 53 on 2012-04-08

Post score: 0


2 Answers 2


Rosanswers logo

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.

Originally posted by roehling with karma: 1951 on 2012-04-08

This answer was ACCEPTED on the original site

Post score: 3

Original comments

Comment by Mac on 2012-04-09:
roslaunch is going (usually) going to run in ~/.ros, unless you change some environment variables.

Comment by roehling on 2012-04-09:
I think it does, but I have never read any official specification about that, so you probably should not rely on it.

Comment by pmarinplaza on 2012-10-15:
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?


Rosanswers logo

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;

Originally posted by Bence Magyar with karma: 358 on 2012-04-09

This answer was NOT ACCEPTED on the original site

Post score: 1

Original comments

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

Comment by Bence Magyar on 2012-04-11:
fixed, thx

Comment by Stephan on 2012-04-11:
...no... you should print "yeah" istead of "sorry" when image loading succeeds ;-)

Comment by Bence Magyar on 2012-04-11:
:) modified it in a pessimist way


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.