bool saveImage(cv::Mat image, std::string &filename, bool depth = false) { if (!image.empty()) { try { filename = (g_format).str(); } catch (...) { g_format.clear(); } try { filename = (g_format % path).str(); } catch (...) { g_format.clear(); } try { filename = (g_format % path % count_).str(); } catch (...) { g_format.clear(); } try { filename = (g_format % path % count_ % "jpg").str(); } catch (...) { g_format.clear(); } try { if(!depth){ filename = (g_format % path % count_ % control % "jpg").str(); }else{ filename = (g_format % path_depth % count_ % control % "jpg").str(); } } catch (...) { g_format.clear(); } if ( save_all_image || save_image_service ) { try{ cv::imwrite(filename, image); ROS_INFO("Saved image %s", filename.c_str()); save_image_service = false; }catch(runtime_error& ex){ fprintf(stderr, "Exception converting image to PNG format: %s\n", ex.what()); return false; } } else { return false; } } else { ROS_WARN("Couldn't save image, no data!"); return false; } return true; }
bool saveImage(const sensor_msgs::ImageConstPtr& image_msg, std::string &filename) { cv::Mat image; try { image = cv_bridge::toCvShare(image_msg, encoding)->image; } catch(cv_bridge::Exception) { ROS_ERROR("Unable to convert %s image to bgr8", image_msg->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; }