filterSpeckles函数的应用

本文介绍了一种用于过滤视差图中小斑点噪声的方法,该方法通过设置最大斑点尺寸和最大差异值来确定哪些区域被视为噪声。适用于16位的视差图像,能够有效去除小范围的噪声,保持较大连续区域的完整性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Filters off small noise blobs (speckles) in the disparity map
//用于过滤不同块的小斑点,可以在深度图中应用
C++: void filterSpeckles(InputOutputArray img, double newVal, int maxSpeckleSize, double maxDiff, InputOutputArray buf=noArray() )
Python: cv2.filterSpeckles(img, newVal, maxSpeckleSize, maxDiff[, buf]) → None
Parameters:

**img** – The input 16-bit signed disparity image
//输入的16位的视差图或者深度图


**maxSpeckleSize** – The maximum speckle size to consider it a speckle. Larger blobs are not affected by the algorithm
//斑点块的大小

**maxDiff** – Maximum difference between neighbor disparity pixels to put them into the same blob. Note that since StereoBM, StereoSGBM and may be other algorithms return a fixed-point disparity map, where disparity values are multiplied by 16, this scale factor should be taken into account when specifying this parameter value.
//

**buf** – The optional temporary buffer to avoid memory allocation within the function.
//

应用:见大疆guidance 视觉追踪demo

https://github.com/dji-sdk/Guidance-SDK/blob/master/doc/Guides/Visual_Tracking_tutorial/visual_Tracking_tutorial_cn.md

### 使用OpenCV进行视差图填充的方法 在处理双目视觉中的视差图时,由于遮挡或其他原因可能会出现无效区域。为了提高后续应用的质量,通常需要对这些无效区域进行填充。OpenCV提供了多种工具来实现这一目标。 #### 1. Speckle Filter用于去除孤立噪声并填充小孔洞 对于较小面积的无效区,可以采用`cv::filterSpeckles()`函数来进行过滤和初步填充。该函数通过设定最大斑点尺寸(`maxSpeckleSize`)以及相邻像素间允许的最大差异(`maxDiff`)参数,有效地移除那些不符合条件的小范围异常值,并将其替换为指定的新数值(newVal)[^3]。 ```cpp // C++ code example using filterSpeckles function from OpenCV #include <opencv2/opencv.hpp> int main(){ cv::Mat disparity; // Assume this is your input disparity map with some invalid regions marked as zero or NaN. // Parameters for speckle filtering double newVal = 0; int maxSpeckleSize = 50; double maxDiff = 2; // Apply the speckle filter to remove small isolated noise and fill tiny holes cv::filterSpeckles(disparity, newVal, maxSpeckleSize, maxDiff); return 0; } ``` #### 2. 中值滤波器(Median Filtering)平滑边缘过渡 当面对较大规模但形状较为规则的缺失部分时,可考虑运用中值滤波技术。这种方法能够较好地保持边界特征的同时减少随机噪点的影响。具体操作是在遍历整个图像的过程中,针对每个含有有效数据的位置周围选取一定大小窗口内的中间值作为当前坐标的最终输出结果[^4]。 ```cpp // Example of applying median blur on a valid region only within the disparity image void applyMedianBlurOnValidRegion(cv::Mat &disparity){ const int kernel_size = 7; // Kernel size should be odd number // Create mask indicating where we have good disparities (non-zero values) cv::Mat mask = disparity != 0; // Perform median blurring while respecting validity mask cv::medianBlur(disparity, disparity, kernel_size); // Restore original value at positions that were originally invalid after processing disparity.setTo(0, ~mask); } ``` #### 3. 自定义插值策略(Inpainting Techniques) 如果上述简单方式无法满足需求,则可以根据实际情况设计更复杂的修复方案。例如基于邻域平均、最近邻复制或是更为高级的能量最小化模型等手段完成大面积空缺填补工作。OpenCV内置有专门为此目的服务的功能——inpaint(),它支持不同的算法选项如TELEA或NS方法。 ```cpp // Using inpaint method provided by OpenCV library void performInpainting(cv::Mat &disparity){ // Prepare an appropriate mask highlighting all areas requiring repair cv::Mat mask = disparity == 0 || std::isnan(disparity.at<float>(i,j)); // Choose between INPAINT_TELEA / INPAINT_NS flags based on preference int flag = cv::INPAINT_TELEA; // Execute inpainting process over entire image according to given parameters cv::inpaint(disparity, mask, disparity, 3, flag); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值