function findHeight(img, Hr)
% this function returns the height of an object in the image
%
% user will be asked to click on points that define parallel lines in order
% to find the vanishing line, vertical vanishing point and the top and base
% of the reference object once. Then for every object measured, the user
% will need to click on its top and base.
%%
imshow(img); hold on;
title('click on four points to define two parallel lines');
pts = ginput(2);
line([pts(1,1), pts(2,1)],[pts(1,2), pts(2,2)],'Color','r','LineWidth',2)
line1 = cross([pts(1,1); pts(1,2); 1], [pts(2,1); pts(2,2); 1]);

pts = ginput(2);
line([pts(1,1), pts(2,1)],[pts(1,2), pts(2,2)],'Color','r','LineWidth',2)
line2 = cross([pts(1,1); pts(1,2); 1], [pts(2,1); pts(2,2); 1]);
pt1 = cross(line1,line2); pt1=pt1/pt1(3);

xMin = min(0,pt1(1));
xMax = max(size(img,2),pt1(1));
xlim([xMin xMax]);
ylim([min(0,pt1(2)) max(size(img,1),pt1(2))]);
plot(pt1(1), pt1(2), 'or');
%
title('Now define another set of parallel lines in a similar way');
pts = ginput(2);
line([pts(1,1), pts(2,1)],[pts(1,2), pts(2,2)],'Color','r','LineWidth',2)
line1 = cross([pts(1,1); pts(1,2); 1], [pts(2,1); pts(2,2); 1]);
pts = ginput(2);
line([pts(1,1), pts(2,1)],[pts(1,2), pts(2,2)],'Color','r','LineWidth',2)
line2 = cross([pts(1,1); pts(1,2); 1], [pts(2,1); pts(2,2); 1]);
pt2 = cross(line1,line2);  
pt2=pt2/pt2(3);
xlim([min(xMin,pt2(1)) max(xMax,pt2(1))]);
ylim([min(0,pt2(2)) max(size(img,1),pt2(2))]);
plot(pt2(1), pt2(2), 'or');

horizon = cross(pt1,pt2);
line([pt1(1), pt2(1)],[pt1(2), pt2(2)],'Color','y','LineWidth',2)

%horizon = horizon/horizon(3);

x1=1; y1 = (horizon(1)*x1+horizon(3))/horizon(2);
x2=size(img,2); y2 = (horizon(1)*x2+horizon(3))/horizon(2);
[x1 y1 x2 y2]
line([x1,x2],[-y1,-y2], 'Color', 'y', 'LineWidth',3);

title('Now define another set of parallel lines in the vertical direction');

pts = ginput(2);
line([pts(1,1), pts(2,1)],[pts(1,2), pts(2,2)],'Color','r','LineWidth',2)
line1 = cross([pts(1,1); pts(1,2); 1], [pts(2,1); pts(2,2); 1]);
pts = ginput(2);
line([pts(1,1), pts(2,1)],[pts(1,2), pts(2,2)],'Color','r','LineWidth',2)
line2 = cross([pts(1,1); pts(1,2); 1], [pts(2,1); pts(2,2); 1]);
v = cross(line1,line2); %vertical vanishing point
%
title('click on the top of the referece object');
a = ginput(1); tr = [a(1); a(2); 1];
title('click on the base of the referece object');
a = ginput(1); br = [a(1); a(2); 1];

alpha = -norm(cross(br,tr))/(Hr*dot(horizon,br)*norm(cross(v,tr)));

%
repeat = 'y';
while repeat=='y'
    title('click on the top of the object to be measured');
    a = ginput(1); tx = [a(1); a(2); 1];
    title('click on the base of the object to be measured');
    a = ginput(1); bx = [a(1); a(2); 1];
    Hx = -norm(cross(bx,tx))/(alpha*dot(horizon,bx)*norm(cross(v,tx)));
    fprintf('Height of object is %3.3f\n', Hx);
    
    % NOW using cross-ratios
    linebrbx = cross(br,bx); % line that joins the base points br and bx
    q = cross(linebrbx,horizon); q=q/q(3); % intersection of horizon with above line
    lineqtr = cross(q,tr); % line that joins q and tr
    linebxv = cross(bx,v); % line that joins bx and vanishing point v
    i = cross(lineqtr, linebxv); i=i/i(3);
    
    cRatio = ( norm(i-bx)/norm(v-bx))/(norm(i-tx)/norm(v-tx));
    if norm(i-bx)<norm(tx-bx)
        cRatio = -cRatio;
    end
    Hx2 = Hr*(cRatio - 1)/cRatio;
    fprintf('Height of object using X-ratio is %3.3f\n\n', Hx2);
    
    
    
    %repeat = input('Would you like to measure the height of another object? (y/n) ','s');
    

end
 





