Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
Collapse
Brand Logo

ModalAI Forum

  1. ModalAI Support Forum
  2. Software Development
  3. ROS
  4. /tof_depth image conversion problem

/tof_depth image conversion problem

Scheduled Pinned Locked Moved ROS
4 Posts 2 Posters 2.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Judoor 0
    Regular
    wrote on last edited by
    #1

    Hello,
    I would like to convert the depth image from mono8 to UC16 while maintaining accurate depth references.
    Currently, I am doing this, but I am ending up with a flat image.

    void depthCallback(const sensor_msgs::ImageConstPtr &msg)
         {
             try
             {
    
                 // Convert the input ROS image to an OpenCV image
                 cv_bridge::CvImagePtr cv_ptr = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::MONO8);
    
                 // Create a new image in CV_16UC1 format
                 cv::Mat converted_image;
                 cv_ptr->image.convertTo(converted_image, CV_16UC1, 256.0); // Scale mono8 values to 16-bit
    
                 // Publish the converted image
                 cv_bridge::CvImage out_msg;
                 out_msg.header = msg->header; // Keep the same header
                 out_msg.encoding = sensor_msgs::image_encodings::TYPE_16UC1;
                 out_msg.image = converted_image;
                 depth_pub_.publish(out_msg.toImageMsg());
                 // depth_pub_.publish(depth_msg);
             }
             catch (cv_bridge::Exception &e)
             {
                 ROS_ERROR("CvBridge Error in depthCallback: %s", e.what());
             }
         }
    

    Also, I want to mix it with an RGB image to get an RGBD image, I'm using the rgbdsync nodelet from rtabmap_ros. But I'm unable to make it work. Does someone already did that ?
    Julien

    J 1 Reply Last reply
    0
    • J Judoor 0

      Hello,
      I would like to convert the depth image from mono8 to UC16 while maintaining accurate depth references.
      Currently, I am doing this, but I am ending up with a flat image.

      void depthCallback(const sensor_msgs::ImageConstPtr &msg)
           {
               try
               {
      
                   // Convert the input ROS image to an OpenCV image
                   cv_bridge::CvImagePtr cv_ptr = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::MONO8);
      
                   // Create a new image in CV_16UC1 format
                   cv::Mat converted_image;
                   cv_ptr->image.convertTo(converted_image, CV_16UC1, 256.0); // Scale mono8 values to 16-bit
      
                   // Publish the converted image
                   cv_bridge::CvImage out_msg;
                   out_msg.header = msg->header; // Keep the same header
                   out_msg.encoding = sensor_msgs::image_encodings::TYPE_16UC1;
                   out_msg.image = converted_image;
                   depth_pub_.publish(out_msg.toImageMsg());
                   // depth_pub_.publish(depth_msg);
               }
               catch (cv_bridge::Exception &e)
               {
                   ROS_ERROR("CvBridge Error in depthCallback: %s", e.what());
               }
           }
      

      Also, I want to mix it with an RGB image to get an RGBD image, I'm using the rgbdsync nodelet from rtabmap_ros. But I'm unable to make it work. Does someone already did that ?
      Julien

      J Offline
      J Offline
      Judoor 0
      Regular
      wrote on last edited by
      #2

      Can you help me with the depth image conversion ?

      Alex KushleyevA 1 Reply Last reply
      0
      • J Judoor 0

        Can you help me with the depth image conversion ?

        Alex KushleyevA Offline
        Alex KushleyevA Offline
        Alex Kushleyev
        ModalAI Team
        wrote on last edited by Alex Kushleyev
        #3

        @Judoor-0 , can you please clarify what you mean a "flat image" ?

        Also, it seems you can convert the type to MONO16 right in toCvCopy call. Take a look at the implementation, which will automatically re-scale the data (from MONO8 to MONO16), so you will not lose any information (because in this case you will be upscaling, that is going from 8 bit to 16 bit): https://github.com/strawlab/vision_opencv/blob/master/cv_bridge/src/cv_bridge.cpp#L174

        So you can probably just have:

        cv_bridge::CvImagePtr cv_ptr = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::MONO16);
        
        // Publish the converted image
        cv_bridge::CvImage out_msg;
        out_msg.header = msg->header; // Keep the same header
        out_msg.encoding = sensor_msgs::image_encodings::TYPE_16UC1;
        out_msg.image = cv_ptr->image;
        depth_pub_.publish(out_msg.toImageMsg());
        

        or i think you can just do the following because toCvCopy will automatically copy the header and update the encoding for you:

        cv_bridge::CvImagePtr cv_ptr = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::MONO16);
        depth_pub_.publish(cv_ptr->toImageMsg());
        

        I have not tried running it, but hopefully should point you in the right direction!

        Alex

        J 1 Reply Last reply
        0
        • Alex KushleyevA Alex Kushleyev

          @Judoor-0 , can you please clarify what you mean a "flat image" ?

          Also, it seems you can convert the type to MONO16 right in toCvCopy call. Take a look at the implementation, which will automatically re-scale the data (from MONO8 to MONO16), so you will not lose any information (because in this case you will be upscaling, that is going from 8 bit to 16 bit): https://github.com/strawlab/vision_opencv/blob/master/cv_bridge/src/cv_bridge.cpp#L174

          So you can probably just have:

          cv_bridge::CvImagePtr cv_ptr = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::MONO16);
          
          // Publish the converted image
          cv_bridge::CvImage out_msg;
          out_msg.header = msg->header; // Keep the same header
          out_msg.encoding = sensor_msgs::image_encodings::TYPE_16UC1;
          out_msg.image = cv_ptr->image;
          depth_pub_.publish(out_msg.toImageMsg());
          

          or i think you can just do the following because toCvCopy will automatically copy the header and update the encoding for you:

          cv_bridge::CvImagePtr cv_ptr = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::MONO16);
          depth_pub_.publish(cv_ptr->toImageMsg());
          

          I have not tried running it, but hopefully should point you in the right direction!

          Alex

          J Offline
          J Offline
          Judoor 0
          Regular
          wrote on last edited by
          #4

          @Alex-Kushleyev thank you alex i'll try that

          1 Reply Last reply
          0

          Hello! It looks like you're interested in this conversation, but you don't have an account yet.

          Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

          With your input, this post could be even better 💗

          Register Login
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          ModalAI
          Categories Recent Tags ModalAI.com Docs
          © 2026 ModalAI® · Accelerating autonomy for smaller, smarter, safer drones · Powered by NodeBB
          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups