# [3D] Depth map from 2 images

dense mapping, that means that it could take up to half an hour to process! Also, it heavily depends on how you tune some of the variables.

`clc`

clear all

close all

`%Initialize images`

im_r=imread('Left_Checkerboard_rectified.bmp');

im_l=imread('Right_Checkerboard_rectified.bmp');</code></p>

`%Initialize variables`

`w=10;`

`dispmax=55;`

`%Resize images`

im_l_res=zeros(size(im_l,1)+2*w,size(im_l,2)+2*w+dispmax);

im_r_res=zeros(size(im_r,1)+2*w,size(im_r,2)+2*w+dispmax);

im_l_res(w:end-1-w,w:end-1-w-dispmax)=im_l;

im_r_res(w:end-1-w,w:end-1-w-dispmax)=im_r;

im_l_res=uint8(im_l_res);

im_r_res=uint8(im_r_res);

`%Initialize SAD`

`%SAD=zeros(size(im_l_res,1),size(im_l_res,2),dispmax+1);`

for y=w+1:1:size(im_l_res,1)-w %For each epipolar line (row)

`y %to see where it is`

for x=w+1:1:size(im_l_res,2)-w-dispmax %For each pixel on that row

left=im_l_res(y-w:y+w,x-w:x+w);

for disp=0:1:dispmax

right=im_r_res(y-w:y+w,x-w+disp:x+w+disp);

`%Take the sum of absolute difference`

SAD(y-5,x-5,disp+1)=sum(abs(left(:)-right(:)));

`end`

`end`

`end`

[SAD_min,SAD_min_loc]=min(SAD,[],3);

imagesc(SAD_min_loc)

Hi, can I use the depth map generated by your code to obtain the stereo correspondence from a given point in the right image to the left one? and if, how? and with which level of accurancy (numerically) ?

thanks a lot!

hi..

i want to construct 3d from 2d face images…if i use u r code above am not getting 3d image..can u help me in this?

while running this code i am getting the error like this

Assignment has more non-singleton rhs dimensions than non-singleton subscripts

Error in dense (line 23)

im_l_res(w:end-1-w,w:end-1-w-dispmax)=im_l;

im_l_res=263×395 double

im_l=243x320x3 double

Just use 8-bit gray-scale bitmaps as input or add rgb2gray(rgb_img) to convert images