function [coeff,pruned_img] = pruneregress(bin_img,prior_coeffs,rects)
%PRUNEREGRESS Least squares regression on a pruned binary image.
%
%   COEFF = PRUNEREGRESS(BIN_IMG,PRIOR_COEFFS) returns a 1x3 vector COEFF of
%   the regression coefficients from the quadratic model y = ax^2 + bx + c.
%   PRIOR_COEFFS is the coefficients of a previous regression using the same
%   quadratic model. The algorithm prunes the image by taking only the
%   connected components that intersect the line from PRIOR_COEFFS. Then the
%   points of these pruned connected components are passed into the quadratic
%   regression. The coefficients are based upon an origin at the top-left
%   corner or (0,0) of the original image. 
%
%   [COEFF,PRUNED_IMG] = PRUNEREGRESS(..) where PRUNED_IMG is the
%   image containing only components that intersect the curve specified by
%   the prior coefficients.
%
%   PRUNEREGRESS(...,RECTS) RECTS is an Nx4 matrix containing all of
%   the collinear word rectangles. The rectangles contain [col row w
%   h] corresponding to the point (col,row) that is the top-left
%   corner of the rectangle, w is the width, and h is the
%   height. RECTS may be used for determining priors for the
%   regression, however this is not currently implemented.
%
%   When this function runs the regression, it takes the data points from the
%   pruned components, moves the origin of the data points to the center of the
%   data, regresses, then shifts the origin back to the original image
%   coordinate system.
%
%   This regression assumes that the data provided is part of a
%   collinear set of rectangles. Thus the regression uses all
%   points in the image, regardless of the information provided in
%   RECTS.
%
%   See also PRUNE, INITREGRESS

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

  deg = length(prior_coeffs) - 1;
  
  
  if deg==2
    in_coeffs = prior_coeffs;
  else
    in_coeffs = [prior_coeffs 0]; % Add a second-order term
  end
    
  pruned_img = prune(bin_img,in_coeffs);
  
  % nothing in pruned image?
  if ~any(any(pruned_img))
    % what bad luck. it's probably better to keep everything and have at it.
    pruned_img = bin_img;
    coeff = prior_coeffs;
  else
    % re-run regression on pruned image
    coeff = initregress(pruned_img, deg);
  end
  
