From 7ff61bfacc8db5a9ca174b9d0da207b276c6b18c Mon Sep 17 00:00:00 2001 From: Markus Vieth Date: Mon, 23 Sep 2019 15:53:53 +0200 Subject: [PATCH 1/2] Add functionality for depth images (16UC1 and 32FC1) --- .../opportunistic_link/image_compression.h | 4 --- .../opportunistic_link/image_compression.cpp | 27 ++++++++++++++++--- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/opportunistic_link/include/opportunistic_link/image_compression.h b/opportunistic_link/include/opportunistic_link/image_compression.h index 4992cb2..2e1aaa4 100644 --- a/opportunistic_link/include/opportunistic_link/image_compression.h +++ b/opportunistic_link/include/opportunistic_link/image_compression.h @@ -10,10 +10,6 @@ namespace image_compression { class ImageHandler { - protected: - - ; - public: ImageHandler() {} diff --git a/opportunistic_link/src/opportunistic_link/image_compression.cpp b/opportunistic_link/src/opportunistic_link/image_compression.cpp index ecad4fb..3c2fc94 100644 --- a/opportunistic_link/src/opportunistic_link/image_compression.cpp +++ b/opportunistic_link/src/opportunistic_link/image_compression.cpp @@ -21,7 +21,12 @@ void ImageHandler::reset_decoder() sensor_msgs::Image ImageHandler::decompress_image(const sensor_msgs::CompressedImage& compressed, const std::string& encoding) { sensor_msgs::Image decompressed; - cv::Mat decoded = cv::imdecode(compressed.data, CV_LOAD_IMAGE_ANYCOLOR); + cv::Mat decoded = cv::imdecode(compressed.data, CV_LOAD_IMAGE_UNCHANGED); + if(decoded.data==NULL) { + ROS_WARN("Decoding unsuccessful"); + } else { + ROS_DEBUG_STREAM("Decoding successful, rows=" << decoded.rows << ", cols=" << decoded.cols << ", opencv type=" << decoded.type()); + } cv_bridge::CvImage converted; converted = cv_bridge::CvImage(compressed.header, encoding, decoded); converted.toImageMsg(decompressed); @@ -37,9 +42,23 @@ sensor_msgs::CompressedImage ImageHandler::compress_image(const sensor_msgs::Ima cv_ptr = cv_bridge::toCvCopy(image); cv::Mat cv_image = cv_ptr->image; std::vector encoding_params; - encoding_params.push_back(CV_IMWRITE_JPEG_QUALITY); - encoding_params.push_back(quality); - bool ret = cv::imencode(".jpg", cv_image, compressed.data, encoding_params); + bool ret; + ROS_DEBUG_STREAM("Image encoding: " << image.encoding); + if(image.encoding=="16UC1") { + ROS_DEBUG("Encoding in png format"); + ret = cv::imencode(".png", cv_image, compressed.data, encoding_params); + compressed.format="png"; + } else if(image.encoding=="32FC1") { + ROS_DEBUG("Encoding in tiff format"); + ret = cv::imencode(".tiff", cv_image, compressed.data, encoding_params); + // jpeg and png are the only acceptable values for the format field, so it is not set here + } else { + ROS_DEBUG("Encoding in jpeg format"); + encoding_params.push_back(CV_IMWRITE_JPEG_QUALITY); + encoding_params.push_back(quality); + ret = cv::imencode(".jpg", cv_image, compressed.data, encoding_params); + compressed.format="jpeg"; + } if (ret) { ROS_DEBUG("OpenCV encoding success"); From 1be479e9621c94aab62c0963c2cc4335da9b26aa Mon Sep 17 00:00:00 2001 From: Markus Vieth Date: Tue, 24 Sep 2019 09:57:12 +0200 Subject: [PATCH 2/2] Make style changes and other requested changes --- .../opportunistic_link/image_compression.cpp | 27 ++++++++++++------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/opportunistic_link/src/opportunistic_link/image_compression.cpp b/opportunistic_link/src/opportunistic_link/image_compression.cpp index 3c2fc94..29cb805 100644 --- a/opportunistic_link/src/opportunistic_link/image_compression.cpp +++ b/opportunistic_link/src/opportunistic_link/image_compression.cpp @@ -22,9 +22,12 @@ sensor_msgs::Image ImageHandler::decompress_image(const sensor_msgs::CompressedI { sensor_msgs::Image decompressed; cv::Mat decoded = cv::imdecode(compressed.data, CV_LOAD_IMAGE_UNCHANGED); - if(decoded.data==NULL) { - ROS_WARN("Decoding unsuccessful"); - } else { + if(decoded.data == NULL) + { + throw std::runtime_error("OpenCV image decoding failed"); + } + else + { ROS_DEBUG_STREAM("Decoding successful, rows=" << decoded.rows << ", cols=" << decoded.cols << ", opencv type=" << decoded.type()); } cv_bridge::CvImage converted; @@ -42,22 +45,26 @@ sensor_msgs::CompressedImage ImageHandler::compress_image(const sensor_msgs::Ima cv_ptr = cv_bridge::toCvCopy(image); cv::Mat cv_image = cv_ptr->image; std::vector encoding_params; - bool ret; + bool ret = false; ROS_DEBUG_STREAM("Image encoding: " << image.encoding); - if(image.encoding=="16UC1") { + if(image.encoding == "16UC1") { ROS_DEBUG("Encoding in png format"); ret = cv::imencode(".png", cv_image, compressed.data, encoding_params); - compressed.format="png"; - } else if(image.encoding=="32FC1") { + compressed.format = "png"; + } + else if(image.encoding == "32FC1") + { ROS_DEBUG("Encoding in tiff format"); ret = cv::imencode(".tiff", cv_image, compressed.data, encoding_params); - // jpeg and png are the only acceptable values for the format field, so it is not set here - } else { + compressed.format = "tiff"; + } + else + { ROS_DEBUG("Encoding in jpeg format"); encoding_params.push_back(CV_IMWRITE_JPEG_QUALITY); encoding_params.push_back(quality); ret = cv::imencode(".jpg", cv_image, compressed.data, encoding_params); - compressed.format="jpeg"; + compressed.format = "jpeg"; } if (ret) {