% 12.2.01 - Stephan Dale - CS6441 Computer Vision
% sg.m

% this script creates a depth map from an image by sampling the greylevels.
% mystereogram.m is then called to create a stereogram.

clear all;
echo off;

disp(' ');
disp('-- sg.m -- create a stereogram --');
disp(' ');

% the image must be a greylevel bitmap image
disp('depth image (greyscale indexed format) selection');
filename = input('please enter filename (surrounded by single quotes): ');
disp('loading depth image...');
depth_image = imread(filename);

% have to convert to double, as many simple mathematical operations are not defined on the type uint8
depth_image = double(depth_image);

height = size(depth_image,1);
width = size(depth_image,2);

% find the maximum and minimum greylevels, so that the depth map can be scaled
max_level = max(max(depth_image));
min_level = min(min(depth_image));

difference = max_level - min_level;

% subtract the minimum greylevel, so that they range from 0 to 'difference'
depth_image = depth_image - min_level;

% don't want parts of the image to lie on the screen - focussing on the screen
% will make you loose depth info.
max_depth = -1;
while max_depth<0 | max_depth>1
   max_depth = input('please enter max. depth (0-1): ');
end;
min_depth = 0.0;

step = (max_depth-min_depth)/difference;

% step through the depth image calculating an assotiated depth for each pixel
% this scales the depth map to values ranging from min_depth to max_depth
disp('calculating depth information...');
for i=1:width
   for j=1:height
      Z(j,i) = (depth_image(j,i)*step)+min_depth;
   end;
end;
% display depth map
figure;
colormap gray;
image(Z*size(colormap,1));

% call mystereogram.m to create and display the stereogram
disp('creating stereogram...');

disp('colour selection');
disp('0: all colours');
disp('1: reds');
disp('2: greens');
disp('3: blues');
colour = -1;
while colour<0 | colour>3
   colour = input('please enter 0-3: ');
end;
if colour==1
   colour = 'r';
elseif colour==2
   colour = 'g';
elseif colour==3
   colour = 'b';
else
   colour = 'a';
end;
num_colours = 0;
while num_colours<2 
   num_colours = input('please enter number of colours (>=2): ');  
end;

distance = 0;
while distance<10
   distance = input('please enter distance of eyes from image (in cm >=10): ');
end;
distance = distance*10;  % as mystereogram requires distance in mm
size = 0;
while size<=0
   size = input('please enter pixel size (in mm >0): ');
end;

im = mystereogram(Z,colour,num_colours,size,distance);
%im(1:100,10)
figure;
colormap default;
image(im);

disp(' ');
disp('-- end --');
disp(' ');
