function [coeff,evidence] = baseregress(bottoms, prior_coeffs, rects,...
                                         sigma2, U)
%BASEREGRESS regression on the bottom points of a pruned binary image
%
%   [COEFF,EVIDENCE] = BASEREGRESS(BOTTOMS,PRIOR_COEFFS) Where BOTTOMS
%   is a binary image where the 1 values are the bottom outline of
%   text. PRIOR_COEFFS is the coefficients of a previous regression
%   using the same quadratic model as COEFF described below. We will
%   use PRIOR_COEFFS as a prior for this model's regression.
%
%   It returns COEFF which is a vector containing [c b a], which are the
%   coefficients of the quadratic y = ax^2 + bx + c. This quadratic represents
%   the baseline of the binarized text. These coefficients are with respect to
%   the image coordinate system.
%
%   BASEREGRESS(BOTTOMS,PRIOR_COEFFS,RECTS) Where RECTS is an nx4
%   matrix containing the bounding rectangles for each of the n words
%   in our bottoms image. RECTS is used to optimize the parameters for
%   the regression formula.
%
%   /** Insert Final Regression Information Here */
%
%   This regression assumes that the data provided is part of a collinear set
%   of rectangles. In other words, it will fit a regression to all points in
%   in the image that intersect the PRIOR_COEFFS, regardless of the
%   information provided in RECTS.
%
%   See also BOTTOMS, INITREGRESS, PARABOLICREGRESS, PRUNEREGRESS, REGRESS,
%   BASELINE.

% Copyright (c) 2011
% Jerod Weinman
% jerod@acm.org

% $Id$
 
% 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/>.

    evidence = -Inf;

    deg = length(prior_coeffs)-1;
  
    [rows,cols] = find(bottoms);

    % If insufficient points, return the prior estimate
    if (length(rows) <= deg)
      coeff = prior_coeffs;
      return;
    end;
    
    % Attempt to compute the average size of the rectangles for parameters.
    if (nargin<2)
        avgheight = 2*range(rows);
    else
        avgheight = wmean(rects(:,4),rects(:,3)); % Mean height weighted by width
    end;

    % Translate the points so that the origin is in the center.
    rowshift = mean(rows);
    colshift = mean(cols);
    rows = rows - rowshift;
    cols = cols - colshift;

    % Translate the prior_coeffs so that it is relative to the new origin.
    prior_coeffs = translatequadratic(prior_coeffs, [-rowshift -colshift]);
    
    % Fit a quadratic to the data using priors
    [coeff,loglik,zz,evidence] = ...
        fitpoly1(deg, rows, cols, 0.5, ...
                 [prior_coeffs ; max([1,avgheight,sigma2(1)]) ...
                  sigma2(2:(deg+1))]', U);
    
    % Translate the curve back to the original image coordinate system.
    coeff = translatequadratic(coeff,[rowshift colshift]);
    
