In [10]:
from PIL import Image
from numpy import *
from pylab import *
import scipy.misc
from scipy import ndimage
import numpy as np

In [11]:
import stereo
stereo = reload(stereo)

In [12]:
im_l = array(Image.open('scene1.row3.col3.ppm').convert('L'), 'f')
im_r = array(Image.open('scene1.row3.col4.ppm').convert('L'), 'f')

In [67]:
def plane_sweep_sad(im_l, im_r, start, steps, wid):
    """ Find disparity image using SAD (sum of absolute difference). """

    m, n = im_l.shape

    # arrays to hold the different sums
    mean_l = np.zeros((m, n))
    mean_r = np.zeros((m, n))
    s = np.zeros((m, n))
    s_l = np.zeros((m, n))
    s_r = np.zeros((m, n))

    # array to hold depth planes
    dmaps = np.zeros((m, n, steps))

    # compute mean of patch
    ndimage.filters.uniform_filter(im_l, wid, mean_l)
    ndimage.filters.uniform_filter(im_r, wid, mean_r)

    # normalized images
    #norm_l = im_l - mean_l
    #norm_r = im_r - mean_r
    norm_l = im_l
    norm_r = im_r

    # try different disparities
    for displ in range(steps):
        # move left image to the right, compute sums
        # sum of nominator
        ndimage.filters.uniform_filter(np.absolute(np.roll(norm_l, -displ-start)-norm_r), wid, s)
        # sum of denominator
        ndimage.filters.uniform_filter(
            np.roll(norm_l, -displ-start)*np.roll(norm_l, -displ-start), wid, s_l)
        # sum of denominator
        ndimage.filters.uniform_filter(norm_r*norm_r, wid, s_r)

        # store ncc scores
        dmaps[:, :, displ] = s/np.sqrt(s_l*s_r)

    # pick best depth for each pixel
    return np.argmin(dmaps, axis=2)

In [68]:
def plane_sweep_sad_gauss(im_l, im_r, start, steps, wid):
    """ Find disparity image using SAD
    with Gaussian weighted neighborhoods. """

    m, n = im_l.shape

    # arrays to hold the different sums
    mean_l = np.zeros((m, n))
    mean_r = np.zeros((m, n))
    s = np.zeros((m, n))
    s_l = np.zeros((m, n))
    s_r = np.zeros((m, n))

    # array to hold depth planes
    dmaps = np.zeros((m, n, steps))

    # compute mean of patch
    ndimage.filters.gaussian_filter(im_l, wid, 0, mean_l)
    ndimage.filters.gaussian_filter(im_r, wid, 0, mean_r)

    # normalized images
    #norm_l = im_l - mean_l
    #norm_r = im_r - mean_r
    norm_l = im_l
    norm_r = im_r

    # try different disparities
    for displ in range(steps):
        # move left image to the right, compute sums
        # sum of nominator
        ndimage.filters.gaussian_filter(np.absolute(np.roll(norm_l, -displ-start)-norm_r), wid, 0, s)
        # sum of denominator
        ndimage.filters.gaussian_filter(
            np.roll(norm_l, -displ-start)*np.roll(norm_l, -displ-start), wid, 0, s_l)
        # sum of denominator
        ndimage.filters.gaussian_filter(norm_r*norm_r, wid, 0, s_r)

        # store ncc scores
        dmaps[:, :, displ] = s/np.sqrt(s_l*s_r)

    # pick best depth for each pixel
    return np.argmin(dmaps, axis=2)

In [69]:
start = 4
steps = 12
wid = 9
res = stereo.plane_sweep_ncc(im_l, im_r, start, steps, wid)

In [70]:
start = 4
steps = 12
wid = 3
res2 = stereo.plane_sweep_gauss(im_l, im_r, start, steps, wid)

In [71]:
start = 4
steps = 12
wid = 9
res3 = plane_sweep_sad(im_l, im_r, start, steps, wid)

In [72]:
start = 4
steps = 12
wid = 3
res4 = plane_sweep_sad_gauss(im_l, im_r, start, steps, wid)

In [73]:
scipy.misc.imsave('depth.png', res)

In [74]:
figure(figsize=(16, 16))
gray()
subplot(3, 2, 1)
imshow(Image.open('scene1.row3.col3.ppm'))
axis('off')
subplot(3, 2, 2)
imshow(Image.open('scene1.row3.col4.ppm'))
axis('off')
subplot(3, 2, 3)
imshow(res)
axis('off')
subplot(3, 2, 4)
imshow(res2)
axis('off')
subplot(3, 2, 5)
imshow(res3)
axis('off')
subplot(3, 2, 6)
imshow(res4)
axis('off')
show()



In [ ]: