2026-04-02 16:11:40 +08:00
|
|
|
/*
|
|
|
|
|
Copyright 2025 Manifold Tech Ltd.(www.manifoldtech.com.co)
|
|
|
|
|
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 "image_overlay_node.hpp"
|
|
|
|
|
|
2026-04-06 12:18:55 +08:00
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
cv::Mat resizeToHeight(const cv::Mat& src, int target_h)
|
|
|
|
|
{
|
|
|
|
|
if (src.empty() || target_h <= 0) {
|
|
|
|
|
return src.clone();
|
|
|
|
|
}
|
|
|
|
|
if (src.rows == target_h) {
|
|
|
|
|
return src.clone();
|
|
|
|
|
}
|
|
|
|
|
const double scale = static_cast<double>(target_h) / static_cast<double>(src.rows);
|
|
|
|
|
cv::Mat out;
|
|
|
|
|
cv::resize(src, out, cv::Size(), scale, scale, cv::INTER_LINEAR);
|
|
|
|
|
return out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
2026-04-02 16:11:40 +08:00
|
|
|
#ifdef ROS2
|
|
|
|
|
// ==================== ROS2 Implementation ====================
|
|
|
|
|
|
|
|
|
|
ImageOverlayNode::ImageOverlayNode(const rclcpp::NodeOptions& options)
|
|
|
|
|
: Node("image_overlay_node", options)
|
|
|
|
|
{
|
2026-04-09 15:51:26 +08:00
|
|
|
this->declare_parameter<std::string>("register_keys.overlay_reprojected_topic", "/odin1/overlay_img");
|
2026-04-02 16:11:40 +08:00
|
|
|
this->declare_parameter<std::string>("register_keys.overlay_camera_topic", "/odin1/image/undistorted");
|
2026-04-06 12:18:55 +08:00
|
|
|
this->declare_parameter<std::string>("register_keys.overlay_output_topic", "/odin1/combined_image/compressed");
|
|
|
|
|
this->declare_parameter<int>("register_keys.overlay_jpeg_quality", 85);
|
2026-04-02 16:11:40 +08:00
|
|
|
|
|
|
|
|
reprojected_topic_ = this->get_parameter("register_keys.overlay_reprojected_topic").as_string();
|
|
|
|
|
camera_topic_ = this->get_parameter("register_keys.overlay_camera_topic").as_string();
|
2026-04-06 12:18:55 +08:00
|
|
|
output_topic_ = this->get_parameter("register_keys.overlay_output_topic").as_string();
|
|
|
|
|
jpeg_quality_ = this->get_parameter("register_keys.overlay_jpeg_quality").as_int();
|
|
|
|
|
jpeg_quality_ = std::max(1, std::min(100, jpeg_quality_));
|
2026-04-02 16:11:40 +08:00
|
|
|
|
2026-04-06 12:18:55 +08:00
|
|
|
RCLCPP_INFO(this->get_logger(), "Subscribing to: %s and %s",
|
2026-04-02 16:11:40 +08:00
|
|
|
reprojected_topic_.c_str(), camera_topic_.c_str());
|
2026-04-06 12:18:55 +08:00
|
|
|
RCLCPP_INFO(this->get_logger(), "Publishing compressed hcat to: %s (jpeg q=%d)",
|
|
|
|
|
output_topic_.c_str(), jpeg_quality_);
|
2026-04-02 16:11:40 +08:00
|
|
|
|
|
|
|
|
reproj_sub_ = this->create_subscription<Image>(
|
|
|
|
|
reprojected_topic_, 10,
|
|
|
|
|
std::bind(&ImageOverlayNode::reprojCallback, this, std::placeholders::_1));
|
2026-04-06 12:18:55 +08:00
|
|
|
|
2026-04-02 16:11:40 +08:00
|
|
|
camera_sub_ = this->create_subscription<Image>(
|
|
|
|
|
camera_topic_, 10,
|
|
|
|
|
std::bind(&ImageOverlayNode::cameraCallback, this, std::placeholders::_1));
|
|
|
|
|
|
2026-04-06 12:18:55 +08:00
|
|
|
combined_pub_ = this->create_publisher<CompressedImage>(output_topic_, 10);
|
2026-04-02 16:11:40 +08:00
|
|
|
|
2026-04-06 12:18:55 +08:00
|
|
|
RCLCPP_INFO(this->get_logger(), "ImageOverlayNode: left=camera, right=reprojected, no-sync mode");
|
2026-04-02 16:11:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ImageOverlayNode::reprojCallback(const Image::ConstSharedPtr& msg)
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
cv_bridge::CvImagePtr cv_ptr = cv_bridge::toCvCopy(msg, "bgr8");
|
|
|
|
|
{
|
|
|
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
|
|
|
latest_reproj_img_ = cv_ptr->image.clone();
|
2026-04-06 12:18:55 +08:00
|
|
|
latest_reproj_header_ = msg->header;
|
2026-04-02 16:11:40 +08:00
|
|
|
}
|
|
|
|
|
} catch (cv_bridge::Exception& e) {
|
|
|
|
|
RCLCPP_ERROR(this->get_logger(), "cv_bridge exception (reproj): %s", e.what());
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-04-06 12:18:55 +08:00
|
|
|
publishHcatCompressed();
|
2026-04-02 16:11:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ImageOverlayNode::cameraCallback(const Image::ConstSharedPtr& msg)
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
cv_bridge::CvImagePtr cv_ptr = cv_bridge::toCvCopy(msg, "bgr8");
|
|
|
|
|
{
|
|
|
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
|
|
|
latest_camera_img_ = cv_ptr->image.clone();
|
2026-04-06 12:18:55 +08:00
|
|
|
latest_camera_header_ = msg->header;
|
2026-04-02 16:11:40 +08:00
|
|
|
}
|
|
|
|
|
} catch (cv_bridge::Exception& e) {
|
|
|
|
|
RCLCPP_ERROR(this->get_logger(), "cv_bridge exception (camera): %s", e.what());
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-04-06 12:18:55 +08:00
|
|
|
publishHcatCompressed();
|
2026-04-02 16:11:40 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-06 12:18:55 +08:00
|
|
|
void ImageOverlayNode::publishHcatCompressed()
|
2026-04-02 16:11:40 +08:00
|
|
|
{
|
2026-04-06 12:18:55 +08:00
|
|
|
cv::Mat reproj_copy;
|
|
|
|
|
cv::Mat camera_copy;
|
|
|
|
|
std_msgs::msg::Header out_header;
|
|
|
|
|
|
2026-04-02 16:11:40 +08:00
|
|
|
{
|
|
|
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
|
|
|
if (latest_reproj_img_.empty() || latest_camera_img_.empty()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
reproj_copy = latest_reproj_img_.clone();
|
|
|
|
|
camera_copy = latest_camera_img_.clone();
|
2026-04-06 12:18:55 +08:00
|
|
|
out_header = latest_camera_header_;
|
2026-04-02 16:11:40 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-06 12:18:55 +08:00
|
|
|
const int H = std::max(camera_copy.rows, reproj_copy.rows);
|
|
|
|
|
cv::Mat left = resizeToHeight(camera_copy, H);
|
|
|
|
|
cv::Mat right = resizeToHeight(reproj_copy, H);
|
|
|
|
|
|
|
|
|
|
cv::Mat combined;
|
|
|
|
|
cv::hconcat(left, right, combined);
|
|
|
|
|
|
|
|
|
|
std::vector<uchar> buf;
|
|
|
|
|
const std::vector<int> enc_params = {cv::IMWRITE_JPEG_QUALITY, jpeg_quality_};
|
|
|
|
|
if (!cv::imencode(".jpg", combined, buf, enc_params)) {
|
|
|
|
|
RCLCPP_ERROR(this->get_logger(), "cv::imencode failed");
|
2026-04-02 16:11:40 +08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-06 12:18:55 +08:00
|
|
|
CompressedImage out;
|
|
|
|
|
out.header = out_header;
|
|
|
|
|
out.format = "jpeg";
|
|
|
|
|
out.data.assign(buf.begin(), buf.end());
|
|
|
|
|
combined_pub_->publish(out);
|
2026-04-02 16:11:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ==================== ROS2 Main ====================
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
|
{
|
|
|
|
|
rclcpp::init(argc, argv);
|
2026-04-06 12:18:55 +08:00
|
|
|
|
2026-04-02 16:11:40 +08:00
|
|
|
auto node = std::make_shared<ImageOverlayNode>();
|
2026-04-06 12:18:55 +08:00
|
|
|
|
2026-04-02 16:11:40 +08:00
|
|
|
rclcpp::spin(node);
|
|
|
|
|
rclcpp::shutdown();
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
// ==================== ROS1 Implementation ====================
|
|
|
|
|
|
|
|
|
|
ImageOverlayNode::ImageOverlayNode(ros::NodeHandle& nh, ros::NodeHandle& pnh)
|
|
|
|
|
: nh_(nh), pnh_(pnh)
|
|
|
|
|
{
|
|
|
|
|
pnh_.param<std::string>("register_keys/overlay_reprojected_topic", reprojected_topic_, "/odin1/reprojected_image");
|
|
|
|
|
pnh_.param<std::string>("register_keys/overlay_camera_topic", camera_topic_, "/odin1/image/undistorted");
|
2026-04-06 12:18:55 +08:00
|
|
|
pnh_.param<std::string>("register_keys/overlay_output_topic", output_topic_, "/odin1/combined_image/compressed");
|
|
|
|
|
int q = 85;
|
|
|
|
|
pnh_.param<int>("register_keys/overlay_jpeg_quality", q, 85);
|
|
|
|
|
jpeg_quality_ = std::max(1, std::min(100, q));
|
2026-04-02 16:11:40 +08:00
|
|
|
|
|
|
|
|
ROS_INFO("Subscribing to: %s and %s", reprojected_topic_.c_str(), camera_topic_.c_str());
|
2026-04-06 12:18:55 +08:00
|
|
|
ROS_INFO("Publishing compressed hcat to: %s (jpeg q=%d)", output_topic_.c_str(), jpeg_quality_);
|
2026-04-02 16:11:40 +08:00
|
|
|
|
|
|
|
|
reproj_sub_ = nh_.subscribe(reprojected_topic_, 10, &ImageOverlayNode::reprojCallback, this);
|
|
|
|
|
camera_sub_ = nh_.subscribe(camera_topic_, 10, &ImageOverlayNode::cameraCallback, this);
|
|
|
|
|
|
2026-04-06 12:18:55 +08:00
|
|
|
combined_pub_ = nh_.advertise<sensor_msgs::CompressedImage>(output_topic_, 10);
|
2026-04-02 16:11:40 +08:00
|
|
|
|
2026-04-06 12:18:55 +08:00
|
|
|
ROS_INFO("ImageOverlayNode: left=camera, right=reprojected, no-sync mode");
|
2026-04-02 16:11:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ImageOverlayNode::reprojCallback(const sensor_msgs::ImageConstPtr& msg)
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
cv_bridge::CvImagePtr cv_ptr = cv_bridge::toCvCopy(msg, "bgr8");
|
|
|
|
|
{
|
|
|
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
|
|
|
latest_reproj_img_ = cv_ptr->image.clone();
|
2026-04-06 12:18:55 +08:00
|
|
|
latest_reproj_header_ = msg->header;
|
2026-04-02 16:11:40 +08:00
|
|
|
}
|
|
|
|
|
} catch (cv_bridge::Exception& e) {
|
|
|
|
|
ROS_ERROR("cv_bridge exception (reproj): %s", e.what());
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-04-06 12:18:55 +08:00
|
|
|
publishHcatCompressed();
|
2026-04-02 16:11:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ImageOverlayNode::cameraCallback(const sensor_msgs::ImageConstPtr& msg)
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
cv_bridge::CvImagePtr cv_ptr = cv_bridge::toCvCopy(msg, "bgr8");
|
|
|
|
|
{
|
|
|
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
|
|
|
latest_camera_img_ = cv_ptr->image.clone();
|
2026-04-06 12:18:55 +08:00
|
|
|
latest_camera_header_ = msg->header;
|
2026-04-02 16:11:40 +08:00
|
|
|
}
|
|
|
|
|
} catch (cv_bridge::Exception& e) {
|
|
|
|
|
ROS_ERROR("cv_bridge exception (camera): %s", e.what());
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-04-06 12:18:55 +08:00
|
|
|
publishHcatCompressed();
|
2026-04-02 16:11:40 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-06 12:18:55 +08:00
|
|
|
void ImageOverlayNode::publishHcatCompressed()
|
2026-04-02 16:11:40 +08:00
|
|
|
{
|
2026-04-06 12:18:55 +08:00
|
|
|
cv::Mat reproj_copy;
|
|
|
|
|
cv::Mat camera_copy;
|
|
|
|
|
std_msgs::Header out_header;
|
|
|
|
|
|
2026-04-02 16:11:40 +08:00
|
|
|
{
|
|
|
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
|
|
|
if (latest_reproj_img_.empty() || latest_camera_img_.empty()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
reproj_copy = latest_reproj_img_.clone();
|
|
|
|
|
camera_copy = latest_camera_img_.clone();
|
2026-04-06 12:18:55 +08:00
|
|
|
out_header = latest_camera_header_;
|
2026-04-02 16:11:40 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-06 12:18:55 +08:00
|
|
|
const int H = std::max(camera_copy.rows, reproj_copy.rows);
|
|
|
|
|
cv::Mat left = resizeToHeight(camera_copy, H);
|
|
|
|
|
cv::Mat right = resizeToHeight(reproj_copy, H);
|
|
|
|
|
|
|
|
|
|
cv::Mat combined;
|
|
|
|
|
cv::hconcat(left, right, combined);
|
|
|
|
|
|
|
|
|
|
std::vector<uchar> buf;
|
|
|
|
|
const std::vector<int> enc_params = {cv::IMWRITE_JPEG_QUALITY, jpeg_quality_};
|
|
|
|
|
if (!cv::imencode(".jpg", combined, buf, enc_params)) {
|
|
|
|
|
ROS_ERROR("cv::imencode failed");
|
2026-04-02 16:11:40 +08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-06 12:18:55 +08:00
|
|
|
sensor_msgs::CompressedImage out;
|
|
|
|
|
out.header = out_header;
|
|
|
|
|
out.format = "jpeg";
|
|
|
|
|
out.data.assign(buf.begin(), buf.end());
|
|
|
|
|
combined_pub_.publish(out);
|
2026-04-02 16:11:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ==================== ROS1 Main ====================
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
|
{
|
|
|
|
|
ros::init(argc, argv, "image_overlay_node");
|
|
|
|
|
ros::NodeHandle nh;
|
|
|
|
|
ros::NodeHandle pnh("~");
|
|
|
|
|
|
|
|
|
|
ImageOverlayNode node(nh, pnh);
|
|
|
|
|
|
|
|
|
|
ros::spin();
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
#endif
|