I am trying to write a custom ROS2 node in c++ that subscribes to /depth_to_rgb/image_raw coming from Azure Kinect of type 'sensor_msgs/msg/Image' topic. Then I am trying to get a depth value at a hard-coded pixel value of (10,30).

I am using image_transport for the first time in C++ and I have got the following code so far but it is not subscribing to /depth_to_rgb/image_raw as seen from rqt_graph.

The line in the code "RCLCPP_INFO(get_logger(), "Subscribed!");" doesn't get printed at all.

Any idea why it may not be subscribing? Thank you!

+++ Edit ++++

Okay so I've found my own answer to this problem: blindly using chatgpt to debug complier errors. The problematic line was "image_transport_(rclcpp::Node::make_shared("image_transport_node")" in the constructor. This was creating a node that subscribed to the topic.

I've actually found the answer to this problem via https://answers.ros.org/question/403966/how-to-initialize-image_transport-using-rclcpp/. I had hard time trying to initialise image_transport and the person answered it with "shared pointer with an empty deleter function to be able to pass to Image Transport". Can someone please answer with plain English why it works and what the problem is? I can only follow other people's solution but I can't explain what's going on without too much experience in c++.

#include <chrono>
#include <functional>
#include <memory>
#include <string>

#include "rclcpp/rclcpp.hpp"
#include "image_transport/image_transport.hpp"
#include "cv_bridge/cv_bridge.h"
#include "std_msgs/msg/string.hpp"
#include "sensor_msgs/msg/image.hpp"

class DepthPublisherNode : public rclcpp::Node
    DepthPublisherNode() : Node("depth_publisher_node"), image_transport_(rclcpp::Node::make_shared("image_transport_node"))
        // Define the imageCallback as a member function
        RCLCPP_INFO(get_logger(), "Before subscribing");
        image_sub_ = image_transport_.subscribe("/depth_to_rgb/image_raw", 10, std::bind(&DepthPublisherNode::imageCallback, this, std::placeholders::_1));
        RCLCPP_INFO(get_logger(), "After subscribing");

    image_transport::ImageTransport image_transport_;
    image_transport::Subscriber image_sub_;

    // Corrected imageCallback signature
    void imageCallback(const sensor_msgs::msg::Image::ConstSharedPtr& msg)
        RCLCPP_INFO(get_logger(), "Subscribed!");
        cv_bridge::CvImageConstPtr cv_ptr = cv_bridge::toCvShare(msg, sensor_msgs::image_encodings::TYPE_32FC1);
        float depth = cv_ptr->image.at<float>(10, 30);
        RCLCPP_INFO(get_logger(), "Image Received. Depth at pixel (10, 30): %f", depth);

int main(int argc, char *argv[])
    rclcpp::init(argc, argv);

    auto node = std::make_shared<DepthPublisherNode>();

    RCLCPP_INFO(node->get_logger(), "Starting depth publisher node");

    RCLCPP_INFO(node->get_logger(), "Shutting down depth publisher node");

    return 0;

  • $\begingroup$ Welcome to Robotics Stack Exchange! This may help: my_class_subscriber.cpp $\endgroup$
    – ravi
    Commented Dec 11, 2023 at 10:22


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.