使用cv::ximgproc::FastBilateralSolverFilter优化realsense D455的深度图

本文主要是介绍使用cv::ximgproc::FastBilateralSolverFilter优化realsense D455的深度图,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1 源码

OpenCV中内置了很多滤波器,这里我们讨论cv::ximgproc其中包含的滤波器。
https://docs.opencv.org/3.4/da/d17/group__ximgproc__filters.html
需要注意的是,默认安装的OpenCV中不包含cv::ximgproc,请从源码重修编译。
在这里插入图片描述
在这里贴上我的测试源码。

/*
cv::ximgproc::AdaptiveManifoldFilter
[86] Eduardo SL Gastal and Manuel M Oliveira. Adaptive manifolds for real-time high-dimensional filtering. ACM Transactions on Graphics (TOG), 31(4):33, 2012.cv::ximgproc::EdgeAwareInterpolator
[182] Jerome Revaud, Philippe Weinzaepfel, Zaid Harchaoui, and Cordelia Schmid. Epicflow: Edge-preserving interpolation of correspondences for optical flow. In Computer Vision and Pattern Recognition (CVPR), IEEE Conference on, pages 1164–1172, 2015.cv::ximgproc::DTFilter
[85] Eduardo SL Gastal and Manuel M Oliveira. Domain transform for edge-aware image and video processing. In ACM Transactions on Graphics (TOG), volume 30, page 69. ACM, 2011.cv::ximgproc::DisparityWLSFiltercv::ximgproc::FastBilateralSolverFilter
[14] Jonathan T Barron and Ben Poole. The fast bilateral solver. In European Conference on Computer Vision (ECCV), pages 617–632. Springer International Publishing, 2016.cv::ximgproc::FastGlobalSmootherFilter
[158] Dongbo Min, Sunghwan Choi, Jiangbo Lu, Bumsub Ham, Kwanghoon Sohn, and Minh N Do. Fast global image smoothing based on weighted least squares. Image Processing, IEEE Transactions on, 23(12):5638–5653, 2014.
[66] Zeev Farbman, Raanan Fattal, Dani Lischinski, and Richard Szeliski. Edge-preserving decompositions for multi-scale tone and detail manipulation. In ACM Transactions on Graphics (TOG), volume 27, page 67. ACM, 2008.cv::ximgproc::GuidedFilter
[99] Kaiming He, Jian Sun, and Xiaoou Tang. Guided image filtering. In Computer Vision–ECCV 2010, pages 1–14. Springer, 2010.cv::ximgproc::RidgeDetectionFilter
[64] Niki Estner. Best way of segmenting veins in leaves.
[151] Wolfram Mathematica. Ridge filter mathematica.*/#include <ros/ros.h>
#include <sensor_msgs/Image.h>
#include <cv_bridge/cv_bridge.h>
#include <image_transport/image_transport.h>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/ximgproc.hpp>
#include <message_filters/subscriber.h>
#include <message_filters/time_synchronizer.h>
#include <message_filters/sync_policies/approximate_time.h>#include <pcl/point_types.h>
#include <pcl_ros/point_cloud.h>
#include <pcl/common/common.h>
#include <pcl/common/pca.h>
#include <Eigen/Dense>class ImageProcessor
{
public:ImageProcessor(): it_(nh_), image_sub_(nh_, "/camera/color/image_raw", 1), depth_sub_(nh_, "/camera/aligned_depth_to_color/image_raw", 1){typedef message_filters::sync_policies::ApproximateTime<sensor_msgs::Image, sensor_msgs::Image> MySyncPolicy;// ApproximateTime或者ExactTimesync_.reset(new message_filters::Synchronizer<MySyncPolicy>(MySyncPolicy(10), image_sub_, depth_sub_));sync_->registerCallback(boost::bind(&ImageProcessor::callback, this, _1, _2));pub_ = it_.advertise("/fbs/depth_processed", 1);cloud_pub_ = nh_.advertise<pcl::PointCloud<pcl::PointXYZRGB>>("/fbs/output_cloud", 1);cloud2_pub_ = nh_.advertise<pcl::PointCloud<pcl::PointXYZRGB>>("/fbs/output_cloud2", 1);}void callback(const sensor_msgs::ImageConstPtr& color_msg, const sensor_msgs::ImageConstPtr& depth_msg){cv::Mat color_image, depth_image;try {color_image = cv_bridge::toCvShare(color_msg, "rgb8")->image;depth_image = cv_bridge::toCvShare(depth_msg, sensor_msgs::image_encodings::TYPE_16UC1)->image;cv::Mat confidence;generateConfidence(depth_image, confidence);// 将confidence图像标准化到0-255的范围cv::Mat normalized_confidence;confidence.convertTo(normalized_confidence, CV_8UC1, 255.0); // 将浮点值乘以255以进行转换// // 显示信心图// cv::namedWindow("Confidence", cv::WINDOW_AUTOSIZE); // 创建一个窗口// cv::imshow("Confidence", normalized_confidence);    // 显示信心图// cv::waitKey(1); // 等待1毫秒,让显示函数有机会更新窗口cv::Mat filtered_image;applyFastBilateralSolverFilter(depth_image, color_image, confidence, filtered_image);// applyFastGlobalSmootherFilter(depth_image, color_image, filtered_image);// applyGuidedFilter(depth_image, color_image, filtered_image);// applyRidgeDetectionFilter(depth_image, filtered_image);// 转换回ROS图像并发布sensor_msgs::ImagePtr msg_out = cv_bridge::CvImage(std_msgs::Header(), sensor_msgs::image_encodings::TYPE_16UC1, filtered_image).toImageMsg();pub_.publish(msg_out);// 发布点云pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZRGB>());depthToPointCloud(depth_image, color_image, *cloud);cloud->header.stamp = pcl_conversions::toPCL(color_msg->header.stamp); // Use the correct message for the timestampcloud_pub_.publish(cloud);// 发布点云// pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZRGB>());depthToPointCloud(filtered_image, color_image, *cloud);// cloud->header.stamp = pcl_conversions::toPCL(color_msg->header.stamp); // Use the correct message for the timestampcloud2_pub_.publish(cloud);} catch (const cv_bridge::Exception& e) {ROS_ERROR("cv_bridge exception: %s", e.what());return;}}void generateConfidence(const cv::Mat &depth, cv::Mat &confidence){confidence = cv::Mat::zeros(depth.size(), CV_32F);confidence.setTo(1, depth != 0);}void applyFastBilateralSolverFilter(const cv::Mat &depth, const cv::Mat &color, const cv::Mat &confidence, cv::Mat &output){auto filter = cv::ximgproc::createFastBilateralSolverFilter(color, 10, 1, 10);filter->filter(depth, confidence, output);}void applyFastGlobalSmootherFilter(const cv::Mat &depth, const cv::Mat &color, cv::Mat &output){// 将depth转换为16S格式cv::Mat depth16S;depth.convertTo(depth16S, CV_16S);// 创建Fast Global Smoother滤波器auto filter = cv::ximgproc::createFastGlobalSmootherFilter(color, 128, 10);// 使用16S格式的depth进行滤波filter->filter(depth16S, output);}void applyGuidedFilter(const cv::Mat &depth, const cv::Mat &color, cv::Mat &output){// 将depth图像转换为CV_32F类型cv::Mat depth32F;cv::Mat output32F;depth.convertTo(depth32F, CV_32F, 1.0 / 255.0);auto filter = cv::ximgproc::createGuidedFilter(color, 5, 0.01);filter->filter(depth32F, output32F);output32F.convertTo(output, CV_16U, 255.0);}void applyRidgeDetectionFilter(const cv::Mat &depth, cv::Mat &output){// 将图像转换为浮点类型,这是脊线检测滤波器的要求cv::Mat depth32F;depth.convertTo(depth32F, CV_32FC1, 1.0 / 255.0);// 创建脊线检测滤波器int ddepth = -1; // 使用相同的图像深度作为输入和输出double scale = 1; // Sobel算子的比例因子double delta = 0; // 添加到结果中的可选增量值int borderType = cv::BORDER_DEFAULT; // 边缘填充方式cv::Ptr<cv::ximgproc::RidgeDetectionFilter> ridgeFilter = cv::ximgproc::RidgeDetectionFilter::create();// 应用脊线检测滤波器cv::Mat output32F;ridgeFilter->getRidgeFilteredImage(depth32F, output32F);// 将结果转换回8位图像以便显示output32F.convertTo(output, CV_16UC1, 255.0);}void depthToPointCloud(const cv::Mat& depth_img, const cv::Mat& rgb_img, pcl::PointCloud<pcl::PointXYZRGB>& cloud) {// 假设 fx, fy, cx, cy 是相机内参float fx = 644.7825927734375; // 焦距xfloat fy = 644.0340576171875; // 焦距yfloat cx = 636.1322631835938; // 主点xfloat cy = 367.4698486328125; // 主点ycloud.width = depth_img.cols;cloud.height = depth_img.rows;cloud.is_dense = false;cloud.points.resize(cloud.width * cloud.height);cloud.header.frame_id = "map";for (int v = 0; v < depth_img.rows; v++) {for (int u = 0; u < depth_img.cols; u++) {pcl::PointXYZRGB& point = cloud.points[v * depth_img.cols + u];float depth = depth_img.at<ushort>(v, u) / 1000.0f; // 深度单位转换为米if (depth == 0) continue; // 无效点跳过// 从像素坐标转换到相机坐标系point.x = (u - cx) * depth / fx;point.y = (v - cy) * depth / fy;point.z = depth;// 设置颜色cv::Vec3b rgb = rgb_img.at<cv::Vec3b>(v, u);uint32_t rgb_val = ((uint32_t)rgb[0] << 16 | (uint32_t)rgb[1] << 8 | (uint32_t)rgb[2]);point.rgb = *reinterpret_cast<float*>(&rgb_val);}}}private:ros::NodeHandle nh_;image_transport::ImageTransport it_;image_transport::Publisher pub_;message_filters::Subscriber<sensor_msgs::Image> image_sub_, depth_sub_;boost::shared_ptr<message_filters::Synchronizer<message_filters::sync_policies::ApproximateTime<sensor_msgs::Image, sensor_msgs::Image>>> sync_;ros::Publisher cloud_pub_;ros::Publisher cloud2_pub_;
};int main(int argc, char** argv)
{ros::init(argc, argv, "image_processor");ImageProcessor ip;ros::spin();return 0;
}

这里FBS和其他的滤波器的本质区别是,FBS引入了信心图(confidence),这使得其在进行滤波时不会被无效像素所影响,因此我建议,当使用无信心图的滤波器时,尽量使用其他后处理方法填充空洞,减少无效像素;当使用FBS时,建议深度图宁缺毋滥,尽量保持真值。

2 效果展示

这是realsense D455的原始深度图与经过FBS的深度图是对比。

nullrawFBS
深度图请添加图片描述请添加图片描述
深度图请添加图片描述请添加图片描述
点云在这里插入图片描述在这里插入图片描述

看起来似乎不错,但我们将其转换为点云就会发现,原始深度图干干净净,不同深度处的物体分离性很好;但是经过优化的深度图产生了大量的离群点,这些是FBS“猜”错的点。
请添加图片描述
这个视角下我们看到,FBS确实补全了部分缺失的深度值,但也带来了很大问题,最主要的问题就是这些错误的点。

这篇关于使用cv::ximgproc::FastBilateralSolverFilter优化realsense D455的深度图的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/833416

相关文章

使用DeepSeek API 结合VSCode提升开发效率

《使用DeepSeekAPI结合VSCode提升开发效率》:本文主要介绍DeepSeekAPI与VisualStudioCode(VSCode)结合使用,以提升软件开发效率,具有一定的参考价值... 目录引言准备工作安装必要的 VSCode 扩展配置 DeepSeek API1. 创建 API 请求文件2.

使用TomCat,service输出台出现乱码的解决

《使用TomCat,service输出台出现乱码的解决》本文介绍了解决Tomcat服务输出台中文乱码问题的两种方法,第一种方法是修改`logging.properties`文件中的`prefix`和`... 目录使用TomCat,service输出台出现乱码问题1解决方案问题2解决方案总结使用TomCat,

解决IDEA使用springBoot创建项目,lombok标注实体类后编译无报错,但是运行时报错问题

《解决IDEA使用springBoot创建项目,lombok标注实体类后编译无报错,但是运行时报错问题》文章详细描述了在使用lombok的@Data注解标注实体类时遇到编译无误但运行时报错的问题,分析... 目录问题分析问题解决方案步骤一步骤二步骤三总结问题使用lombok注解@Data标注实体类,编译时

Java中使用Java Mail实现邮件服务功能示例

《Java中使用JavaMail实现邮件服务功能示例》:本文主要介绍Java中使用JavaMail实现邮件服务功能的相关资料,文章还提供了一个发送邮件的示例代码,包括创建参数类、邮件类和执行结... 目录前言一、历史背景二编程、pom依赖三、API说明(一)Session (会话)(二)Message编程客

C++中使用vector存储并遍历数据的基本步骤

《C++中使用vector存储并遍历数据的基本步骤》C++标准模板库(STL)提供了多种容器类型,包括顺序容器、关联容器、无序关联容器和容器适配器,每种容器都有其特定的用途和特性,:本文主要介绍C... 目录(1)容器及简要描述‌php顺序容器‌‌关联容器‌‌无序关联容器‌(基于哈希表):‌容器适配器‌:(

使用Python实现高效的端口扫描器

《使用Python实现高效的端口扫描器》在网络安全领域,端口扫描是一项基本而重要的技能,通过端口扫描,可以发现目标主机上开放的服务和端口,这对于安全评估、渗透测试等有着不可忽视的作用,本文将介绍如何使... 目录1. 端口扫描的基本原理2. 使用python实现端口扫描2.1 安装必要的库2.2 编写端口扫

使用Python实现操作mongodb详解

《使用Python实现操作mongodb详解》这篇文章主要为大家详细介绍了使用Python实现操作mongodb的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、示例二、常用指令三、遇到的问题一、示例from pymongo import MongoClientf

SQL Server使用SELECT INTO实现表备份的代码示例

《SQLServer使用SELECTINTO实现表备份的代码示例》在数据库管理过程中,有时我们需要对表进行备份,以防数据丢失或修改错误,在SQLServer中,可以使用SELECTINT... 在数据库管理过程中,有时我们需要对表进行备份,以防数据丢失或修改错误。在 SQL Server 中,可以使用 SE

使用Python合并 Excel单元格指定行列或单元格范围

《使用Python合并Excel单元格指定行列或单元格范围》合并Excel单元格是Excel数据处理和表格设计中的一项常用操作,本文将介绍如何通过Python合并Excel中的指定行列或单... 目录python Excel库安装Python合并Excel 中的指定行Python合并Excel 中的指定列P

浅析Rust多线程中如何安全的使用变量

《浅析Rust多线程中如何安全的使用变量》这篇文章主要为大家详细介绍了Rust如何在线程的闭包中安全的使用变量,包括共享变量和修改变量,文中的示例代码讲解详细,有需要的小伙伴可以参考下... 目录1. 向线程传递变量2. 多线程共享变量引用3. 多线程中修改变量4. 总结在Rust语言中,一个既引人入胜又可