1Disparity map post-filtering {#tutorial_ximgproc_disparity_filtering} 2============================ 3 4Introduction 5------------ 6 7Stereo matching algorithms, especially highly-optimized ones that are intended for real-time processing 8on CPU, tend to make quite a few errors on challenging sequences. These errors are usually concentrated 9in uniform texture-less areas, half-occlusions and regions near depth discontinuities. One way of dealing 10with stereo-matching errors is to use various techniques of detecting potentially inaccurate disparity 11values and invalidate them, therefore making the disparity map semi-sparse. Several such techniques are 12already implemented in the StereoBM and StereoSGBM algorithms. Another way would be to use some kind of 13filtering procedure to align the disparity map edges with those of the source image and to propagate 14the disparity values from high- to low-confidence regions like half-occlusions. Recent advances in 15edge-aware filtering have enabled performing such post-filtering under the constraints of real-time 16processing on CPU. 17 18In this tutorial you will learn how to use the disparity map post-filtering to improve the results 19of StereoBM and StereoSGBM algorithms. 20 21Source Stereoscopic Image 22------------------------- 23 24![Left view](images/ambush_5_left.jpg) 25![Right view](images/ambush_5_right.jpg) 26 27Source Code 28----------- 29 30We will be using snippets from the example application, that can be downloaded [here ](https://github.com/opencv/opencv_contrib/blob/master/modules/ximgproc/samples/disparity_filtering.cpp). 31 32Explanation 33----------- 34 35The provided example has several options that yield different trade-offs between the speed and 36the quality of the resulting disparity map. Both the speed and the quality are measured if the user 37has provided the ground-truth disparity map. In this tutorial we will take a detailed look at the 38default pipeline, that was designed to provide the best possible quality under the constraints of 39real-time processing on CPU. 40 41-# **Load left and right views** 42 @snippet ximgproc/samples/disparity_filtering.cpp load_views 43 We start by loading the source stereopair. For this tutorial we will take a somewhat challenging 44 example from the MPI-Sintel dataset with a lot of texture-less regions. 45 46-# **Prepare the views for matching** 47 @snippet ximgproc/samples/disparity_filtering.cpp downscale 48 We perform downscaling of the views to speed-up the matching stage at the cost of minor 49 quality degradation. To get the best possible quality downscaling should be avoided. 50 51-# **Perform matching and create the filter instance** 52 @snippet ximgproc/samples/disparity_filtering.cpp matching 53 We are using StereoBM for faster processing. If speed is not critical, though, 54 StereoSGBM would provide better quality. The filter instance is created by providing 55 the StereoMatcher instance that we intend to use. Another matcher instance is 56 returned by the createRightMatcher function. These two matcher instances are then 57 used to compute disparity maps both for the left and right views, that are required 58 by the filter. 59 60-# **Perform filtering** 61 @snippet ximgproc/samples/disparity_filtering.cpp filtering 62 Disparity maps computed by the respective matcher instances, as well as the source left view 63 are passed to the filter. Note that we are using the original non-downscaled view to guide the 64 filtering process. The disparity map is automatically upscaled in an edge-aware fashion to match 65 the original view resolution. The result is stored in filtered_disp. 66 67-# **Visualize the disparity maps** 68 @snippet ximgproc/samples/disparity_filtering.cpp visualization 69 We use a convenience function getDisparityVis to visualize the disparity maps. The second parameter 70 defines the contrast (all disparity values are scaled by this value in the visualization). 71 72Results 73------- 74 75![Result of the StereoBM](images/ambush_5_bm.png) 76![Result of the demonstrated pipeline (StereoBM on downscaled views with post-filtering)](images/ambush_5_bm_with_filter.png) 77