a = vision.ImageDataTypeConverter; %creates the object converts the input image to a single precision data type.
b = vision.ColorSpaceConverter('Conversion','RGB to intensity');
leftI3 = step(a,imread('venusl.ppm')); % step = step response of dynamic system
leftI = step(b,leftI3);
rightI3 = step(a,imread('venusr.ppm'));
rightI = step(b,rightI3);
%*******************************************************************Basic_Block_Matching******************************************************************************************************
Dbasic = zeros(size(leftI), 'single');
disparityRange = 30;
% Selects (2*halfBlockSize+1)-by-(2*halfBlockSize+1) block
halfBlockSize = 3;
blockSize = 2*halfBlockSize+1; %7X7 block
% Allocate space for all template matcher System objects.
tmats = cell(blockSize);
% Initialize progress bar
hWaitBar = waitbar(0, 'Performing basic block matching...');
nRowsLeft = size(leftI, 1);
% Scan over all rows.
for m=1:nRowsLeft
% Set min/max row bounds for image block.
minr = max(1,m-halfBlockSize);
maxr = min(nRowsLeft,m+halfBlockSize);
% Scan over all columns.
for n=1:size(leftI,2)
minc = max(1,n-halfBlockSize);
maxc = min(size(leftI,2),n+halfBlockSize);
% Compute disparity bounds.
mind = max( -disparityRange, 1-minc );
maxd = min( disparityRange, size(leftI,2)-maxc );
% Construct template and region of interest.
template = rightI(minr:maxr,minc:maxc);
templateCenter = floor((size(template)+1)/2);
roi = [minc+templateCenter(2)+mind-1 ...
minr+templateCenter(1)-1 ...
maxd-mind+1 1];
% Lookup proper TemplateMatcher object; create if empty.
if isempty(tmats{size(template,1),size(template,2)})
tmats{size(template,1),size(template,2)} = ...
vision.TemplateMatcher('ROIInputPort',true);
end
thisTemplateMatcher = tmats{size(template,1),size(template,2)};
% Run TemplateMatcher object.
loc = step(thisTemplateMatcher, leftI, template, roi);
Dbasic(m,n) = loc(1) - roi(1) + mind;
end
waitbar(m/nRowsLeft,hWaitBar);
end
close(hWaitBar);
figure(3), clf;
imshow(Dbasic,[]), axis image, colormap('jet'), colorbar;
caxis([0 disparityRange]);
title('Depth map from basic block matching');
%********************************************************************************************************************************