% script for computing 2nd moment texture descriptors using automatic scale 
% selection
%

[L1,L2,phi,aniso,pol,con,window_sizes]=wismm3(L/100);
%mask=con(:,:,3)>0.1;
mask=mean(con,3)>0.05;
s=sqrt(window_sizes-1)/2;
s2=[s 4];
% perform scale selection based on polarity
pol2=pol;
for n=1:length(s)
   N=4*s2(n+1)^2+1;
   pol2(:,:,n)=gaussN(pol(:,:,n),N);
end

dif=diff(pol2,1,3);
thresh=0.02; 
mxm_t=abs(dif)<=thresh;
%whos
mxm_t(:,:,end)=1;
for n=1:length(s)-1
   mxm_t(:,:,n)=n*mxm_t(:,:,n);
end
mxm_t=mxm_t+10*max(s2)*(mxm_t==0);
%mxm_t=mxm_t./(mxm_t>0);

[junk,min_pos]=min(mxm_t,[],3);
ss=s(min_pos);
ss=mask.*ss;
SSNEW=4*ss.^2+1;
clear mxm_t

% now pull out the descriptors from the selected scales
aniso_s=zeros(size(L));
con_s=zeros(size(L));
pol_s=zeros(size(L));
phi_s=zeros(size(L));

for n=2:length(window_sizes)
   BW=SSNEW==window_sizes(n);
   aniso_s=(1-BW).*aniso_s+BW.*squeeze(aniso(:,:,n));
   con_s=(1-BW).*con_s+BW.*squeeze(con(:,:,n));
   pol_s=(1-BW).*pol_s+BW.*squeeze(pol(:,:,n));
   phi_s=(1-BW).*phi_s+BW.*squeeze(phi(:,:,n));
end



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Copyright (c) 1997-1999. The Regents of the University
% of California (Regents). All Rights Reserved.
% 
% Permission to use, copy, modify, and distribute this software and its
% documentation for educational, research, and not-for-profit purposes,
% without fee and without a signed licensing agreement, is hereby
% granted, provided that the above copyright notice, this paragraph and
% the following two paragraphs appear in all copies, modifications, and
% distributions. Contact The Office of Technology Licensing, UC Berkeley,
% 2150 Shattuck Avenue, Suite 510, Berkeley, CA 94720-1620, (510)
% 643-7201, for commercial licensing opportunities. Created by
% Chad Carson, Serge Belongie, and Jitendra Malik, Department of Electrical
% Engineering and Computer Sciences, University of California, Berkeley.
% For technical information, contact carson@eecs.berkeley.edu.
% 
% IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
% SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
% ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
% REGENTS HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
% 
% REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
% LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
% PARTICULAR PURPOSE. THE SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF
% ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS". REGENTS HAS NO OBLIGATION
% TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR
% MODIFICATIONS.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

