function [x1pos, x2pos] = bivalue2(input)

% reshape the input matrix to a 1D array
[SR, SC] = size(input);
input = reshape(input, 1, SR*SC);

% N is the histogram of the image
%figure(200);
edge =linspace(0,255,256);
N = histc(input,edge);
%bar(edge,N,'histc');

count=0; %counter to record the number of 0s in the histogram
temp1 =1; %to store 1st bivalue
temp2 =1;  %to store 2nd bivalue

peaks = [N(1)];
peakpos = [1];
for i=2:1:255
%   if(N(i) == 0)
%      count = count+1;
%   end
	if (N(i)>N(i+1)) & (N(i)>N(i-1))
      peaks=[peaks N(i)];
   	peakpos = [peakpos i];   
   end
   
end

peaks = [peaks N(256)];
peakpos = [peakpos 256];

x1 = max(peaks);
r = find(peaks == x1);
x1pos = peakpos(r(1))-1;
%if length(r)>1
%   x2 = x1;
%%   x2pos = x1pos;
 %  return;
%end
peaks=[peaks(1:r-1) peaks(r+1:length(peaks))];
x2= max(peaks);
r = find(peaks == x2);
x2pos = peakpos(r(1))-1;