close all
clear all
% fn1 = '0966_L.JPG';
% fn2 = '0966_R.JPG';


%1065, 1060, 1057
 I1 =rgb2gray( imread('1057L.JPG'));
 I2 =rgb2gray( imread('1057R.JPG'));
 
 I1 = imresize(I1, [600 800]);
 I2 = imresize(I2, [600 800]);

%   I1 = imread(fn1);
%   I2 = imread(fn2);

% depthIm=rgb2gray(zeros(size(I1)));


baseline=30;
focal=0.4;
%  I1 = rgb2gray(I1);
%  I2 = rgb2gray(I2);
%   I1=I1(340:2550,600:2140);
%   I2=I2(340:2550,600:2140);
% I1=I1(:,:,1);
% I2=I2(:,:,3);
%  f1 = detectHarrisFeatures(I1,'MinQuality',0.01);
% f2 = detectHarrisFeatures(I2,'MinQuality',0.01);
 f1=detectSURFFeatures(I1,'MetricThreshold',5000);
 f2=detectSURFFeatures(I2,'MetricThreshold',5000);
[ff1 vp1] = extractFeatures(I1, f1);
[ff2 vp2] = extractFeatures(I2, f2);

idx = matchFeatures(ff1, ff2);
matchedpoints1 = vp1(idx(:,1),:);
matchedpoints2 = vp2(idx(:,2),:);

showMatchedFeatures(I1, I2, matchedpoints1, matchedpoints2);
loc1 = getfield(matchedpoints1, 'Location');
loc2 = getfield(matchedpoints2, 'Location');

disparity =   sqrt(sum((loc1-loc2).^2,2));
depth = focal*baseline./disparity;
  
Itext = insertText(I1,loc1, depth);
loc1=round(loc1);
for i=1:size(depth)
depthIm(loc1(i,1),loc1(i,2))=depth(i);
end
figure
imshow(Itext);