mirror of
https://github.com/introlab/rtabmap_ros.git
synced 2026-10-03 16:27:46 +08:00
Added pointcloud_to_depthimage node, supporting bgra8/rgba8 format as well as smaller depth image than rgb
This commit is contained in:
+4
-1
@@ -18,7 +18,7 @@ find_package(rviz)
|
||||
|
||||
## System dependencies are found with CMake's conventions
|
||||
# find_package(Boost REQUIRED COMPONENTS system)
|
||||
find_package(RTABMap 0.11.13 REQUIRED)
|
||||
find_package(RTABMap 0.12.1 REQUIRED)
|
||||
|
||||
find_package(OpenCV REQUIRED)
|
||||
|
||||
@@ -321,6 +321,9 @@ target_link_libraries(data_player rtabmap_ros ${QT_LIBRARIES} ${Libraries})
|
||||
add_executable(odom_msg_to_tf src/OdomMsgToTFNode.cpp)
|
||||
target_link_libraries(odom_msg_to_tf rtabmap_ros ${Libraries})
|
||||
|
||||
add_executable(pointcloud_to_depthimage src/PointCloudToDepthImageNode.cpp)
|
||||
target_link_libraries(pointcloud_to_depthimage rtabmap_ros ${Libraries})
|
||||
|
||||
#############
|
||||
## Install ##
|
||||
#############
|
||||
|
||||
+25
-9
@@ -1088,6 +1088,14 @@ bool convertRGBDMsgs(
|
||||
|
||||
int imageWidth = imageMsgs[0]->image.cols;
|
||||
int imageHeight = imageMsgs[0]->image.rows;
|
||||
int depthWidth = depthMsgs[0]->image.cols;
|
||||
int depthHeight = depthMsgs[0]->image.rows;
|
||||
|
||||
UASSERT_MSG(
|
||||
imageWidth % depthWidth == 0 && imageHeight % depthHeight == 0 &&
|
||||
imageWidth/depthWidth == imageHeight/depthHeight,
|
||||
uFormat("rgb=%dx%d depth=%dx%d", imageWidth, imageHeight, depthWidth, depthHeight).c_str());
|
||||
|
||||
int cameraCount = imageMsgs.size();
|
||||
for(unsigned int i=0; i<imageMsgs.size(); ++i)
|
||||
{
|
||||
@@ -1095,12 +1103,17 @@ bool convertRGBDMsgs(
|
||||
imageMsgs[i]->encoding.compare(sensor_msgs::image_encodings::MONO8) ==0 ||
|
||||
imageMsgs[i]->encoding.compare(sensor_msgs::image_encodings::MONO16) ==0 ||
|
||||
imageMsgs[i]->encoding.compare(sensor_msgs::image_encodings::BGR8) == 0 ||
|
||||
imageMsgs[i]->encoding.compare(sensor_msgs::image_encodings::RGB8) == 0) ||
|
||||
imageMsgs[i]->encoding.compare(sensor_msgs::image_encodings::RGB8) == 0 ||
|
||||
imageMsgs[i]->encoding.compare(sensor_msgs::image_encodings::BGRA8) == 0 ||
|
||||
imageMsgs[i]->encoding.compare(sensor_msgs::image_encodings::RGBA8) == 0) ||
|
||||
!(depthMsgs[i]->encoding.compare(sensor_msgs::image_encodings::TYPE_16UC1) == 0 ||
|
||||
depthMsgs[i]->encoding.compare(sensor_msgs::image_encodings::TYPE_32FC1) == 0 ||
|
||||
depthMsgs[i]->encoding.compare(sensor_msgs::image_encodings::MONO16) == 0))
|
||||
{
|
||||
ROS_ERROR("Input type must be image=mono8,mono16,rgb8,bgr8 and image_depth=32FC1,16UC1,mono16");
|
||||
ROS_ERROR("Input type must be image=mono8,mono16,rgb8,bgr8,bgra8,rgba8 and "
|
||||
"image_depth=32FC1,16UC1,mono16. Current rgb=%s and depth=%s",
|
||||
imageMsgs[i]->encoding.c_str(),
|
||||
depthMsgs[i]->encoding.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -1110,11 +1123,11 @@ bool convertRGBDMsgs(
|
||||
imageMsgs[i]->image.cols,
|
||||
imageHeight,
|
||||
imageMsgs[i]->image.rows).c_str());
|
||||
UASSERT_MSG(depthMsgs[i]->image.cols == imageWidth && depthMsgs[i]->image.rows == imageHeight,
|
||||
uFormat("imageWidth=%d vs %d imageHeight=%d vs %d",
|
||||
imageWidth,
|
||||
UASSERT_MSG(depthMsgs[i]->image.cols == depthWidth && depthMsgs[i]->image.rows == depthHeight,
|
||||
uFormat("depthWidth=%d vs %d imageHeight=%d vs %d",
|
||||
depthWidth,
|
||||
depthMsgs[i]->image.cols,
|
||||
imageHeight,
|
||||
depthHeight,
|
||||
depthMsgs[i]->image.rows).c_str());
|
||||
|
||||
rtabmap::Transform localTransform = rtabmap_ros::getTransform(frameId, depthMsgs[i]->header.frame_id, depthMsgs[i]->header.stamp, listener, waitForTransform);
|
||||
@@ -1170,7 +1183,7 @@ bool convertRGBDMsgs(
|
||||
}
|
||||
if(depth.empty())
|
||||
{
|
||||
depth = cv::Mat(imageHeight, imageWidth*cameraCount, subDepth.type());
|
||||
depth = cv::Mat(depthHeight, depthWidth*cameraCount, subDepth.type());
|
||||
}
|
||||
|
||||
if(ptrImage->image.type() == rgb.type())
|
||||
@@ -1185,7 +1198,7 @@ bool convertRGBDMsgs(
|
||||
|
||||
if(subDepth.type() == depth.type())
|
||||
{
|
||||
subDepth.copyTo(cv::Mat(depth, cv::Rect(i*imageWidth, 0, imageWidth, imageHeight)));
|
||||
subDepth.copyTo(cv::Mat(depth, cv::Rect(i*depthWidth, 0, depthWidth, depthHeight)));
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -1224,7 +1237,10 @@ bool convertStereoMsg(
|
||||
rightImageMsg->encoding.compare(sensor_msgs::image_encodings::BGR8) == 0 ||
|
||||
rightImageMsg->encoding.compare(sensor_msgs::image_encodings::RGB8) == 0))
|
||||
{
|
||||
ROS_ERROR("Input type must be image=mono8,mono16,rgb8,bgr8");
|
||||
ROS_ERROR("Input type must be image=mono8,mono16,rgb8,bgr8,bgra8,rgba8");
|
||||
ROS_ERROR("Input type must be image=mono8,mono16,rgb8,bgr8,bgra8,rgba8 Current left=%s and right=%s",
|
||||
leftImageMsg->encoding.c_str(),
|
||||
rightImageMsg->encoding.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,194 @@
|
||||
/*
|
||||
Copyright (c) 2010-2016, Mathieu Labbe - IntRoLab - Universite de Sherbrooke
|
||||
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 Universite de Sherbrooke 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 <ros/ros.h>
|
||||
#include <ros/publisher.h>
|
||||
#include <ros/subscriber.h>
|
||||
|
||||
#include <rtabmap_ros/MsgConversion.h>
|
||||
#include <rtabmap/core/util3d.h>
|
||||
#include <rtabmap/core/util2d.h>
|
||||
#include <rtabmap/utilite/ULogger.h>
|
||||
|
||||
#include <sensor_msgs/PointCloud2.h>
|
||||
#include <sensor_msgs/Image.h>
|
||||
#include <sensor_msgs/image_encodings.h>
|
||||
#include <sensor_msgs/CameraInfo.h>
|
||||
|
||||
#include <image_transport/image_transport.h>
|
||||
|
||||
#include <pcl/point_cloud.h>
|
||||
#include <pcl/point_types.h>
|
||||
#include <pcl_conversions/pcl_conversions.h>
|
||||
|
||||
#include <message_filters/sync_policies/approximate_time.h>
|
||||
#include <message_filters/sync_policies/exact_time.h>
|
||||
#include <message_filters/subscriber.h>
|
||||
|
||||
image_transport::Publisher depthImage16Pub;
|
||||
image_transport::Publisher depthImage32Pub;
|
||||
std::string fixedFrameId;
|
||||
tf::TransformListener * listener;
|
||||
double waitForTransform = 0.1;
|
||||
int fillHolesSize = 0;
|
||||
double fillHolesError = 0.1;
|
||||
int decimation = 1;
|
||||
|
||||
void callback(
|
||||
const sensor_msgs::PointCloud2ConstPtr & pointCloud2Msg,
|
||||
const sensor_msgs::CameraInfoConstPtr & cameraInfoMsg)
|
||||
{
|
||||
if(depthImage32Pub.getNumSubscribers() > 0 || depthImage16Pub.getNumSubscribers() > 0)
|
||||
{
|
||||
rtabmap::Transform cloudDisplacement = rtabmap_ros::getTransform(
|
||||
pointCloud2Msg->header.frame_id,
|
||||
fixedFrameId,
|
||||
pointCloud2Msg->header.stamp,
|
||||
cameraInfoMsg->header.stamp,
|
||||
*listener,
|
||||
waitForTransform);
|
||||
|
||||
if(cloudDisplacement.isNull())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
rtabmap::Transform cloudToCamera = rtabmap_ros::getTransform(
|
||||
pointCloud2Msg->header.frame_id,
|
||||
cameraInfoMsg->header.frame_id,
|
||||
cameraInfoMsg->header.stamp,
|
||||
*listener,
|
||||
waitForTransform);
|
||||
|
||||
if(cloudToCamera.isNull())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
rtabmap::Transform localTransform = cloudToCamera*cloudDisplacement;
|
||||
|
||||
rtabmap::CameraModel model = rtabmap_ros::cameraModelFromROS(*cameraInfoMsg, localTransform);
|
||||
|
||||
if(decimation > 1)
|
||||
{
|
||||
if(model.imageWidth()%decimation == 0 && model.imageHeight()%decimation == 0)
|
||||
{
|
||||
model = model.scaled(1.0f/float(decimation));
|
||||
}
|
||||
else
|
||||
{
|
||||
ROS_ERROR("decimation (%d) not valid for image size %dx%d",
|
||||
decimation,
|
||||
model.imageWidth(),
|
||||
model.imageHeight());
|
||||
}
|
||||
}
|
||||
|
||||
pcl::PCLPointCloud2::Ptr cloud(new pcl::PCLPointCloud2);
|
||||
pcl_conversions::toPCL(*pointCloud2Msg, *cloud);
|
||||
|
||||
cv_bridge::CvImage depthImage;
|
||||
depthImage.image = rtabmap::util3d::projectCloudToCamera(model.imageSize(), model.K(), cloud, model.localTransform());
|
||||
|
||||
if(fillHolesSize > 0)
|
||||
{
|
||||
depthImage.image = rtabmap::util2d::fillDepthHoles(depthImage.image, fillHolesSize, fillHolesError);
|
||||
}
|
||||
|
||||
depthImage.header = cameraInfoMsg->header;
|
||||
|
||||
if(depthImage32Pub.getNumSubscribers())
|
||||
{
|
||||
depthImage.encoding = sensor_msgs::image_encodings::TYPE_32FC1;
|
||||
depthImage32Pub.publish(depthImage.toImageMsg());
|
||||
}
|
||||
|
||||
if(depthImage16Pub.getNumSubscribers())
|
||||
{
|
||||
depthImage.encoding = sensor_msgs::image_encodings::TYPE_16UC1;
|
||||
depthImage.image = rtabmap::util2d::cvtDepthFromFloat(depthImage.image);
|
||||
depthImage16Pub.publish(depthImage.toImageMsg());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
ros::init(argc, argv, "pointcloud_to_depthimage");
|
||||
|
||||
ULogger::setLevel(ULogger::kError);
|
||||
ULogger::setType(ULogger::kTypeConsole);
|
||||
|
||||
listener = new tf::TransformListener();
|
||||
|
||||
ros::NodeHandle nh;
|
||||
ros::NodeHandle pnh("~");
|
||||
|
||||
int queueSize = 10;
|
||||
pnh.param("queue_size", queueSize, queueSize);
|
||||
pnh.param("fixed_frame_id", fixedFrameId, fixedFrameId);
|
||||
pnh.param("wait_for_transform", waitForTransform, waitForTransform);
|
||||
pnh.param("fill_holes_size", fillHolesSize, fillHolesSize);
|
||||
pnh.param("fill_holes_error", fillHolesError, fillHolesError);
|
||||
pnh.param("decimation", decimation, decimation);
|
||||
|
||||
if(fixedFrameId.empty())
|
||||
{
|
||||
ROS_ERROR("fixed_frame_id should be set! If the robot "
|
||||
"is moving, it could be \"odom\". If not moving, it "
|
||||
"could be \"base_link\".");
|
||||
return -1;
|
||||
}
|
||||
|
||||
ROS_INFO("Params:");
|
||||
ROS_INFO(" queue_size=%d", queueSize);
|
||||
ROS_INFO(" fixed_frame_id=%s", fixedFrameId.c_str());
|
||||
ROS_INFO(" wait_for_transform=%fs", waitForTransform);
|
||||
ROS_INFO(" fill_holes_size=%d pixels (0=disabled)", fillHolesSize);
|
||||
ROS_INFO(" fill_holes_error=%f", fillHolesError);
|
||||
ROS_INFO(" decimation=%d", decimation);
|
||||
|
||||
image_transport::ImageTransport it(nh);
|
||||
depthImage16Pub = it.advertise("image_raw", 1); // 16 bits unsigned in mm
|
||||
depthImage32Pub = it.advertise("image", 1); // 32 bits float in meters
|
||||
|
||||
message_filters::Subscriber<sensor_msgs::PointCloud2> pointCloudSub;
|
||||
message_filters::Subscriber<sensor_msgs::CameraInfo> cameraInfoSub;
|
||||
|
||||
typedef message_filters::sync_policies::ApproximateTime<sensor_msgs::PointCloud2, sensor_msgs::CameraInfo> MyApproxSyncPolicy;
|
||||
message_filters::Synchronizer<MyApproxSyncPolicy> approxSync(MyApproxSyncPolicy(queueSize), pointCloudSub, cameraInfoSub);
|
||||
approxSync.registerCallback(boost::bind(&callback, _1, _2));
|
||||
|
||||
pointCloudSub.subscribe(nh, "cloud", 1);
|
||||
cameraInfoSub.subscribe(nh, "camera_info", 1);
|
||||
|
||||
ros::spin();
|
||||
|
||||
delete listener;
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user