# applyfilterfftsameLocalNormPerOri

## PURPOSE

applyfilterfftsameLocalNormPerOri Filter image by a bank of filters, with per-orientation local normalization.

## SYNOPSIS

function [allfiltered] = applyfilterfftsameLocalNormPerOri(I, allfilter)

## DESCRIPTION

     applyfilterfftsameLocalNormPerOri Filter image by a bank of filters, with per-orientation local normalization.

     I: input images
     allfilter: filter bank
     threshold: filter response is set to zero if below threshold

## CROSS-REFERENCE INFORMATION

This function calls:

This function is called by:

## SUBFUNCTIONS

- function v = sigmoidOnOriginalResponse(v)

## SOURCE CODE

    0001 function [allfiltered] = applyfilterfftsameLocalNormPerOri(I, allfilter)
    0002 % applyfilterfftsameLocalNormPerOri Filter image by a bank of filters, with per-orientation local normalization.
    0003 %
    0004 % I: input images
    0005 % allfilter: filter bank
    0006 % threshold: filter response is set to zero if below threshold
    0007
    0008 nimage = size(I, 2);    % number of training images
    0009 norient = size(allfilter, 2);     % number of orientations
    0010 h = (size(allfilter{1}, 1)-1)/2;  % half size of filters
    0011 [sx, sy] = size(I{1});  % size of images
    0012 allfiltered = cell(nimage, norient);  % filtered images
    0013
    0014 fftI = cell(1,nimage);
    0015 for i = 1 : nimage
    0016    fftI{i} = fft2(I{i}, sx+h+h, sy+h+h);
    0017 end
    0018 fftf = cell(1,norient);
    0019 for o = 1 : norient
    0020    fftf{o} = fft2(allfilter{1, o}, sx+h+h, sy+h+h);
    0021 end
    0022
    0023 mask = ones(sx,sy);
    0024 mask([1:h sx-h:sx],:) = 0; mask(:,[1:h sy-h:sy]) = 0;
    0025
    0026 for i = 1 : nimage
    0027    fftIi = fftI{i};
    0028    for o = 1 : norient
    0029        fftfo = fftf{o};
    0030        out = ifft2(fftIi.*fftfo);
    0031        filtered = out(h+1:h+sx, h+1:h+sy);
    0032        re = real(filtered); im = imag(filtered);
    0033        energy = re.*re + im.*im;  % compute the local energy
    0034        ave = mean(mean( energy(h+1:end-h,h+1:end-h) ));
    0035        allfiltered{i, o} = energy / ave;
    0036    end
    0037 end
    0038
    0039
    0040 radius = 20; % 20 for good results
    0041 aveFilter = ones(2*radius+1,2*radius+1)/((2*radius+1)^2);
    0042 for i = 1:nimage
    0043     for o = 1:norient
    0044         energy = allfiltered{i,o};
    0045         energy([1:2 end-1:end],:) = 0; energy(:,[1:2 end-1:end]) = 0;
    0046         localMeanMap = imfilter(energy,aveFilter,'replicate');
    0047         allfiltered{i,o} = sigmoidOnOriginalResponse(energy) .* energy .* mask ./ (localMeanMap + 1e-10);
    0048     end
    0049 end
    0050
    0051 %% sigmoid transform
    0052 function v = sigmoidOnOriginalResponse(v)
    0053 v = 1 ./ ( 1 + exp( 1 * -( v - 3 ) ) );