Depth image Convert 16UC1 to 32FC1 - Dieptranivsr/DroneIVSR GitHub Wiki

Check parameter:

  1. check.cpp
  2. CMakeLists.txt
  3. package.xml

check.cpp

#include <iostream>

#include <ros/ros.h>

#include <cv_bridge/cv_bridge.h>

ros::Subscriber depth_image_sub;
ros::Publisher changeDepth;
cv::Mat depth_image;

void DepthCallback(const sensor_msgs::ImageConstPtr& img)
{
  cv_bridge::CvImagePtr cv_ptr;
  cv_ptr = cv_bridge::toCvCopy(img, img->encoding);

  if (img->encoding == sensor_msgs::image_encodings::TYPE_16UC1) {
    (cv_ptr->image).convertTo(cv_ptr->image, CV_32FC1);
  }

  cv_ptr->image.copyTo(depth_image);
  cv_ptr->header = img->header;    
  cv_ptr->encoding = sensor_msgs::image_encodings::TYPE_32FC1;

  changeDepth.publish(cv_ptr);
}

int main(int argc, char** argv) {
  ros::init(argc, argv, "check_depthcamera");
  ros::NodeHandle nh;

  depth_image_sub = nh.subscribe("camera/depth/image_rect_raw", 10, &DepthCallback);
  changeDepth = nh.advertise<sensor_msgs::Image>("/camera/ChangeDepth", 10);

  ros::Duration(1.0).sleep();
  ros::spin();

  return 0;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.0.2)
project(check_depthcam)

find_package(OpenCV REQUIRED)

find_package(catkin REQUIRED COMPONENTS
  roscpp
  cv_bridge
)

## System depend
find_package(Eigen3 REQUIRED)
find_package(PCL 1.7 REQUIRED)

catkin_package(
# INCLUDE_DIRS include
# LIBRARIES check_depthcamera
# CATKIN_DEPENDS roscpp
#  DEPENDS system_lib
)

include_directories( 
    SYSTEM 
    include 
    ${catkin_INCLUDE_DIRS}
    ${Eigen3_INCLUDE_DIRS} 
    ${PCL_INCLUDE_DIRS}
    ${OpenCV_INCLUDE_DIRS}
)

link_directories(${PCL_LIBRARY_DIRS})

set(CMAKE_CXX_FLAGS "-std=c++11 ${CMAKE_CXX_FLAGS} -O3 -Wall")

add_executable(check_depthcam
    src/check.cpp 
    )

target_link_libraries( check_depthcam
    ${catkin_LIBRARIES} 
    ${PCL_LIBRARIES}
    )  

package.xml

<?xml version="1.0"?>
<package format="2">
  <name>check_depthcam</name>
  <version>0.0.0</version>
  <description>The check_depthcam package</description>

  <maintainer email="[email protected]">dieptran</maintainer>

  <license>TODO</license>

  <buildtool_depend>catkin</buildtool_depend>
  <build_depend>roscpp</build_depend>
  <build_export_depend>roscpp</build_export_depend>
  <exec_depend>roscpp</exec_depend>


  <!-- The export tag contains other, unspecified, tags -->
  <export>
    <!-- Other tools can request additional information be placed here -->

  </export>
</package>
⚠️ **GitHub.com Fallback** ⚠️