[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.

[caption id="attachment_183" align="aligncenter" width="306" caption="Example image (right eye)"][/caption] [caption id="attachment_182" align="aligncenter" width="372" caption="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."][/caption]
  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;
  10. %Resize images
  11. im_l_res=zeros(size(im_l,1)+2*w,size(im_l,2)+2*w+dispmax);
  12. im_r_res=zeros(size(im_r,1)+2*w,size(im_r,2)+2*w+dispmax);
  13. im_l_res(w:end-1-w,w:end-1-w-dispmax)=im_l;
  14. im_r_res(w:end-1-w,w:end-1-w-dispmax)=im_r;
  15. im_l_res=uint8(im_l_res);
  16. im_r_res=uint8(im_r_res);
  17. %Initialize SAD
  18. %SAD=zeros(size(im_l_res,1),size(im_l_res,2),dispmax+1);
  19. for y=w+1:1:size(im_l_res,1)-w %For each epipolar line (row)
  20. y %to see where it is
  21. for x=w+1:1:size(im_l_res,2)-w-dispmax  %For each pixel on that row
  22. left=im_l_res(y-w:y+w,x-w:x+w);
  23. for disp=0:1:dispmax
  24. right=im_r_res(y-w:y+w,x-w+disp:x+w+disp);
  25. %Take the sum of absolute difference
  26. SAD(y-5,x-5,disp+1)=sum(abs(left(:)-right(:)));
  27. end
  28. end
  29. end
  30. [SAD_min,SAD_min_loc]=min(SAD,[],3);
  31. imagesc(SAD_min_loc)

You may also like...

4 Responses

  1. 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!

  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. 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