我该如何改进这个视差图?

时间:2015-08-09 22:12:50

标签: python image-processing disparity-mapping

我一直在研究一段代码来创建视差图。

我不想使用OpenCV来加载/保存将它们转换为灰度的图像。

到目前为止,我已设法实现了in this website解释的算法。我正在使用使用绝对差值和(SAD)的算法版本。为了测试我的实现,我正在使用this dataset中的立体图像。

这是我的代码:

import cv2
import numpy as np

# Load the stereo images
img = cv2.imread('bow-view1.png')
img2 = cv2.imread('bow-view5.png')
# convert stereo images to grayscale
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
gray2 = cv2.cvtColor(img2,cv2.COLOR_BGR2GRAY)
# get the size of the images
# l -> lines
# c -> columns
# v -> channel (RGB)
l,c,v = img.shape

# initialize arrays
minSAD = np.ones((l,c)) * 1000
sad = np.ones((l,c))
winsad = np.ones((l,c))
disp = np.zeros((l,c))

max_shift = 30

# set size of the SAD window
w_l = 2
w_c = 2

for shift in range(max_shift):
    print("New Shift: %d"%(shift))
    for u in range(0,l):
        for v in range(0,c):
            # calculate SAD
            if(u+shift < l):
                sad[u,v] = np.abs((int(gray[u,v]) - int(gray2[u+shift,v])))
            sum_sad = 0
            for d in range(w_l):
                for e in range(w_c):
                    if(u+d < l and v+e < c):
                        sum_sad +=  sad[u+d,v+e]

            winsad[u,v] = sum_sad
            # Save disparity
            if(sad[u,v] < minSAD[u,v]):
                minSAD[u,v] = winsad[u,v]
                disp[u,v] = shift

print("Process Complete")
# write disparity map to image  
cv2.imwrite('outputHT/disparity/sad.png',disp)
print("Disparity Map Generated")

这是该代码生成的输出: The disparity map generated by the code.

我应该得到一个类似(或非常接近)的输出: enter image description here

我尝试了几种窗口尺寸(在SAD步骤中),但我不断得到像这样的结果或者全黑的图像。

任何可以帮助我找出问题的答案,或者至少让我指出正确方向的答案都将非常感激!

1 个答案:

答案 0 :(得分:0)

你在这里遗漏的一件事是 * [new tag] webview-m40_r1 -> webview-m40_r1 * [new tag] webview-m40_r2 -> webview-m40_r2 * [new tag] webview-m40_r3 -> webview-m40_r3 * [new tag] webview-m40_r4 -> webview-m40_r4 Your identity is: Nuwan <nuwanst722@gmail.com> If you want to change this, please re-run 'repo init' with --config-name repo has been initialized in /home/nuwan2/Desktop/workspace/original_sources nuwan2@nuwan:~/Desktop/workspace/original_sources$ nuwan2@nuwan:~/Desktop/workspace/original_sources$ ls nuwan2@nuwan:~/Desktop/workspace/original_sources$ 数组中的所有值都在0到30之间,对应于黑色像素,所以为了将这些值映射到0到25​​5之间,你必须乘以转移8点。

相关问题