[3D] Depth map from 2 images

With this code written in matlab, you can get a depth map from 2 images or pictures. These two images should be the equivalent of the left eye and the right eye. This is DENSE mapping, SPARSE mapping is also possible, but you should use the VLSIFT matlab toolbox for that, for instance.

You can imput two images, it will procude a depth 3d map from the best match. This is 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.

Example image (right eye)

Result (with viables set differently) The darker it is, the further away. Also, but background is not great, that is because the parallax in this case is focussed on the human standing there.

  1. clc
  2. clear all
  3. close all
  4. %Initialize images
  5. im_r=imread('Left_Checkerboard_rectified.bmp');
  6. im_l=imread('Right_Checkerboard_rectified.bmp');</code></p>
  7. %Initialize variables
  8. w=10;
  9. dispmax=55;
  11. %Resize images
  12. im_l_res=zeros(size(im_l,1)+2*w,size(im_l,2)+2*w+dispmax);
  13. im_r_res=zeros(size(im_r,1)+2*w,size(im_r,2)+2*w+dispmax);
  15. im_l_res(w:end-1-w,w:end-1-w-dispmax)=im_l;
  16. im_r_res(w:end-1-w,w:end-1-w-dispmax)=im_r;
  18. im_l_res=uint8(im_l_res);
  19. im_r_res=uint8(im_r_res);
  21. %Initialize SAD
  22. %SAD=zeros(size(im_l_res,1),size(im_l_res,2),dispmax+1);
  24. for y=w+1:1:size(im_l_res,1)-w %For each epipolar line (row)
  25. y %to see where it is
  26. for x=w+1:1:size(im_l_res,2)-w-dispmax  %For each pixel on that row
  27. left=im_l_res(y-w:y+w,x-w:x+w);
  29. for disp=0:1:dispmax
  31. right=im_r_res(y-w:y+w,x-w+disp:x+w+disp);
  33. %Take the sum of absolute difference
  35. SAD(y-5,x-5,disp+1)=sum(abs(left(:)-right(:)));
  36. end
  38. end
  40. end
  42. [SAD_min,SAD_min_loc]=min(SAD,[],3);
  43. imagesc(SAD_min_loc)

Tim Zaman

MSc Biorobotics. Specialization in computer vision and deep learning. Works at NVIDIA.

You may also like...

4 Responses

  1. singh says:

    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=263×395 double
    im_l=243x320x3 double

    • Kropek1987 says:

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

  2. suvarna says:

    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?

  3. Alessandro says:

    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!