function param = estwarp_condens_PLS(frm, tmpl, param, opt)

%% Copyright (C) Qing Wang.
%% All rights reserved.

n = opt.numsample;
sz = size(tmpl.mean);
N = sz(1)*sz(2);

param.param = repmat(affparam2geom(param.est(:)), [1,n]); 
param.param = param.param + randn(6,n).*repmat(opt.affsig(:),[1,n]); 
wimgs = warpimg(frm, affparam2mat(param.param), sz);

diff = reshape(wimgs,[N,n]) -  repmat(tmpl.mean(:),[1,n]); 
coef = tmpl.W'*diff;  
diff = coef - repmat(tmpl.template,[1,n]);

param.conf = exp(-sum(diff.^2)./opt.condenssig)';  


param.conf = param.conf ./ sum(param.conf); 
[~,maxidx] = max(param.conf);
param.est = affparam2mat(param.param(:,maxidx));
param.wimg = wimgs(:,:,maxidx);
param.proj = coef(:,maxidx); 


