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

Revision history [back]

click to hide/show revision 1
initial version

I faced the same problem and I was breaking my head over it. However, I recently came up with a solution that solves it. I looked at the source code of both nodes, image_view and image_saver. Looks like there is an explicit conversion that is implemented in image_view, but the same is not added in image_saver. So I cloned the source code from git (link) and made the following modification in the image_saver code. After this you can save exactly what you see in image_view

bool saveImage(const sensor_msgs::ImageConstPtr& image_msg, std::string &filename) {
cv::Mat image;
cv_bridge::CvImageConstPtr cv_ptr;
cv_bridge::CvtColorForDisplayOptions options;
options.bg_label=-1;
options.colormap=-1;
options.do_dynamic_scaling=0;
options.min_image_value=0;
options.max_image_value=10;
try
{
  image = cv_bridge::toCvShare(image_msg, encoding)->image;
} catch(cv_bridge::Exception)
{
   cv_ptr = cv_bridge::cvtColorForDisplay(cv_bridge::toCvShare(image_msg), "", options);
   image=cv_ptr->image;
  // ROS_ERROR("Unable to convert %s image to %s", image_msg->encoding.c_str(), encoding.c_str());
  // return false;
} catch(cv_bridge::Exception)
{

  ROS_ERROR("Unable to convert %s image to %s", image_msg->encoding.c_str(), encoding.c_str());
  return false;
}

if (!image.empty()) {
  try {
    filename = (g_format).str();
  } catch (...) { g_format.clear(); }
  try {
    filename = (g_format % count_).str();
  } catch (...) { g_format.clear(); }
  try { 
    filename = (g_format % count_ % "jpg").str();
  } catch (...) { g_format.clear(); }

  if ( save_all_image || save_image_service ) {
    cv::imwrite(filename, image);
    ROS_INFO("Saved image %s", filename.c_str());

    save_image_service = false;
  } else {
    return false;
  }
} else {
  ROS_WARN("Couldn't save image, no data!");
  return false;
}
return true;

}