function labeling(purpose, image_filename)
% This function allows one to label a total of 50 corners of an image
% and save them into a .mat file.
num_of_corners = 50;
[img, map] = imread(['./' purpose '/' image_filename]);
[by, bx] = size(img);
figure;
imshow(im2double(ind2rgb(img,map)));
corners = zeros(num_of_corners, 2);
c = 0;
while c < num_of_corners
    [x,y] = ginput(1);
    R_xy = round([x,y]);
    x = R_xy(1);
    y = R_xy(2);
    if ~is_valid(x,y, corners(:,1), corners(:,2), bx, by)
       disp('Input not valid. Please repeat.');
       continue;
    end
    c = c + 1;
    disp(['Labelling ' image_filename, ' -- corner number: ' num2str(c)]);
    corners(c,:) = [x,y];
end
save([purpose '_' image_filename(1:end-4), '_corners.mat'], 'corners');
close;
end

function res = is_valid(x,y, xv, yv, bx, by)
    cond1 = x > 0 && y > 0 && x < bx && y < by;
    r1 = xv == x;
    r2 = yv == y;
    cond2 = sum(r1 & r2)==0;
    res = cond1 & cond2;
end