close all 
clear all


%% handle original image
imageOriginal = imread('seeds/Pr-Seeds-05.tif', 'TIF');
imageOriginal = imageOriginal(30:(end-170), 220:(end-200));
size(imageOriginal)
% imshow(imageOriginal)


%% edge detection
% first binarization / fill operations
image = imageOriginal > 160;
image = imfill(image, 'holes');
% imshow(image)

% find contour
image = edge(image, 'sobel');
[xVec yVec] = find(image == 1);

%% identify
rng(3)
bnd = obtainBnd([yVec'; -xVec']);
% bnd = obtainBnd([xVec'; yVec']);
fitShape = fitShapeBnd(bnd, 'lambdaLocal', 1, ...
    'lambdaError', 'sqRough');

meanWidth = fitShape.meanWidth
lambda = fitShape.lambda
roundness = 2*lambda / meanWidth

%% make figure
close all
figure,axis off
viewBoundaryShape(fitShape)
xlim([-270 280])
ylim([-285 281])
axes
imshow(~image)
hAxes = get(gcf, 'children');
set(gcf, 'children', hAxes(end:-1:1))

print('-dpng', '-r300', 'semFit.png')