function [output,l_list]=fuzzy_threshold(input)

%This function finds the fuzzy threshold value of
%an image and applies the threshold function
%at this value.
%-----------------------------------------------

input=double(input);
histo=histogram(input);
[rows,cols]=size(input);
output=zeros([rows,cols]);

sig_factor=round(0.05*max(histo)); 
%a pixel value is significant if it occurs more than 0.05 times the %occurance of the most occuring pixel

flag=0;
p_start=0;
for i=1:1:length(histo) %for each pixel value
    if (histo(i)>sig_factor)&&(flag==0) %check if its significant
        flag=1;                         %if yes, define it as the start
        p_start=i;
    end;
end;

flag=0;
p_end=0;
for i=length(histo):-1:1 %for each pixel value
    if (histo(i)>sig_factor)&&(flag==0) %check if its significant
        flag=1;                         %if yes, define it as the end
        p_end=i;
    end;
end;

p_range=p_end-p_start;
x=0:1:255;
%generate fuzzy seet transfer function
y=zmf(x,[round(p_start+0.1*p_range),round(p_start+0.4*p_range)]);
sum=0;




for i=1:1:rows
    for j=1:1:cols
        %calculate the number of dark pixels
        sum=sum+y(input(i,j)+1);
    end
end


thresh_value=p_start;
i=histo(1);
while (i<sum)
    thresh_value=thresh_value+1;
    i=i+histo(thresh_value);
end;
%generate thresholded output
output=uint8(threshold(input,thresh_value));
