// Copyright 2008, 2019 Willow Garage, Inc., Andreas Klintberg, Joshua Whitley // All rights reserved. // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are met: // // * Redistributions of source code must retain the above copyright // notice, this list of conditions and the following disclaimer. // // * Redistributions in binary form must reproduce the above copyright // notice, this list of conditions and the following disclaimer in the // documentation and/or other materials provided with the distribution. // // * Neither the name of the copyright holder nor the names of its // contributors may be used to endorse or promote products derived from // this software without specific prior written permission. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE // POSSIBILITY OF SUCH DAMAGE. #include #include #include #include "cv_bridge/cv_bridge.hpp" #include "tracetools_image_pipeline/tracetools.h" #include #include #include #include #include #include #include namespace image_proc { RectifyNode::RectifyNode(const rclcpp::NodeOptions & options) : rclcpp::Node("RectifyNode", options) { // TransportHints does not actually declare the parameter this->declare_parameter("image_transport", "raw"); // For compressed topics to remap appropriately, we need to pass a // fully expanded and remapped topic name to image_transport auto node_base = this->get_node_base_interface(); image_topic_ = node_base->resolve_topic_or_service_name("image", false); std::string rect_topic = node_base->resolve_topic_or_service_name("image_rect", false); queue_size_ = this->declare_parameter("queue_size", 5); interpolation_ = this->declare_parameter("interpolation", 1); // Setup lazy subscriber using publisher connection callback rclcpp::PublisherOptions pub_options; pub_options.event_callbacks.matched_callback = [this](rclcpp::MatchedInfo &) { if (pub_rect_.getNumSubscribers() == 0) { sub_camera_.shutdown(); } else if (!sub_camera_) { // Create subscriber with QoS matched to subscribed topic publisher auto qos_profile = getQosProfile(this, image_topic_); image_transport::TransportHints hints(*this); sub_camera_ = image_transport::create_camera_subscription( *this, image_topic_, [this]( const sensor_msgs::msg::Image::ConstSharedPtr & image_msg, const sensor_msgs::msg::CameraInfo::ConstSharedPtr & info_msg) { imageCb(image_msg, info_msg); }, hints.getTransport(), qos_profile); } }; // Create publisher - allow overriding QoS settings (history, depth, reliability) pub_options.qos_overriding_options = rclcpp::QosOverridingOptions::with_default_policies(); pub_rect_ = image_transport::create_publisher(*this, rect_topic, rclcpp::SystemDefaultsQoS(), pub_options); } void RectifyNode::imageCb( const sensor_msgs::msg::Image::ConstSharedPtr & image_msg, const sensor_msgs::msg::CameraInfo::ConstSharedPtr & info_msg) { TRACEPOINT( image_proc_rectify_init, static_cast(this), static_cast(&(*image_msg)), static_cast(&(*info_msg))); if (pub_rect_.getNumSubscribers() < 1) { TRACEPOINT( image_proc_rectify_fini, static_cast(this), static_cast(&(*image_msg)), static_cast(&(*info_msg))); return; } // Verify camera is actually calibrated if (info_msg->k[0] == 0.0) { RCLCPP_ERROR( this->get_logger(), "Rectified topic '%s' requested but camera publishing '%s' " "is uncalibrated", pub_rect_.getTopic().c_str(), sub_camera_.getInfoTopic().c_str()); TRACEPOINT( image_proc_rectify_fini, static_cast(this), static_cast(&(*image_msg)), static_cast(&(*info_msg))); return; } // If zero distortion, just pass the message along bool zero_distortion = true; for (size_t i = 0; i < info_msg->d.size(); ++i) { if (info_msg->d[i] != 0.0) { zero_distortion = false; break; } } // This will be true if D is empty/zero sized if (zero_distortion) { pub_rect_.publish(image_msg); TRACEPOINT( image_proc_rectify_fini, static_cast(this), static_cast(&(*image_msg)), static_cast(&(*info_msg))); return; } // Update the camera model model_.fromCameraInfo(info_msg); // Create cv::Mat views onto both buffers const cv::Mat image = cv_bridge::toCvShare(image_msg)->image; cv::Mat rect; // Rectify and publish model_.rectifyImage(image, rect, interpolation_); // Allocate new rectified image message auto rect_msg = std::make_unique(); cv_bridge::CvImage(image_msg->header, image_msg->encoding, rect).toImageMsg(*rect_msg); pub_rect_.publish(std::move(rect_msg)); TRACEPOINT( image_proc_rectify_fini, static_cast(this), static_cast(&(*image_msg)), static_cast(&(*info_msg))); } } // namespace image_proc #include "rclcpp_components/register_node_macro.hpp" // Register the component with class_loader. // This acts as a sort of entry point, allowing the component to be discoverable when its library // is being loaded into a running process. RCLCPP_COMPONENTS_REGISTER_NODE(image_proc::RectifyNode)