close all; clear all;
warning('off','images:initSize:adjustingMag');  % surpress image size too big to fit screen warning

imgpath = 'Spring_1.bmp';

[img, ratio] = selectSpringAndGridOnImage(imgpath);
[vertices, ashape, edge, gray, bw] = detectVertices(img);

figure; imshow(bw); hold on;
%plot(vertices(:,1), vertices(:,2), 'r.');

centers = clusterizeVertices(vertices,20);
plot(centers(:,1),centers(:,2),'gx');

[connectionMatrix, distanceMatrix] = computeGraph(centers, 1);

[springLength, maxDeflectionAngle, maxDeflectionLocation, springDeflection, avgPitch, maxPitch, maxPitchLocation, pitchStdDeviation, midpointMatrix, edgeMidpointMatrix, errorString] = measureValues(bw, distanceMatrix, connectionMatrix, centers, ratio);

% check for errors in measureValues
if (isempty(errorString))
    plotResult(img, springLength, maxDeflectionAngle, maxDeflectionLocation, springDeflection, avgPitch, maxPitch, maxPitchLocation, pitchStdDeviation, midpointMatrix, edgeMidpointMatrix, distanceMatrix, connectionMatrix, centers);
else
    fprintf(2,errorString);
end