# GetNaturalStat

## PURPOSE

GetNaturalStat - Obtain statistics from natural images.

## SYNOPSIS

function GetNaturalStat

## DESCRIPTION

     GetNaturalStat - Obtain statistics from natural images.

## CROSS-REFERENCE INFORMATION

This function calls:

- applyfilterfftsameLocalNorm applyfilterfftsameLocalNorm - Filter image by a bank of filters with local normalization.
- makefilter makefilter - Make Gabor filters at fixed scale with norient orientations

This function is called by:

- StartFromHere StartFromHere - The entry point of this project.

## SOURCE CODE

    0001 function GetNaturalStat
    0002 % GetNaturalStat - Obtain statistics from natural images.
    0003 %
    0004
    0005 % CONSTANTS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    0006 scale = .7;  % scale of Gabors, length = 17 pixels
    0007 norient = 16;  % number of orientations
    0008 Upperbound = 6.; % saturation value
    0009 binsize = .2;  % binsize for computing F
    0010
    0011 % TRAINING EXAMPLES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    0012 rootFolder = '..';
    0013 negFolder = sprintf('%s/image/neg',rootFolder);  % image input folder
    0014 negTrainImages = dir(sprintf('%s/neg*.jpg', negFolder));
    0015 nimage = length(negTrainImages); % #images
    0016 nimage = min(2,nimage); % enough to use the first 2 images only
    0017 negTrainImages = negTrainImages(1:nimage);
    0018 Isize = zeros(nimage, 2);
    0019 I = cell(1,nimage);
    0020 for i = 1 : nimage
    0021     tmpIm = imread([negFolder '/' negTrainImages(i).name]);
    0022     if size(tmpIm,3) == 3
    0023         tmpIm = rgb2gray(tmpIm);
    0024     end
    0025     I{i} = double(tmpIm);
    0026     Isize(i, :) = size(I{i});
    0027 end
    0028
    0029 % FILTERING IMAGES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    0030 disp('start filtering');
    0031 tic
    0032 [allfilter allsymbol] = makefilter(scale, norient);  % generate Gabor filters
    0033 h = (size(allfilter{1}, 1)-1)/2;  % half size of Gabor
    0034 allfiltered = cell(nimage,norient);
    0035 for i = 1:nimage
    0036     allfiltered(i,:) = applyfilterfftsameLocalNorm(I(i), allfilter);  % filter training images
    0037 end
    0038 disp(['filtering time: ' num2str(toc) ' seconds']);
    0039
    0040 binnum = floor(Upperbound/binsize)+1;  % binnumbers
    0041 histog = zeros(binnum, 1);  % store F
    0042
    0043 % MEX-C %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    0044 disp('start histogramming');
    0045 Chistog(nimage, norient, allfiltered, h, Isize(:,1), Isize(:,2), binsize, binnum, histog, Upperbound);
    0046 disp(['histogramming time: ' num2str(toc) ' seconds']);
    0047
    0048 % RESULTS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    0049 r = (0:(binnum-1))*binsize;
    0050
    0051 M = 50;
    0052 e = zeros(M, 1);
    0053 Z = zeros(M, 1);
    0054 for k=1:M
    0055     lambda = (k-1.)/10.;
    0056     p = exp(lambda*r).*(histog');
    0057     Z(k) = sum(p*binsize); p = p/Z(k);
    0058     %plot(r, p);
    0059     e(k) = sum(r.*p*binsize);
    0060 end
    0061 lam = (0:(M-1))/10.;
    0062 lz = log(Z);
    0063
    0064 save(sprintf('%s/result/natural_stat.mat',rootFolder), 'binnum','M',...
    0065     'lam', 'e', 'lz', 'Upperbound',...
    0066     'scale', 'norient', 'allfilter', 'allsymbol');
    0067
    0068
    0069
    0070