等号右边第一项表示像素点 p 的初始匹配代价;第二项表示 p 的前一个像素点 p − r 的最小匹配代价:若和 p 的视差差值为 0,无需加任何惩罚因子,差值为 1,加惩罚因子 P1 ,若差值大于 1,则惩罚因子为 P2;第三项表示前一个像素点 p − r 沿 r 路径上的最小匹配代价,加入该项的目的是抑制 L_{r}( p, d ) 的数值过大,并不会影响视差空间。
defget_path_cost(slice, offset): """ part of the aggregation step, finds the minimum costs in a D x M slice (where M = the number of pixels in the given direction) :param slice: M x D array from the cost volume. :param offset: ignore the pixels on the border. :return: M x D array of the minimum costs for a given slice in a given direction. """ P1 = 5 P2 = 70
defselect_disparity(aggregation_volume): """ last step of the sgm algorithm, corresponding to equation 14 followed by winner-takes-all approach. :param aggregation_volume: H x W x D x N array of matching cost for all defined directions. :return: disparity image. """ volume = np.sum(aggregation_volume, axis=3) disparity_map = np.argmin(volume, axis=2) return disparity_map