2

I have been working on a piece of code to create a disparity map.

I don't want to use OpenCV for more than loading / saving the images converting them to grayscale.

So far, I've managed to implement the algorithm explained in this website. I'm using the version of the algorithm that uses the Sum of Absolute Differences (SAD). To test my implementation, I'm using the stereo images from this dataset.

Here's my code:

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")

This is the output generated by that code: The disparity map generated by the code.

I should get an output similar (or very close to) this: enter image description here

I've tried several window sizes (in the SAD step), but I keep getting results like this one or images that are all black.

Any answer that helps me figure out the problem or that at least points me in the right direction will be very appreciated!

m-oliv
  • 419
  • 11
  • 27
  • Seems to me, acording to that webpage, that all you have to do is [*equalize the histogram*](http://docs.opencv.org/doc/tutorials/imgproc/histograms/histogram_equalization/histogram_equalization.html) of your resulting disparity map. – Imanol Luengo Aug 10 '15 at 09:35

1 Answers1

0

One thing you are missing here is that all the values in the disp array will be between 0 and 30 which correspond to black pixel, so in order to map these values between 0 and 255 you have to multiply the shift by 8.

Gilles Gouaillardet
  • 8,193
  • 11
  • 24
  • 30
Ayush Goel
  • 21
  • 2