function guides = textfitguides(binImg, rects, params, opts)
% TEXTFITGUIDES Fit polynomials to top and bottom of binarized text image
%
%  GUIDES = TEXTFITGUIDES(BINIMG, RECTS, PARAMS, OPTS) where BINIMG
%    is a logical image indicating the location of text, RECTS is
%    an Nx4 matrix of text rectangle coordinates with each row in [x y w h]
%    format. PARAMS is a struct. OPTS is a struct which may have fields:
%      guideOutlierLogProb
%      guideToleranceVar
%    which are documented in the VIDI function. Output GUIDES is a
%    struct with the following fields:
%      bottom     1x3 coefficient vector for bottom guide
%      top        1x3 coefficient vector for top guide
%      prunedImg  Logical subset of binImg with distractor components pruned
%      bottomsImg Logical subset of binImg containing candidate bottom points
%      topsImg    Logical subset of binImg containing candidate top points
%
% See also TEXTBINGROUP, TEXTBINRECTS, 
  
    
% Copyright (c) 2014
% Jerod Weinman
% jerod@acm.org

% $Id$
 
% Jerod Weinman
% jerod@acm.org
% /home/data/svn/vision/tools/package/GPL_STATEMENT
 

   % Verify number of inputs
  if nargin~=4
    error('vidi:textfitguides','Wrong number of input arguments')
  end;
  
  % Verify binarized image
  if ~(islogical(binImg) && ismatrix(binImg) )
    error('vidi:textfitguides','Input binImg must be a logical matrix');
  end;
    
  % Verify rectangles
  if ~(isnumeric(rects) && ndims(rects)==2 && size(rects,2)==4)
    error('vidi:textfitguides','Input rects must be an Nx4 matrix');
  end;

  % Verify params
  if ~isa(params,'struct')
    error('vidi:textfitguides','Input params must be a struct');
  end;

  % Verify opts
  if ~isa(opts,'struct')
    error('vidi:textfitguides','Input opts must be a struct');
  end;
  
  % Check for outlier (negative) log probability
  if isfield(opts,'guideOutlierLogProb')
    if isempty(opts.guideOutlierLogProb) || iscalar(opts.guideOutlierLogProb)
      outlierLogProb = opts.guideOutlierLogProb;
    else
      error('vidi:textfitguides','opts.guideOutlierLogProb must be a scalar');
    end
  else
    outlierLogProb = []; % Empty will cause default value to be used
  end

    % Check for outlier (negative) log probability
  if isfield(opts,'guideToleranceVar')
    if isempty(opts.guideToleranceVar) || ...
          ( ismatrix(opts.guideToleranceVar) && ...
            ndims(opts.guideToleranceVar)==2 && ...
            size(opts.guideToleranceVar,1)==2 && ...
            size(opts.guideToleranceVar,2)==3 )
      toleranceVar = opts.guideToleranceVar
    else
      error('vidi:textfitguides', ...
            'If non-empty, opts.guideToleranceVar must be a 2x4 matrix');
    end
  else
    toleranceVar = []; % Empty will cause default value to be used
  end
  
  
  % Run guide-fitting algorithm
  [botCoeff,topCoeff,prunedImg,bottomsImg,topsImg] = ...
            basetoplocal5(binImg,rects,outlierLogProb,toleranceVar);

  % Package results into a single struct
  guides = struct('bottom',     botCoeff, ...
                  'top',        topCoeff, ...
                  'prunedImg',  prunedImg, ...
                  'bottomsImg', bottomsImg, ...
                  'topsImg',    topsImg );
                  
  % Combine guide coefficients
  %guideCoeff = [bot_coeff ; top_coeff];
  
  % Combine three result images into one
  %guideImg = uint8( 255 * cat(3,tops_img,bottoms_img,pruned_img) );
  
                  
  
