Skip to content

Commit a18fcfb

Browse files
1222-takeshiwep21yukkysaitomitsudome-rnnmm
authored
feat: image_transport_decompressor package (autowarefoundation#43)
* Add nodelets of tlr nodes (autowarefoundation#715) * Add classifier nodelet Signed-off-by: Daisuke Nishimatsu <border_goldenmarket@yahoo.co.jp> * Replace boost::shared_ptr into std::shared_ptr Signed-off-by: Daisuke Nishimatsu <border_goldenmarket@yahoo.co.jp> * Add lock guard Signed-off-by: Daisuke Nishimatsu <border_goldenmarket@yahoo.co.jp> * Add detetcor nodelet Signed-off-by: Daisuke Nishimatsu <border_goldenmarket@yahoo.co.jp> * Integrate main into node Signed-off-by: Daisuke Nishimatsu <border_goldenmarket@yahoo.co.jp> * Add SubscriberStatusCallback Signed-off-by: Daisuke Nishimatsu <border_goldenmarket@yahoo.co.jp> * add image_transport_decompresser nodelet Signed-off-by: Yukihiro Saito <yukky.saito@gmail.com> * Add visualizer nodelet Signed-off-by: Daisuke Nishimatsu <border_goldenmarket@yahoo.co.jp> * fixed bug Signed-off-by: Yukihiro Saito <yukky.saito@gmail.com> * Fix plugin name Signed-off-by: Daisuke Nishimatsu <border_goldenmarket@yahoo.co.jp> * Launch nodelet Signed-off-by: Daisuke Nishimatsu <border_goldenmarket@yahoo.co.jp> * Fix classifier constructor Signed-off-by: Daisuke Nishimatsu <border_goldenmarket@yahoo.co.jp> * add decompresser node Signed-off-by: Yukihiro Saito <yukky.saito@gmail.com> * fix typo Signed-off-by: Yukihiro Saito <yukky.saito@gmail.com> * fixed bug Signed-off-by: Yukihiro Saito <yukky.saito@gmail.com> * fixed bug Signed-off-by: Yukihiro Saito <yukky.saito@gmail.com> * cosmetic change Signed-off-by: Yukihiro Saito <yukky.saito@gmail.com> * add param Signed-off-by: Yukihiro Saito <yukky.saito@gmail.com> * fix bug Signed-off-by: Yukihiro Saito <yukky.saito@gmail.com> * Fix build warning Signed-off-by: Daisuke Nishimatsu <border_goldenmarket@yahoo.co.jp> * change rgb Signed-off-by: Yukihiro Saito <yukky.saito@gmail.com> * change rgb Signed-off-by: Yukihiro Saito <yukky.saito@gmail.com> Co-authored-by: Yukihiro Saito <yukky.saito@gmail.com> * remove ROS1 packages temporarily Signed-off-by: mitsudome-r <ryohsuke.mitsudome@tier4.jp> * Revert "remove ROS1 packages temporarily" This reverts commit 4f557b764f89acf6fa4dfdad198abe53021aefe7. Signed-off-by: mitsudome-r <ryohsuke.mitsudome@tier4.jp> * add COLCON_IGNORE to ros1 packages Signed-off-by: mitsudome-r <ryohsuke.mitsudome@tier4.jp> * Rename h files to hpp (autowarefoundation#142) * Change includes * Rename files * Adjustments to make things compile * Other packages * Adjust copyright notice on 532 out of 699 source files (autowarefoundation#143) * Use quotes for includes where appropriate (autowarefoundation#144) * Use quotes for includes where appropriate * Fix lint tests * Make tests pass hopefully * Run uncrustify on the entire Pilot.Auto codebase (autowarefoundation#151) * Run uncrustify on the entire Pilot.Auto codebase * Exclude open PRs * Unify Apache-2.0 license name (autowarefoundation#1242) * Refine BSD license name (autowarefoundation#1244) * Porting image transport decompressor (autowarefoundation#1428) Signed-off-by: wep21 <border_goldenmarket@yahoo.co.jp> * Cosmetic change (autowarefoundation#1443) * Fix -Wunused-parameter (autowarefoundation#1836) * Fix -Wunused-parameter Signed-off-by: Kenji Miyake <kenji.miyake@tier4.jp> * Fix mistake Signed-off-by: Kenji Miyake <kenji.miyake@tier4.jp> * fix spell * Fix lint issues Signed-off-by: Kenji Miyake <kenji.miyake@tier4.jp> * Ignore flake8 warnings Signed-off-by: Kenji Miyake <kenji.miyake@tier4.jp> Co-authored-by: Hiroki OTA <hiroki.ota@tier4.jp> * Change formatter to clang-format and black (autowarefoundation#2332) * Revert "Temporarily comment out pre-commit hooks" This reverts commit 748e9cdb145ce12f8b520bcbd97f5ff899fc28a3. * Replace ament_lint_common with autoware_lint_common Signed-off-by: Kenji Miyake <kenji.miyake@tier4.jp> * Remove ament_cmake_uncrustify and ament_clang_format Signed-off-by: Kenji Miyake <kenji.miyake@tier4.jp> * Apply Black Signed-off-by: Kenji Miyake <kenji.miyake@tier4.jp> * Apply clang-format Signed-off-by: Kenji Miyake <kenji.miyake@tier4.jp> * Fix build errors Signed-off-by: Kenji Miyake <kenji.miyake@tier4.jp> * Fix for cpplint * Fix include double quotes to angle brackets Signed-off-by: Kenji Miyake <kenji.miyake@tier4.jp> * Apply clang-format Signed-off-by: Kenji Miyake <kenji.miyake@tier4.jp> * Fix build errors Signed-off-by: Kenji Miyake <kenji.miyake@tier4.jp> * Add COLCON_IGNORE (autowarefoundation#500) Signed-off-by: Kenji Miyake <kenji.miyake@tier4.jp> * remove COLCON_IGNORE (autowarefoundation#515) * doc image transport decompressor (autowarefoundation#602) * add README * minimal doc * Remove template comments from node documents (autowarefoundation#608) * rename document file * remove template comment * feat: add launch file for image transport decompressor Co-authored-by: Daisuke Nishimatsu <42202095+wep21@users.noreply.github.com> Co-authored-by: Yukihiro Saito <yukky.saito@gmail.com> Co-authored-by: mitsudome-r <ryohsuke.mitsudome@tier4.jp> Co-authored-by: Nikolai Morin <nnmmgit@gmail.com> Co-authored-by: Kazuki Miyahara <kmiya@outlook.com> Co-authored-by: Kenji Miyake <31987104+kenji-miyake@users.noreply.github.com> Co-authored-by: Hiroki OTA <hiroki.ota@tier4.jp> Co-authored-by: Satoshi OTA <44889564+satoshi-ota@users.noreply.github.com> Co-authored-by: Tomoya Kimura <tomoya.kimura@tier4.jp>
1 parent e0fc816 commit a18fcfb

File tree

6 files changed

+339
-0
lines changed

6 files changed

+339
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
cmake_minimum_required(VERSION 3.5)
2+
project(image_transport_decompressor)
3+
4+
### Compile options
5+
if(NOT CMAKE_CXX_STANDARD)
6+
set(CMAKE_CXX_STANDARD 14)
7+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
8+
set(CMAKE_CXX_EXTENSIONS OFF)
9+
endif()
10+
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
11+
add_compile_options(-Wall -Wextra -Wpedantic)
12+
endif()
13+
14+
15+
find_package(ament_cmake_auto)
16+
ament_auto_find_build_dependencies()
17+
18+
find_package(OpenCV REQUIRED)
19+
20+
include_directories(
21+
${OpenCV_INCLUDE_DIRS}
22+
)
23+
24+
ament_auto_add_library(image_transport_decompressor SHARED
25+
src/image_transport_decompressor.cpp
26+
)
27+
28+
target_link_libraries(image_transport_decompressor
29+
${OpenCV_LIBRARIES}
30+
)
31+
32+
rclcpp_components_register_node(image_transport_decompressor
33+
PLUGIN "image_preprocessor::ImageTransportDecompressor"
34+
EXECUTABLE image_transport_decompressor_node
35+
)
36+
37+
if(BUILD_TESTING)
38+
find_package(ament_lint_auto REQUIRED)
39+
ament_lint_auto_find_test_dependencies()
40+
endif()
41+
42+
ament_auto_package()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# image_transport_decompressor
2+
3+
## Purpose
4+
5+
The `image_transport_decompressor` is a node that decompresses images.
6+
7+
## Inner-workings / Algorithms
8+
9+
## Inputs / Outputs
10+
11+
### Input
12+
13+
| Name | Type | Description |
14+
| -------------------------- | ----------------------------------- | ---------------- |
15+
| `~/input/compressed_image` | `sensor_msgs::msg::CompressedImage` | compressed image |
16+
17+
### Output
18+
19+
| Name | Type | Description |
20+
| -------------------- | ------------------------- | ------------------ |
21+
| `~/output/raw_image` | `sensor_msgs::msg::Image` | decompressed image |
22+
23+
## Parameters
24+
25+
## Assumptions / Known limits
26+
27+
## (Optional) Error detection and handling
28+
29+
## (Optional) Performance characterization
30+
31+
## (Optional) References/External links
32+
33+
## (Optional) Future extensions / Unimplemented parts
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright 2020 Tier IV, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef IMAGE_TRANSPORT_DECOMPRESSOR__IMAGE_TRANSPORT_DECOMPRESSOR_HPP_
16+
#define IMAGE_TRANSPORT_DECOMPRESSOR__IMAGE_TRANSPORT_DECOMPRESSOR_HPP_
17+
18+
#include <rclcpp/rclcpp.hpp>
19+
20+
#include <sensor_msgs/msg/compressed_image.hpp>
21+
#include <sensor_msgs/msg/image.hpp>
22+
23+
#include <memory>
24+
#include <string>
25+
#include <utility>
26+
27+
namespace image_preprocessor
28+
{
29+
class ImageTransportDecompressor : public rclcpp::Node
30+
{
31+
public:
32+
explicit ImageTransportDecompressor(const rclcpp::NodeOptions & node_options);
33+
34+
private:
35+
void onCompressedImage(
36+
const sensor_msgs::msg::CompressedImage::ConstSharedPtr input_compressed_image_msg);
37+
38+
rclcpp::Subscription<sensor_msgs::msg::CompressedImage>::SharedPtr compressed_image_sub_;
39+
rclcpp::Publisher<sensor_msgs::msg::Image>::SharedPtr raw_image_pub_;
40+
std::string encoding_;
41+
};
42+
43+
} // namespace image_preprocessor
44+
45+
#endif // IMAGE_TRANSPORT_DECOMPRESSOR__IMAGE_TRANSPORT_DECOMPRESSOR_HPP_
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<launch>
2+
<arg name="input_topic_name" default="/input/compressed_image" />
3+
<arg name="output_topic_name" default="/output/raw_image" />
4+
5+
<node pkg="image_transport_decompressor" exec="image_transport_decompressor_node" name="image_transport_decompressor_node">
6+
<remap from="input" to="$(var input_topic_name)" />
7+
<remap from="output" to="$(var output_topic_name)" />
8+
</node>
9+
</launch>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0"?>
2+
<package format="3">
3+
<name>image_transport_decompressor</name>
4+
<version>0.0.0</version>
5+
<description>The image_transport_decompressor package</description>
6+
7+
<maintainer email="yukihiro.saito@tier4.jp">Yukihiro Saito</maintainer>
8+
<author email="yukihiro.saito@tier4.jp">Yukihiro Saito</author>
9+
<license>Apache License 2.0</license>
10+
<author>Patrick Mihelich</author>
11+
<author>Julius Kammerl</author>
12+
<license>BSD-3-Clause</license>
13+
14+
<buildtool_depend>ament_cmake_auto</buildtool_depend>
15+
16+
<depend>cv_bridge</depend>
17+
<depend>rclcpp</depend>
18+
<depend>rclcpp_components</depend>
19+
<depend>sensor_msgs</depend>
20+
21+
<test_depend>ament_lint_auto</test_depend>
22+
<test_depend>autoware_lint_common</test_depend>
23+
24+
<export>
25+
<build_type>ament_cmake</build_type>
26+
</export>
27+
28+
29+
</package>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
// Copyright 2020 Tier IV, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
/*********************************************************************
16+
* Software License Agreement (BSD License)
17+
*
18+
* Copyright (c) 2012, Willow Garage, Inc.
19+
* All rights reserved.
20+
*
21+
* Redistribution and use in source and binary forms, with or without
22+
* modification, are permitted provided that the following conditions
23+
* are met:
24+
*
25+
* * Redistributions of source code must retain the above copyright
26+
* notice, this list of conditions and the following disclaimer.
27+
* * Redistributions in binary form must reproduce the above
28+
* copyright notice, this list of conditions and the following
29+
* disclaimer in the documentation and/or other materials provided
30+
* with the distribution.
31+
* * Neither the name of the Willow Garage nor the names of its
32+
* contributors may be used to endorse or promote products derived
33+
* from this software without specific prior written permission.
34+
*
35+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
38+
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
39+
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
40+
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
41+
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
43+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
44+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
45+
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
46+
* POSSIBILITY OF SUCH DAMAGE.
47+
*********************************************************************/
48+
49+
#include "image_transport_decompressor/image_transport_decompressor.hpp"
50+
51+
#include <opencv2/highgui/highgui.hpp>
52+
#include <opencv2/imgproc/imgproc.hpp>
53+
54+
#include <sensor_msgs/image_encodings.hpp>
55+
56+
#include <cv_bridge/cv_bridge.h>
57+
58+
#include <limits>
59+
#include <memory>
60+
#include <string>
61+
#include <utility>
62+
#include <vector>
63+
64+
namespace image_preprocessor
65+
{
66+
ImageTransportDecompressor::ImageTransportDecompressor(const rclcpp::NodeOptions & node_options)
67+
: rclcpp::Node("image_transport_decompressor", node_options),
68+
encoding_(declare_parameter("encoding", "default"))
69+
{
70+
compressed_image_sub_ = create_subscription<sensor_msgs::msg::CompressedImage>(
71+
"~/input/compressed_image", rclcpp::SensorDataQoS(),
72+
std::bind(&ImageTransportDecompressor::onCompressedImage, this, std::placeholders::_1));
73+
raw_image_pub_ =
74+
create_publisher<sensor_msgs::msg::Image>("~/output/raw_image", rclcpp::SensorDataQoS());
75+
}
76+
77+
void ImageTransportDecompressor::onCompressedImage(
78+
const sensor_msgs::msg::CompressedImage::ConstSharedPtr input_compressed_image_msg)
79+
{
80+
cv_bridge::CvImagePtr cv_ptr(new cv_bridge::CvImage);
81+
// Copy message header
82+
cv_ptr->header = input_compressed_image_msg->header;
83+
84+
// Decode color/mono image
85+
try {
86+
cv_ptr->image = cv::imdecode(cv::Mat(input_compressed_image_msg->data), cv::IMREAD_COLOR);
87+
88+
// Assign image encoding string
89+
const size_t split_pos = input_compressed_image_msg->format.find(';');
90+
if (split_pos == std::string::npos) {
91+
// Older version of compressed_image_transport does not signal image format
92+
switch (cv_ptr->image.channels()) {
93+
case 1:
94+
cv_ptr->encoding = sensor_msgs::image_encodings::MONO8;
95+
break;
96+
case 3:
97+
cv_ptr->encoding = sensor_msgs::image_encodings::BGR8;
98+
break;
99+
default:
100+
RCLCPP_ERROR(
101+
get_logger(), "Unsupported number of channels: %i", cv_ptr->image.channels());
102+
break;
103+
}
104+
} else {
105+
std::string image_encoding;
106+
if (encoding_ == std::string("default")) {
107+
image_encoding = input_compressed_image_msg->format.substr(0, split_pos);
108+
} else if (encoding_ == std::string("rgb8")) {
109+
image_encoding = "rgb8";
110+
} else if (encoding_ == std::string("bgr8")) {
111+
image_encoding = "bgr8";
112+
} else {
113+
image_encoding = input_compressed_image_msg->format.substr(0, split_pos);
114+
}
115+
116+
cv_ptr->encoding = image_encoding;
117+
118+
if (sensor_msgs::image_encodings::isColor(image_encoding)) {
119+
std::string compressed_encoding = input_compressed_image_msg->format.substr(split_pos);
120+
bool compressed_bgr_image =
121+
(compressed_encoding.find("compressed bgr") != std::string::npos);
122+
123+
// Revert color transformation
124+
if (compressed_bgr_image) {
125+
// if necessary convert colors from bgr to rgb
126+
if (
127+
(image_encoding == sensor_msgs::image_encodings::RGB8) ||
128+
(image_encoding == sensor_msgs::image_encodings::RGB16)) {
129+
cv::cvtColor(cv_ptr->image, cv_ptr->image, CV_BGR2RGB);
130+
}
131+
132+
if (
133+
(image_encoding == sensor_msgs::image_encodings::RGBA8) ||
134+
(image_encoding == sensor_msgs::image_encodings::RGBA16)) {
135+
cv::cvtColor(cv_ptr->image, cv_ptr->image, CV_BGR2RGBA);
136+
}
137+
138+
if (
139+
(image_encoding == sensor_msgs::image_encodings::BGRA8) ||
140+
(image_encoding == sensor_msgs::image_encodings::BGRA16)) {
141+
cv::cvtColor(cv_ptr->image, cv_ptr->image, CV_BGR2BGRA);
142+
}
143+
} else {
144+
// if necessary convert colors from rgb to bgr
145+
if (
146+
(image_encoding == sensor_msgs::image_encodings::BGR8) ||
147+
(image_encoding == sensor_msgs::image_encodings::BGR16)) {
148+
cv::cvtColor(cv_ptr->image, cv_ptr->image, CV_RGB2BGR);
149+
}
150+
151+
if (
152+
(image_encoding == sensor_msgs::image_encodings::BGRA8) ||
153+
(image_encoding == sensor_msgs::image_encodings::BGRA16)) {
154+
cv::cvtColor(cv_ptr->image, cv_ptr->image, CV_RGB2BGRA);
155+
}
156+
157+
if (
158+
(image_encoding == sensor_msgs::image_encodings::RGBA8) ||
159+
(image_encoding == sensor_msgs::image_encodings::RGBA16)) {
160+
cv::cvtColor(cv_ptr->image, cv_ptr->image, CV_RGB2RGBA);
161+
}
162+
}
163+
}
164+
}
165+
} catch (cv::Exception & e) {
166+
RCLCPP_ERROR(get_logger(), "%s", e.what());
167+
}
168+
169+
size_t rows = cv_ptr->image.rows;
170+
size_t cols = cv_ptr->image.cols;
171+
172+
if ((rows > 0) && (cols > 0)) {
173+
// Publish message to user callback
174+
auto image_ptr = std::make_unique<sensor_msgs::msg::Image>(*cv_ptr->toImageMsg());
175+
raw_image_pub_->publish(std::move(image_ptr));
176+
}
177+
}
178+
} // namespace image_preprocessor
179+
180+
#include <rclcpp_components/register_node_macro.hpp>
181+
RCLCPP_COMPONENTS_REGISTER_NODE(image_preprocessor::ImageTransportDecompressor)

0 commit comments

Comments
 (0)