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

Optimize image conversion

asked 2014-11-24 14:26:35 -0600

Ruud gravatar image

Hey all,

I am trying to optimize some image conversion code to boost the framerate in my system. I now copy from a source pixel by pixel, but that is not really efficient.

I have a pointer to a uint8_t array, which formerly was a pointer to a char array. Either of the two should be filled in the data field of a sensor_msgs::Image message.

I am not an expert, please enlighten me with the efficient approach here! :)

      uint8_t *uint8_pointer = reinterpret_cast<uint8_t*>( (unsigned char*) grayscale_channel[0].L() );

      int height = atoi(height_pointer.ToString().Text());
      int width  = atoi(width_pointer.ToString().Text());

      sensor_msgs::Image output_image;

      output_image.header.stamp     = timestamp.data;
      output_image.height           = height;
      output_image.width            = width;
      output_image.encoding         = "mono8";
      output_image.is_bigendian     = false;
      output_image.step             = 1 * width;

      for(int i=0; i<(width*height);i++)
      {
           output_image.data.push_back(uint8_pointer[i]);
      }

      return output_image;
edit retag flag offensive close merge delete

Comments

I tried simply pushing back the dereferenced pointer, however that throws an error during runtime.

output_image.data.push_back( *uint8_pointer );

The error:

 Error loading image: OGRE EXCEPTION(2:InvalidParametersException): Stream size does not match calculated image size in Imag
Ruud gravatar image Ruud  ( 2014-11-24 14:34:22 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2014-11-24 17:55:14 -0600

kmhallen gravatar image

The for() loop can be replaced by:

output_image.data.resize(width*height); // Allocate memory
memcpy(output_image.data.data(), uint8_pointer, width*height); // Efficient copy

Instead of running individual nodes, running several nodelets in a single process can also speed up message transmission. http://wiki.ros.org/nodelet

edit flag offensive delete link more

Comments

That works!

I will do some reading on memory allocation and memcopy. Also thanks for the reference to the nodelet, that might also be very valuable to speed things up. :)

Ruud gravatar image Ruud  ( 2014-11-25 08:42:23 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2014-11-24 14:26:35 -0600

Seen: 531 times

Last updated: Nov 24 '14