Good day!

I am trying to learn how to manually implement stereo matching algorithms. I'm basically starting with the most basic of them all - Absolute Difference.

I found some slides online that describe how to do it. Basically, from what I understand, I should calculate the difference between the pixels in my left image and the same pixel in the right image "shifted" by a certain distance/disparity. Then among these disparities, I select the minimum, which makes sense to me since the pixel with the lowest disparity means that it is most likely the same pixel in the left image.

I've prototyped that in MATLAB. Here is the code:

```
im_left = imread('tsu_left.png');
im_right = imread('tsu_right.png');
height = size(im_left, 1);
width = size(im_left, 2);
disparity_max = 16;
ad_costs = zeros(height, width,disparity_max);
for disparity = 1:disparity_max
for row = 1:height
for col = 1:width
%Left to right matching
col_disp = col - disparity;
if col_disp < 1
ad_costs(row, col, disparity) = 0;
else
%Average RGB
left_pixel = (im_left(row, col, 1) + im_left(row, col, 2) + im_left(row, col, 3))/3;
right_pixel = (im_right(row, col_disp, 1) + im_right(row, col_disp, 2) + im_right(row, col_disp, 3))/3;
%Subtract averages
ad_costs(row, col, disparity) = abs(left_pixel - right_pixel);
end
end
end
end
min_costs = zeros(height, width);
for disparity = 1:disparity_max
for row = 1:height
for col = 1:width
%The minimum disparity is chosen
min_costs(row, col) = min(ad_costs(row, col, :));
end
end
end
```

Take note that I haven't implemented the variant where the differences in a certain window is summed, leading to Sum of Absolute Differences. I am only taking the difference per pixel, per disparity. The lecture slides I found online says that it should look like this (rightmost image):

https://dl.dropboxusercontent.com/u/92715312/lec.PNG

However, the result from the code above (using imshow(min_costs)) yield something like this:

https://dl.dropboxusercontent.com/u/92715312/res.PNG

I can't figure out why the outputs are so different. Is there some trivial step that I am missing, or is my understanding of how the algorithm works wrong? I am also using the tsukuba image.