You need to enable JavaScript to run this app. OpenCV Mat上使用RealSense rs2::spatial_filter深度滤波无效果排查
优惠活动
大模型
产品
解决方案
定价
更多

OpenCV Mat上使用RealSense rs2::spatial_filter深度滤波无效果排查

rs2::spatial_filter 无效果问题排查与解决

核心根因

  • RealSense官方提供的所有深度后处理滤波器(含空间滤波、时域滤波、空洞填充),内部实现仅适配RS2_FORMAT_Z16格式的16位无符号整数深度帧。当输入帧格式为RS2_FORMAT_DISTANCE(32位浮点)时,滤波器会直接执行内置格式校验短路逻辑,不执行任何像素计算,直接将输入帧句柄透传为输出,因此不会产生任何数据修改。
  • 你为了适配本地存储的浮点深度图,将虚拟流的格式从原生支持的Z16改为了DISTANCE,相当于绕过了RealSense内部的深度单位与格式映射,直接触发了滤波器的无效果透传分支。

具体解决步骤

1. 修正虚拟深度流的格式配置

将rsImageConverter构造函数中的流定义改回RealSense滤波器支持的Z16格式,同时匹配对应的深度单位参数:

rsImageConverter::rsImageConverter(int w, int h, int bppDepth) :
        w(w),
        h(h),
        bppDepth(bppDepth),
        depth_sensor(dev.add_sensor("Depth"))
{
    rs2_intrinsics depth_intrinsics{ w, h, (float)(w / 2), (float)(h / 2), 
                                     (float)w, (float)h, RS2_DISTORTION_BROWN_CONRADY,
                                     {0,0,0,0,0} };
    // 流格式改为RS2_FORMAT_Z16,每像素字节数为2(对应uint16_t长度)
    depth_stream = depth_sensor.add_video_stream({  RS2_STREAM_DEPTH, 0, 0,
                                                    w, h, 60, bppDepth,
                                                    RS2_FORMAT_Z16, depth_intrinsics });
    // 匹配Z16格式的深度单位:1个整数深度值对应0.001米(1毫米)
    depth_sensor.add_read_only_option(RS2_OPTION_DEPTH_UNITS, 0.001f);

    depth_sensor.open(depth_stream);
    depth_sensor.start(syncer);
}

2. 调整数据转换流程

将OpenCV中存储的单位为米的CV_32F深度图,提前转换为Z16格式的整数深度图,再传入软件传感器生成合法的rs2深度帧:

cv::Mat rawDepthImg = load_raw_depth("/path/to/depth/image");

// 米级浮点深度转毫米级16位整数深度
cv::Mat depthZ16;
rawDepthImg.convertTo(depthZ16, CV_16U, 1000.0);

// 传入的每像素字节数为uint16的长度2
rsImageConverter ic{rawDepthImg.cols, rawDepthImg.rows, sizeof(uint16_t)};
if ( !ic.convertFrame(reinterpret_cast<uint8_t*>(depthZ16.data), nullptr) )
{
    fprintf(stderr, "Could not load depth.\n");
    exit(1);
}
rs2::frame rsDepthFrame = ic.getDepth();

3. 滤波结果转换回OpenCV格式

空间滤波的输出仍为Z16格式的深度帧,需要转回浮点格式后再和原始深度图做对比,才能看到滤波带来的数值变化:

rs2::spatial_filter spat_filter;
spat_filter.set_option(RS2_OPTION_FILTER_MAGNITUDE, 2.0f);
spat_filter.set_option(RS2_OPTION_FILTER_SMOOTH_ALPHA, 0.5f);
spat_filter.set_option(RS2_OPTION_FILTER_SMOOTH_DELTA, 20.0f);

rs2::frame rsFilteredDepthFrame = spat_filter.process(rsDepthFrame);

// 拷贝滤波后的Z16深度数据
cv::Mat filteredDepthZ16(rawDepthImg.size(), CV_16U);
memcpy(filteredDepthZ16.data, rsFilteredDepthFrame.get_data(),
       rawDepthImg.cols * rawDepthImg.rows * sizeof(uint16_t));

// 毫米级整数深度转回米级浮点深度
cv::Mat filteredDepth;
filteredDepthZ16.convertTo(filteredDepth, CV_32F, 0.001);

// 此时显示与差图即可观察到滤波效果
cv::imshow("Original depth", rawDepthImg);
cv::imshow("Filtered depth", filteredDepth);
cv::imshow("Diff", filteredDepth - rawDepthImg);
cv::waitKey(0);

注意事项

  • 你当前设置的空间滤波参数均为Z16单位(毫米)下的合理取值,不需要调整,只要输入帧格式符合要求即可正常生效。
  • RealSense滤波器的内部计算逻辑完全基于Z16格式设计,强行修改流格式为浮点类型不会获得任何处理加速,只会触发内置的格式校验透传逻辑。

内容的提问来源于stack exchange,提问作者Stan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.26 09:57:03