function [base_coeff,top_coeff,pruned_img,bottoms_img,tops_img] = ...
      basetoplocal5(bin_img,rects,U,tol)
% BASETOPLOCAL5 Robust text baseline regression and character height estimation
%
%   [COEFF,DIST,PRUNED_IMG,BOTTOMS_IMG,TOPS_IMG,DISTANCES] = 
%      BASETOPLOCAL5(BIN_IMG,RECTS) returns a vector,
%   COEFF, of length DEG containing the coefficients, [c b] or [c b
%   a], of the baseline of the binarized text contained in BIN_IMG. In
%   the case when DEG=2, the baseline coefficients, COEFF, are from
%   the quadratic model y = ax^2 + bx + c. When DEG=1, the
%   coefficients COEFF are from a linear model y=b*x+c. The
%   coefficient's coordinates are with respect to the image coordinate
%   system. DIST measures the typical (median) distance between the
%   bottom and top of a character along a normal of the curve. Note
%   this is not synonymous with font height. RECTS is an Nx4 matrix whose
%   rows contain [x y w h] word bounding box coordinates.
%
%   BASETOPLOCAL5(...,U) is an optional scalar uniform outlier
%   negative log probability. May be empty to use the default.
%
%   BASETOPLOCAL5(...,TOL) is a 2x3 matrix containing the tolerance
%   (variance) for normal deviation from the coefficients of the prior
%   guide curve, which is typically a robust fit to the pruned binary
%   image for the base guide (first row of tol), and the resulting
%   base guide for the top guide (second row of tol). May be empty to
%   use the default.
%
%   BASETOPLOCAL5 uses the method described in Section 5 of the following article:
%
%     J. Weinman, Z. Butler, D. Knoll, and J. Feild, Toward Integrated Scene Text
%     Reading. IEEE TPAMI 36(2):375--387, 2014.doi:10.1109/TPAMI.2013.126
%
%   See also INITREGRESS, PARABOLICREGRESS, PRUNEREGRESS, REGRESS,
%   BASEREGRESS.
%
% Copyright (c) 2012
% Jerod Weinman
% jerod@acm.org

% $Id: basetoplocal5.m 594 2014-10-24 12:04:55Z weinman $
 
% Jerod Weinman, Zach Butler, Dugan Knoll, Alexander Turner, Yirong Jing
% jerod@acm.org
% Copyright 2011, 2012, 2013, 2014
 
%    This file is part of PIONEER WordNorm.
%
%    PIONEER WordNorm is free software: you can redistribute it and/or modify
%    it under the terms of the GNU General Public License as published by
%    the Free Software Foundation, either version 3 of the License, or
%    (at your option) any later version.
%
%    PIONEER WordNorm is distributed in the hope that it will be useful,
%    but WITHOUT ANY WARRANTY; without even the implied warranty of
%    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%    GNU General Public License for more details.
%
%    You should have received a copy of the GNU General Public License
%    along with PIONEER WordNorm.  If not, see <http://www.gnu.org/licenses/>.


 if nargin<3 || isempty(U)
     % Set the default outlier uniform negative log probability value
     avgheight = wmean(rects(:,4),rects(:,3)); % Mean height weighted by width
     U = log(avgheight);
 end;
 
 if nargin<4 || isempty(tol)
    tol = [1 1e-3 1e-5 ; ...
           1 1e-7 1e-10];
 end

 [base_coeff1, top_coeff1,...
  pruned_img1, bottoms_img1, tops_img1,...
  base_evidence1, top_evidence1] = basetoplocal3(bin_img,1,rects,U,tol);


 [base_coeff2, top_coeff2,...
  pruned_img2, bottoms_img2, tops_img2,...
  base_evidence2, top_evidence2] = basetoplocal3(bin_img,2,rects,U,tol);

  % Adjusts for bias in the DEG=1 evidence
  base_missing1 = sumv(~bottoms_img1 & bottoms_img2);
  base_evidence1 = base_evidence1 - base_missing1*U;
  top_missing1 = sumv(~tops_img1 & tops_img2);
  top_evidence1 = top_evidence1 - top_missing1*U;
  evidence1 = base_evidence1 + top_evidence1;
  
  % Adjusts for bias in the DEG=2 evidence
  base_missing2 = sumv(bottoms_img1 & ~bottoms_img2);
  base_evidence2 = base_evidence2 - base_missing2*U;
  top_missing2 = sumv(tops_img1 & ~tops_img2);
  top_evidence2 = top_evidence2 - top_missing2*U;
  evidence2 = base_evidence2 + top_evidence2;

  if (evidence2 > evidence1)
    base_coeff = base_coeff2;
    pruned_img = pruned_img2;
    bottoms_img = bottoms_img2;
    top_coeff = top_coeff2;
    tops_img = tops_img2;
  else
    base_coeff = base_coeff1;
    base_coeff(3) = 0;
    top_coeff = top_coeff1;
    top_coeff(3) = 0;
    pruned_img = pruned_img1;
    bottoms_img = bottoms_img1;
    tops_img = tops_img1;
  end;
