function [springROI, pxmmRatio] = selectSpringAndGridOnImage(imgpath)

img = imread(imgpath);
figure(1); imshow(img);
set(gca,'position',[0 0 1 1],'units','normalized');  % no border

choice = '';

while ~strcmp(choice, 'Confirm')    
    close(figure(2));
    figure(1);
    
    uiwait(msgbox('Select the area where the spring is located in the image', ...
    'Select Spring', ...
    'modal'));
    
    rect = getrect(figure(1));  % spring selection input rectangle ( top-left point + length)
    disp(['Selected rectangle: (xmin: ' num2str(rect(1)) ', ymin: ' ...
        num2str(rect(2)) ', width: ' num2str(rect(3)) ', height: ' ...
        num2str(rect(4)) ')']);
    
    ROI_xmin = max(1, floor(rect(1)));    
    ROI_ymin = max(1, floor(rect(2)));
    ROI_width = floor(rect(3));
    ROI_height = floor(rect(4));
    
    % negative xmin
    if rect(1) < 1
       ROI_width = floor(rect(1)) + ROI_width; 
    end
    
    % negative ymin
    if rect(2) < 1
        ROI_height = floor(rect(2)) + ROI_height;
    end
    
    % width too large
    if ROI_width + ROI_xmin > size(img,2)
        ROI_width = size(img,2) - ROI_xmin;
    end
    
    % height too large
    if ROI_height + ROI_ymin > size(img,1)
        ROI_height = size(img,1) - ROI_ymin;
    end
    
    springROI = img(ROI_ymin:ROI_ymin+ROI_height, ROI_xmin:ROI_xmin+ROI_width, :);
    
    figure(2); imshow(springROI);

    % Construct a questdlg with two options
    choice = questdlg('Confirm spring selection.', ...
        'Confirm Selection', ...
        'Confirm','New Selection','New Selection');
end

figure(1);
choice = '';
while ~strcmp(choice, 'Confirm')
    delete(findobj(gca,'Type','line','Marker','+'));
    uiwait(msgbox('Select 4 corners inside the reference grid! Order does not matter!','Reference Grid','modal'));
    click_x = zeros(1,4);
    click_y = zeros(1,4);
    for i = 1:4
        [click_x(i), click_y(i)] = ginput(1);
        hold on; plot(click_x(i), click_y(i), 'Marker', '+', 'Color', 'red', 'MarkerSize', 12);
    end
    
    % Construct a questdlg with two options
    choice = questdlg('Confirm reference grid selection.', ...
        'Confirm Selection', ...
        'Confirm','New Selection','New Selection');
end

promptAlongX = 'Specify the amount of squares inside the selected area along X axis: ';
promptAlongY = 'Specify the amount of squares inside the selected area along Y axis: ';
promptGridDivision = 'Specify grid division in mm: ';
squaresAlongX = input(promptAlongX);
squaresAlongY = input(promptAlongY);
gridDivision = input(promptGridDivision);

click_x_mean = mean(click_x);
click_y_mean = mean(click_y);

clicks = zeros(4,2);
for i = 1:4
    if click_x(i) < click_x_mean && click_y(i) > click_y_mean
        clicks(1,1) = click_x(i);
        clicks(1,2) = click_y(i);
    elseif click_x(i) > click_x_mean && click_y(i) > click_y_mean
        clicks(2,1) = click_x(i);
        clicks(2,2) = click_y(i);
    elseif click_x(i) > click_x_mean && click_y(i) < click_y_mean
        clicks(3,1) = click_x(i);
        clicks(3,2) = click_y(i);
    elseif click_x(i) < click_x_mean && click_y(i) < click_y_mean
        clicks(4,1) = click_x(i);
        clicks(4,2) = click_y(i);
    else
        error('Unhandled grid selection click case!');
    end
end

grid_distances = zeros(1,4);
for i = 1:4
    grid_distances(i) = pdist([clicks(i,:); clicks(mod(i,4)+1,:)], 'euclidean');  % from lower-left corner to upper-left corner, anti-clockwise
end

pxmmRatio = mean([(mean([grid_distances(1),grid_distances(3)]) / squaresAlongX), (mean([grid_distances(2),grid_distances(4)]) / squaresAlongY)]) / gridDivision;