%
%
% [B, sorted_edge] = SegmentPetalFromBackground(I);
%
% Input params: 
%            I - the input image
% Output params:
%            B - the binary image
%            sorted_edge - the edge of the objects in B, ordered.
%
%  Dr. A. I. Hanna 2005.
function [B, sorted_edge] = SegPetalFromBackgound(I)
%I = imread('11.JPG');
I = double(I);
I = I./max(I(:));
B = calcPetalBinim(I);
E = edge(double(B));
[x, y] = find(E == 1);
if length(x)>0
    sorted_edge = image_sort(x,y); 
else
    sorted_edge = E;
end
return;



function [binim] = calcPetalBinim(I);
I1 = I(:,:,1);
binim = imadjust(I1, stretchlim(I1),[])>.55;
binim = imclearborder(binim);
binim = bwareaopen(binim, 8000);
binim = imdilate(binim, strel('disk', 5));
binim = bwfill(binim, 'holes');