Sum of Absolute Differences 即“绝对差之和”简称SAD。该算法通过比较立体图像对(一般为左右图)中的光照亮度差异来寻找匹配点对,进而计算视差。
代码如下(示例):
import os
import time
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
代码如下(示例):
window_size=5
Dmax=64
本文使用的图片来源于Middlebury数据集
代码如下:
#Read rectified stereo image pairs
os.chdir(r'此处为图片路径')
LeftRawImage = cv.imread("左图文件名")
RightRawImage = cv.imread("右图文件名")
#Convert to gray image
LeftRawImage = cv.cvtColor(LeftRawImage,cv.COLOR_BGR2GRAY)
RightRawImage = cv.cvtColor(RightRawImage,cv.COLOR_BGR2GRAY)
左图:
右图:
代码如下,此代码对图片做了padding,保证输出视差图和原图具有相同分辨率:
class SAD():
def __init__(self, Lraw, Rraw, window_size, Dmax):
self.Lraw = Lraw
self.Rraw = Rraw
self.window_size = window_size
self.Dmax = Dmax
self.ROWS = Lraw.shape[0]
self.COLS = Lraw.shape[1]
self.PADDING_SIZE = int((window_size-1)/2)
#Calculate Absolute Differences
def AD(self):
AD_PADDING=np.zeros((self.ROWS,self.COLS,self.Dmax))
AD_PADDING=np.pad(AD_PADDING,(self.PADDING_SIZE,self.PADDING_SIZE))
Cost_SAD=np.zeros((self.ROWS,self.COLS,Dmax))
for d in range(0,self.Dmax):
for i in range(0,self.ROWS):
for j in range(0,self.COLS):
if(j - d >= 0):
AD_PADDING[i+self.PADDING_SIZE,j+self.PADDING_SIZE,d]=np.abs(int(self.Lraw[i,j])-int(self.Rraw[i,j-d]))
Cost_SAD[i,j,d] = np.sum(AD_PADDING[i:i+window_size,j:j+window_size,d])
else:
Cost_SAD[i,j,d]=0
return Cost_SAD
#Calculate the minimum index
def argmin(self,Cost_volume):
Disparity = np.zeros((self.ROWS,self.COLS),dtype=np.int8)
for i in range(0,self.ROWS):
for j in range(0,self.COLS):
Cost_sad = Cost_volume[i,j,:]
index_sort= np.argsort(Cost_sad)
Disparity[i,j]=index_sort[0]
return Disparity
代码如下:
#Calculate disparity
time_start = time.time()
SAD_Middleburry = SAD(LeftRawImage,RightRawImage,window_size, Dmax)
Cost_sad = SAD_Middleburry.AD()
Disparity_image = SAD_Middleburry.argmin(Cost_sad)
print("用时:",time.time() - time_start,"s")
plt.imshow(Disparity_image,cmap='plasma')
plt.show()
plt.imsave('./Disparity.png', Disparity_image, cmap='plasma')
SAD算法虽然简单高效,但是容易受光照、曝光等噪声影响,视差图的效果往往不是特别鲁棒。
注:以上代码运行较慢,在作者的电脑上运行时间为325s左右,运行后请耐心等待。以后作者将不定时更新立体匹配算法的python代码,包括Census,AD-Census等,同时作者将学习相关加速方法,并对所写代码做出改进,以便高效运行。
作者并不精通python编程,如有错误,欢迎各位大佬不吝赐教!如有疑问,欢迎大家一起讨论!谢谢!