%% 
% 3D distance calculations using a light source
clc
clear
image = imread('fake_light_throat.bmp');    %new baseline
%image = imread('testPic.png');    %new baseline
red_threshold = 34;
green_threshold = 150;
depth = 0;
dot_diameter = 0;
disparity = 0;
focal_length = 301.8239;


figure
imshow(image)
% Segment the image using the red channel.
% Green dot has a very low red channel response.
for i=1:size(image,1)
    for j=1:size(image,2)
        if (image(i,j,1) > red_threshold)
            image(i,j,1) = 0;
            image(i,j,2) = 0;
            image(i,j,3) = 0;
        end
    end
end

% Re-segment the image using the green channel. 
% (To eliminate false positives)
% Green dot has a very high green channel response.
left_most_point_x = Inf;
right_most_point_x = -Inf;
for i=1:size(image,1)
    for j=1:size(image,2)
        if (image(i,j,2) < green_threshold)
            image(i,j,1) = 0;
            image(i,j,2) = 0;
            image(i,j,3) = 0;
        else
            if j < left_most_point_x
                left_most_point_x = j;
            end
            if j > right_most_point_x
                right_most_point_x = j;
            end
        end
    end
end
figure
imshow(image)
dot_diameter = right_most_point_x - left_most_point_x;
