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

Problem with sensor_msgs::Image::ConstPtr conversion to IplImage

asked 2011-03-04 08:52:25 -0600

Handsan gravatar image

updated 2014-01-28 17:09:16 -0600

ngrennan gravatar image

Hello Ros-Users,

i have a strange problem using the CvBridge and OpenCV. I subscribed to a topic delivering images. The Problem is that cvSaveImage() fails sometimes with :

OpenCV Error: Bad argument (Unknown array type) in cvarrToMat, file /tmp/buildd/ros-diamondback-vision-opencv-1.4.1/debian/ros-diamondback-vision-opencv/opt/ros/diamondback/stacks/vision_opencv/opencv2/build/opencv-svn/modules/core/src/matrix.cpp, line 641 terminate called after throwing an instance of 'cv::Exception' what(): /tmp/buildd/ros-diamondback-vision-opencv-1.4.1/debian/ros-diamondback-vision-opencv/opt/ros/diamondback/stacks/vision_opencv/opencv2/build/opencv-svn/modules/core/src/matrix.cpp:641: error: (-5) Unknown array type in function cvarrToMat

The strangeness is that if i do cvSaveImage() directly in my callbackFunction it does not fail at all. Ist just fails (after some time) if i push the images back into a vector. I want to buffer the images for later processing and then store them to HDD, so that's why i am doing the vector-stuff.

My callback function is:

void RosClass::imageCallback(const sensor_msgs::Image::ConstPtr& msg){
       //image_queue is a std::vector<sensor_msgs::Image::ConstPtr>
       //cvSaveImage("test.jpg", getIplImage(msg)) works perfectly

My Function to convert the Ros-Image to an IplImage is:

IplImage* RosClass::getIplImage(sensor_msgs::Image::ConstPtr msg){  

    IplImage *img = 0;
    std::string cv_encoding="passthrough";
    sensor_msgs::CvBridge bridge;

        img = bridge.imgMsgToCv(msg, cv_encoding);
    }catch(sensor_msgs::CvBridgeException error) { //TODO: Compiler meldet ein Warning
        ROS_ERROR("Failed to convert sensor_msgs::Image to img_t with error [%d]", error);
        return 0;

    return img;

This is the function where i retrieve the images from the vector and try to save them to HDD. But this fails. Not instantly but after some time. My workflow is only: Push images to the vector - if vector.size() > Threash - store them to HDD - clear the vector. Thats all. It seems like the cvSaveImage() fails after the vector was 1 time cleared. I have no idea whats the problem. Any suggestions? :)

void RosClass::processBufferedImages(){
    for(int i = 0; i < image_queue.size();i++){
        //Convert to IplImage -> works without any problems
        IplImage *img = getIplImage(;
        //Save Images to HDD -> fails after some time ... :/

Thx for reading.

edit retag flag offensive close merge delete


Update: If i uncomment the "image_queue.clear()" it fails anyway. Not instantly but after a short time. The vector size is low with an element count of lower than 100 elements. So the amount of memory should not be any problem.
Handsan gravatar image Handsan  ( 2011-03-04 09:17:17 -0600 )edit

3 Answers

Sort by ยป oldest newest most voted

answered 2011-03-10 22:58:26 -0600

I had a similar problem, to solve it, I made a local copy of the image received each time and that was the image that I stored on an array.

edit flag offensive delete link more


As far as I understand the ConstPtr typedef magic, it is a reference-counting pointer-class. However as it is given by reference, not by value, there is no new instance created and thus the reference counter isn't increased. So I imagine it should suffice to copy (or receive by value) the ConstPtr. Just a guess though
Felix Endres gravatar image Felix Endres  ( 2011-03-11 06:00:43 -0600 )edit

answered 2011-03-10 19:43:24 -0600

void RosClass::imageCallback(const sensor_msgs::Image::ConstPtr& msg){

What is this "temp" you are pushing?

edit flag offensive delete link more

answered 2012-04-14 06:50:15 -0600

this post is marked as community wiki

This post is a wiki. Anyone with karma >75 is welcome to improve it.

I also face the same problem. I wonder is there any solution after so long ... Any good Suggestion ?

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2011-03-04 08:52:25 -0600

Seen: 3,229 times

Last updated: Apr 14 '12