function boundaryBench(pbDir,pres,nthresh,fast)
% function boundaryBench(pbDir,pres,nthresh,fast)
%
% Run the boundary detector benchmark on the Pb files found in
% pbDir for the BSDS test images.
%
% See also imgList, bsdsRoot.
%
% David Martin <dmartin@eecs.berkeley.edu>
% March 2003

if nargin<3, nthresh=1; end
if nargin<4, fast=0; end

iids_test = load(fullfile(bsdsRoot,'iids_test.txt'));
iids = iids_test(:)';

cntR_total = zeros(nthresh,1);
sumR_total = zeros(nthresh,1);
cntP_total = zeros(nthresh,1);
sumP_total = zeros(nthresh,1);
scores = zeros(numel(iids),5);

for i = 1:numel(iids),
  iid = iids(i);
  %fprintf(2,'Processing image %d/%d (iid=%d)...\n',i,numel(iids),iid);
  fprintf(2,'Processing image %d.bmp...\n',iid);

  pbFile = fullfile(pbDir,sprintf('%d.bmp',iid));
  fprintf(2,'  Reading input file...\n');
  pb = double(imread(pbFile))/255;
  if ndims(pb)~=2,
    error(sprintf('pb file ''%s'' is not grayscale',pbFile));
  end

  fprintf(2,'  Reading ground truth...\n');
  segs = readSegs(pres,iid);

  % Make sure the pb and the segmentations are the same size.
  if numel(pb)<numel(segs{1}),
    % upsample pb to the size of the segmentations
    pb = imresize(pb,size(segs{1}),'nearest');
  elseif numel(pb)>numel(segs{1}),
    error('Cannot evaluate pb bigger than segmentations.');
  end

  if fast,
    fprintf(2,'  Calculating precision/recall (fast method) ');
    [thresh,cntR,sumR,cntP,sumP] = boundaryPRfast(pb,segs,nthresh);
  else
    fprintf(2,'  Calculating precision/recall... \n');
    [thresh,cntR,sumR,cntP,sumP] = boundaryPR(pb,segs,nthresh);
  end
  
%  fprintf(2,'  Writing results...\n');

  R = cntR ./ (sumR + (sumR==0));
  P = cntP ./ (sumP + (sumP==0));
  %F = fmeasure(R,P);
    
  fprintf('RESULT: RECALL=%f, PRECISION=%f \n -- results saved in %d_rp.txt\n',R,P,iid);


  fname = fullfile(pbDir,sprintf('%d_rp.txt',iid));
  fid = fopen(fname,'w');
  if fid==-1, 
    error(sprintf('Could not open file %s for writing.',fname));
  end
  fprintf(fid,'%f %f\n',[R P]');
  fclose(fid);
end
