From a6eb736269d4e7843ecd82731e6d6e476a3d2556 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Hern=C3=A1ndez=20Cordero?= Date: Fri, 19 Jan 2024 15:25:48 +0100 Subject: [PATCH] ROS 2: Add option to prepend timestamp to image filename in image_saver node (#870) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Related to this PR in ROS 1 https://github.com/ros-perception/image_pipeline/pull/806/files --------- Signed-off-by: Alejandro Hernández Cordero --- image_view/include/image_view/image_saver_node.hpp | 1 + image_view/src/image_saver_node.cpp | 9 +++++++++ 2 files changed, 10 insertions(+) diff --git a/image_view/include/image_view/image_saver_node.hpp b/image_view/include/image_view/image_saver_node.hpp index 217ea71d2..af3b0434c 100644 --- a/image_view/include/image_view/image_saver_node.hpp +++ b/image_view/include/image_view/image_saver_node.hpp @@ -70,6 +70,7 @@ class ImageSaverNode private: boost::format g_format; + bool stamped_filename_; bool save_all_image_{false}; bool save_image_service_{false}; std::string encoding_; diff --git a/image_view/src/image_saver_node.cpp b/image_view/src/image_saver_node.cpp index 5760b1488..9707f8a1b 100644 --- a/image_view/src/image_saver_node.cpp +++ b/image_view/src/image_saver_node.cpp @@ -48,6 +48,7 @@ #include #include +#include #include #include "cv_bridge/cv_bridge.hpp" @@ -91,6 +92,7 @@ ImageSaverNode::ImageSaverNode(const rclcpp::NodeOptions & options) format_string = this->declare_parameter("filename_format", std::string("left%04i.%s")); encoding_ = this->declare_parameter("encoding", std::string("bgr8")); save_all_image_ = this->declare_parameter("save_all_image", true); + stamped_filename_ = this->declare_parameter("stamped_filename", false); request_start_end_ = this->declare_parameter("request_start_end", false); g_format.parse(format_string); @@ -144,6 +146,13 @@ bool ImageSaverNode::saveImage( } if (save_all_image_ || save_image_service_) { + if (stamped_filename_) { + std::stringstream ss; + ss << this->now().nanoseconds(); + std::string timestamp_str = ss.str(); + filename.insert(0, timestamp_str); + } + if (cv::imwrite(filename, image)) { RCLCPP_INFO(this->get_logger(), "Saved image %s", filename.c_str()); } else {