-
Notifications
You must be signed in to change notification settings - Fork 67
examples: Add image_pub_sub #629
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
furushchev
wants to merge
1
commit into
mvukov:main
Choose a base branch
from
furushchev:image-transport-example-test
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| load("@com_github_mvukov_rules_ros2//ros2:cc_defs.bzl", "ros2_cpp_binary") | ||
| load("@com_github_mvukov_rules_ros2//ros2:launch.bzl", "ros2_launch") | ||
| load("@com_github_mvukov_rules_ros2//ros2:plugin.bzl", "ros2_plugin") | ||
| load("@com_github_mvukov_rules_ros2//ros2:test.bzl", "ros2_test") | ||
| load("@com_github_mvukov_rules_ros2//ros2:topic.bzl", "ros2_topic") | ||
|
|
||
| ros2_cpp_binary( | ||
| name = "image_publisher", | ||
| srcs = ["image_publisher.cc"], | ||
| data = [ | ||
| ":rotated_plugin", | ||
| ], | ||
| set_up_ament = True, | ||
| deps = [ | ||
| "@ros2_common_interfaces//:cpp_sensor_msgs", | ||
| "@ros2_image_common//:image_transport", | ||
| "@ros2_rclcpp//:rclcpp", | ||
| ], | ||
| ) | ||
|
|
||
| ros2_cpp_binary( | ||
| name = "image_resizer", | ||
| srcs = ["image_resizer.cc"], | ||
| data = [ | ||
| ":rotated_plugin", | ||
| ], | ||
| set_up_ament = True, | ||
| deps = [ | ||
| "@ros2_common_interfaces//:cpp_sensor_msgs", | ||
| "@ros2_image_common//:image_transport", | ||
| "@ros2_rclcpp//:rclcpp", | ||
| ], | ||
| ) | ||
|
|
||
| ros2_plugin( | ||
| name = "rotated_plugin", | ||
| srcs = [ | ||
| "rotated_publisher.cc", | ||
| "rotated_publisher.h", | ||
| "rotated_subscriber.cc", | ||
| "rotated_subscriber.h", | ||
| ], | ||
| plugin_specs = [ | ||
| { | ||
| "base_class_type": "image_transport::PublisherPlugin", | ||
| "class_name": "image_transport/rotated_pub", | ||
| "class_type": "image_pub_sub::RotatedPublisher", | ||
| }, | ||
| { | ||
| "base_class_type": "image_transport::SubscriberPlugin", | ||
| "class_name": "image_transport/rotated_sub", | ||
| "class_type": "image_pub_sub::RotatedSubscriber", | ||
| }, | ||
| ], | ||
| deps = [ | ||
| "@ros2_common_interfaces//:cpp_sensor_msgs", | ||
| "@ros2_image_common//:image_transport", | ||
| "@ros2_pluginlib//:pluginlib", | ||
| "@ros2_rclcpp//:rclcpp", | ||
| ], | ||
| ) | ||
|
|
||
| ros2_test( | ||
| name = "tests", | ||
| size = "small", | ||
| launch_file = "tests.py", | ||
| nodes = [ | ||
| ":image_publisher", | ||
| ":image_resizer", | ||
| ], | ||
| deps = [ | ||
| "@ros2_common_interfaces//:py_sensor_msgs", | ||
| ], | ||
| ) | ||
|
|
||
| ros2_topic( | ||
| name = "topic", | ||
| deps = [ | ||
| "@ros2_common_interfaces//:py_sensor_msgs", | ||
| ], | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| // Copyright 2026 Yuki Furuta | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #include <chrono> | ||
| #include <memory> | ||
| #include <vector> | ||
|
|
||
| #include "image_transport/image_transport.hpp" | ||
| #include "rclcpp/rclcpp.hpp" | ||
| #include "sensor_msgs/image_encodings.hpp" | ||
| #include "sensor_msgs/msg/image.hpp" | ||
|
|
||
| class ImagePublisher final : public rclcpp::Node { | ||
| public: | ||
| ImagePublisher() | ||
| : Node("image_publisher"), pub_(), test_image_(), timer_(nullptr) { | ||
| declare_parameter("width", 1280); | ||
| declare_parameter("height", 720); | ||
| declare_parameter("frequency", 5.0); | ||
|
|
||
| const auto width = get_parameter("width").as_int(); | ||
| const auto height = get_parameter("height").as_int(); | ||
| const auto frequency = get_parameter("frequency").as_double(); | ||
|
|
||
| pub_ = image_transport::create_publisher(this, "~/output"); | ||
|
|
||
| test_image_.header.frame_id = "camera"; | ||
| test_image_.height = height; | ||
| test_image_.width = width; | ||
| test_image_.encoding = sensor_msgs::image_encodings::BGR8; | ||
| test_image_.is_bigendian = false; | ||
| test_image_.step = width * 3; | ||
| test_image_.data.resize(height * width * 3); | ||
|
|
||
| const std::vector<std::vector<uint8_t>> colors = { | ||
| {128, 128, 128}, // Gray | ||
| {0, 255, 255}, // Yellow | ||
| {255, 255, 0}, // Cyan | ||
| {0, 255, 0}, // Green | ||
| {255, 0, 255}, // Magenta | ||
| {0, 0, 255}, // Red | ||
| {255, 0, 0}, // Blue | ||
| }; | ||
|
|
||
| for (size_t y = 0; y < static_cast<size_t>(height); ++y) { | ||
| for (size_t x = 0; x < static_cast<size_t>(width); ++x) { | ||
| size_t bar_index = x / (width / colors.size()); | ||
| if (bar_index >= colors.size()) { | ||
| bar_index = colors.size() - 1; | ||
| } | ||
| const size_t pixel_index = (y * width + x) * 3; | ||
| test_image_.data[pixel_index] = colors[bar_index][0]; // B | ||
| test_image_.data[pixel_index + 1] = colors[bar_index][1]; // G | ||
| test_image_.data[pixel_index + 2] = colors[bar_index][2]; // R | ||
| } | ||
| } | ||
|
|
||
| const auto timer_callback = [this]() -> void { | ||
| test_image_.header.stamp = now(); | ||
| RCLCPP_INFO(get_logger(), "Publishing image %dx%d", test_image_.width, | ||
| test_image_.height); | ||
| pub_.publish(test_image_); | ||
| }; | ||
|
|
||
| const auto period_ms = static_cast<int>(1000.0 / frequency); | ||
| timer_ = create_wall_timer(std::chrono::milliseconds(period_ms), | ||
| timer_callback); | ||
| } | ||
|
|
||
| private: | ||
| image_transport::Publisher pub_; | ||
| sensor_msgs::msg::Image test_image_; | ||
| rclcpp::TimerBase::SharedPtr timer_; | ||
| }; | ||
|
|
||
| int main(int argc, char* argv[]) { | ||
| rclcpp::init(argc, argv); | ||
| rclcpp::spin(std::make_shared<ImagePublisher>()); | ||
| rclcpp::shutdown(); | ||
| return 0; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| // Copyright 2026 Yuki Furuta | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #include <memory> | ||
|
|
||
| #include "image_transport/image_transport.hpp" | ||
| #include "rclcpp/rclcpp.hpp" | ||
| #include "sensor_msgs/image_encodings.hpp" | ||
| #include "sensor_msgs/msg/image.hpp" | ||
|
|
||
| class ImageResizer final : public rclcpp::Node { | ||
| public: | ||
| ImageResizer() | ||
| : Node("image_resizer"), sub_(), pub_(), scale_(1.0) { | ||
| declare_parameter("scale", 1.0); | ||
| scale_ = get_parameter("scale").as_double(); | ||
|
|
||
| declare_parameter("input.image_transport", "raw"); | ||
| const auto transport_hint = get_parameter("input.image_transport").as_string(); | ||
|
|
||
| sub_ = image_transport::create_subscription( | ||
| this, "~/input", | ||
| std::bind(&ImageResizer::imageCallback, this, std::placeholders::_1), | ||
| transport_hint); | ||
| pub_ = image_transport::create_publisher(this, "~/output"); | ||
|
|
||
| RCLCPP_INFO(get_logger(), "Image resizer started with scale: %.2f, transport_hint: %s", | ||
| scale_, transport_hint.c_str()); | ||
| } | ||
|
|
||
| private: | ||
| void imageCallback(const sensor_msgs::msg::Image::ConstSharedPtr& msg) { | ||
| RCLCPP_INFO(get_logger(), "Received image %dx%d, resizing to scale %.2f", | ||
| msg->width, msg->height, scale_); | ||
|
|
||
| if (msg->encoding != sensor_msgs::image_encodings::BGR8) { | ||
| RCLCPP_ERROR(get_logger(), | ||
| "Unsupported encoding '%s'. Only '%s' is supported.", | ||
| msg->encoding.c_str(), | ||
| sensor_msgs::image_encodings::BGR8); | ||
| return; | ||
| } | ||
|
|
||
| auto resized_image = sensor_msgs::msg::Image(); | ||
| resized_image.header = msg->header; | ||
| resized_image.encoding = msg->encoding; | ||
| resized_image.is_bigendian = msg->is_bigendian; | ||
|
|
||
| const size_t resized_width = static_cast<size_t>(msg->width * scale_); | ||
| const size_t resized_height = static_cast<size_t>(msg->height * scale_); | ||
| resized_image.width = resized_width; | ||
| resized_image.height = resized_height; | ||
| resized_image.step = resized_width * 3; | ||
| resized_image.data.resize(resized_height * resized_width * 3); | ||
|
|
||
| for (size_t y = 0; y < resized_height; ++y) { | ||
| for (size_t x = 0; x < resized_width; ++x) { | ||
| size_t src_x = static_cast<size_t>(x / scale_); | ||
| size_t src_y = static_cast<size_t>(y / scale_); | ||
|
|
||
| if (src_x >= msg->width) src_x = msg->width - 1; | ||
| if (src_y >= msg->height) src_y = msg->height - 1; | ||
|
|
||
| const size_t src_index = (src_y * msg->width + src_x) * 3; | ||
| const size_t dst_index = (y * resized_width + x) * 3; | ||
|
|
||
| resized_image.data[dst_index] = msg->data[src_index]; | ||
| resized_image.data[dst_index + 1] = msg->data[src_index + 1]; | ||
| resized_image.data[dst_index + 2] = msg->data[src_index + 2]; | ||
| } | ||
| } | ||
|
|
||
| pub_.publish(resized_image); | ||
| RCLCPP_INFO(get_logger(), "Published resized image %dx%d", | ||
| resized_image.width, resized_image.height); | ||
| } | ||
|
|
||
| image_transport::Subscriber sub_; | ||
| image_transport::Publisher pub_; | ||
| double scale_; | ||
| }; | ||
|
|
||
| int main(int argc, char* argv[]) { | ||
| rclcpp::init(argc, argv); | ||
| rclcpp::spin(std::make_shared<ImageResizer>()); | ||
| rclcpp::shutdown(); | ||
| return 0; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| // Copyright 2026 Yuki Furuta | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #include "rotated_publisher.h" | ||
|
|
||
| #include <vector> | ||
|
|
||
| #include "rclcpp/logging.hpp" | ||
| #include "sensor_msgs/image_encodings.hpp" | ||
|
|
||
| namespace image_pub_sub { | ||
|
|
||
| void RotatedPublisher::publish(const sensor_msgs::msg::Image& message, | ||
| const PublishFn& publish_fn) const { | ||
|
|
||
| if (message.encoding != sensor_msgs::image_encodings::BGR8) { | ||
| auto logger = rclcpp::get_logger("rotated_publisher"); | ||
| RCLCPP_ERROR(logger, | ||
| "RotatedPublisher only supports '%s' encoding, got '%s'", | ||
| sensor_msgs::image_encodings::BGR8, | ||
| message.encoding.c_str()); | ||
| return; | ||
| } | ||
|
|
||
| // Create rotated image by rotating +90 degrees (clockwise). | ||
| // Rotate +90 deg clockwise: pixel at (x, y) moves to (height - 1 - y, x). | ||
| sensor_msgs::msg::Image rotated_image = message; | ||
|
|
||
| const size_t src_width = message.width; | ||
| const size_t src_height = message.height; | ||
| const size_t bytes_per_pixel = 3; | ||
|
|
||
| rotated_image.width = src_height; | ||
| rotated_image.height = src_width; | ||
| rotated_image.step = rotated_image.width * bytes_per_pixel; | ||
|
|
||
| std::vector<uint8_t> rotated_data(rotated_image.height * rotated_image.step); | ||
|
|
||
| for (size_t src_y = 0; src_y < src_height; ++src_y) { | ||
| for (size_t src_x = 0; src_x < src_width; ++src_x) { | ||
| const size_t dst_x = src_height - 1 - src_y; | ||
| const size_t dst_y = src_x; | ||
|
|
||
| const size_t src_offset = (src_y * message.step) + (src_x * bytes_per_pixel); | ||
| const size_t dst_offset = (dst_y * rotated_image.step) + (dst_x * bytes_per_pixel); | ||
|
|
||
| rotated_data[dst_offset + 0] = message.data[src_offset + 0]; | ||
| rotated_data[dst_offset + 1] = message.data[src_offset + 1]; | ||
| rotated_data[dst_offset + 2] = message.data[src_offset + 2]; | ||
| } | ||
| } | ||
|
|
||
| rotated_image.data = rotated_data; | ||
| publish_fn(rotated_image); | ||
| } | ||
|
|
||
| } // namespace image_pub_sub | ||
|
|
||
| #include "pluginlib/class_list_macros.hpp" | ||
| PLUGINLIB_EXPORT_CLASS(image_pub_sub::RotatedPublisher, image_transport::PublisherPlugin) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| // Copyright 2026 Yuki Furuta | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #ifndef ROTATED_PUBLISHER_H | ||
| #define ROTATED_PUBLISHER_H | ||
|
|
||
| #include <string> | ||
|
|
||
| #include "image_transport/simple_publisher_plugin.hpp" | ||
| #include "sensor_msgs/msg/image.hpp" | ||
|
|
||
| namespace image_pub_sub { | ||
|
|
||
| class RotatedPublisher final | ||
| : public image_transport::SimplePublisherPlugin<sensor_msgs::msg::Image> { | ||
| public: | ||
| virtual std::string getTransportName() const final { return "rotated"; } | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here and elsewhere, a small nit: IIRC, compilers are unlikely to optimize a virtual function from this header. Thus, those should be in the .cc file, same as |
||
|
|
||
| protected: | ||
| virtual void publish(const sensor_msgs::msg::Image& message, | ||
| const PublishFn& publish_fn) const final; | ||
| }; | ||
|
|
||
| } // namespace image_pub_sub | ||
|
|
||
| #endif // ROTATED_PUBLISHER_H | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.