function N_features=cp_dunn(x,FeatureFN,ImageId,NonlinId,SmoothId,NormId,FeatDec,sigma,N_s)
% CP_DUNN  Comparative texture measure study: Optimal Gabor filter features
% N_features=cp_dunn(x,FeatureFN,ImageId,NonlinId,SmoothId,NormId,...
%   FeatDec,sigma,N_s)

% Initialization
N_features=1;
if exist('sigma')~=1, sigma=[]; end
if isempty(sigma),    sigma=8;  end
if exist('N_s')~=1,   N_s=[];   end

% Filter design
[x_1,N_tex]=cp_image(ImageId,1); x_1=x_1-mean(x_1(:));
[x_2,N_tex]=cp_image(ImageId,2); x_2=x_2-mean(x_2(:));
if N_tex==2
  [h,U,V,J_min]=dunn(x_1,x_2,sigma,N_s,'Verbose');
else
  error('More than two textures not yet supported with Dunns method');
end
[M_h,N_h]=size(h);
fprintf(1,'(1 filter, P_e=%4.2f)',J_min);

% Filtering
if M_h>15 | N_h>15
  H=zeros(size(x)); H(1:M_h,1:N_h)=h; H=fft2(H);
  y=ifft2(H .* fft2(x));
else
  y=filter2(h,x);
end

% Local energy function
sigma_p=2*sigma;
f_0=1./(2*sqrt(2)*sigma_p);
z=cp_energy(y,NonlinId,SmoothId,NormId,f_0);

% Write feature file
catframe(z,FeatureFN,[],'Interm',FeatDec);
