| function [gist, param] = LMgist(D, HOMEIMAGES, param, HOMEGIST) |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| if nargin==4 |
| precomputed = 1; |
| |
| |
|
|
| |
| |
| |
| |
| else |
| precomputed = 0; |
| HOMEGIST = ''; |
| end |
|
|
| |
| param.boundaryExtension = 32; |
|
|
| if nargin<3 |
| |
| param.imageSize = 128; |
| param.orientationsPerScale = [8 8 8 8]; |
| param.numberBlocks = 4; |
| param.fc_prefilt = 4; |
| param.G = createGabor(param.orientationsPerScale, param.imageSize1+2*param.boundaryExtension); |
| else |
| if ~isfield(param, 'G') |
| param.G = createGabor(param.orientationsPerScale, param.imageSize+2*param.boundaryExtension); |
| end |
| end |
|
|
| |
| |
| Nfeatures = size(param.G,3)*param.numberBlocks^2; |
|
|
| if isstruct(D) |
| |
| Nscenes = length(D); |
| typeD = 1; |
| end |
| if iscell(D) |
| |
| Nscenes = length(D); |
| typeD = 2; |
| end |
| if isnumeric(D) |
| |
| Nscenes = size(D,4); |
| typeD = 3; |
| end |
|
|
| |
| gist = zeros([Nscenes Nfeatures], 'single'); |
| for n = 1:Nscenes |
| g = []; |
| todo = 1; |
| |
| |
| if precomputed==1 |
| filegist = fullfile(HOMEGIST, D(n).annotation.folder, [D(n).annotation.filename(1:end-4) '.mat']); |
| if exist(filegist, 'file') |
| load(filegist, 'g'); |
| todo = 0; |
| end |
| end |
| |
| |
| if todo==1 |
| disp([n Nscenes]) |
|
|
| |
| try |
| switch typeD |
| case 1 |
| img = LMimread(D, n, HOMEIMAGES); |
| case 2 |
| img = imread(fullfile(HOMEIMAGES, D{n})); |
| case 3 |
| img = D(:,:,:,n); |
| end |
| catch |
| disp(D(n).annotation.folder) |
| disp(D(n).annotation.filename) |
| rethrow(lasterror) |
| end |
| |
| img = single(img); |
| if(size(img,3) > 1) |
| img = rgb2gray(img); |
| end |
|
|
| |
| |
| img = imresize(img, param.imageSize, 'bilinear'); |
|
|
| |
| img = img-min(img(:)); |
| img = 255*img/max(img(:)); |
| |
| if Nscenes>1 |
| imshow(uint8(img)) |
| title(n) |
| end |
|
|
| |
| |
| output = img; |
|
|
| |
| g = gistGabor(output, param); |
| |
| |
| if precomputed |
| mkdir(fullfile(HOMEGIST, D(n).annotation.folder)) |
| save (filegist, 'g') |
| end |
| end |
| |
| gist(n,:) = g; |
| drawnow |
| end |
|
|
| function output = prefilt(img, fc) |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| if nargin == 1 |
| fc = 4; |
| end |
|
|
| w = 5; |
| s1 = fc/sqrt(log(2)); |
|
|
| |
| img = log(img+1); |
| img = padarray(img, [w w], 'symmetric'); |
| [sn, sm, c, N] = size(img); |
| n = max([sn sm]); |
| n = n + mod(n,2); |
| img = padarray(img, [n-sn n-sm], 'symmetric','post'); |
|
|
| |
| [fx, fy] = meshgrid(-n/2:n/2-1); |
| gf = fftshift(exp(-(fx.^2+fy.^2)/(s1^2))); |
| gf = repmat(gf, [1 1 c N]); |
|
|
| |
| output = img - real(ifft2(fft2(img).*gf)); |
| clear img |
|
|
| |
| localstd = repmat(sqrt(abs(ifft2(fft2(mean(output,3).^2).*gf(:,:,1,:)))), [1 1 c 1]); |
| output = output./(.2+localstd); |
|
|
| |
| output = output(w+1:sn-w, w+1:sm-w,:,:); |
|
|
|
|
|
|
| function g = gistGabor(img, param) |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| img = single(img); |
|
|
| w = param.numberBlocks; |
| G = param.G; |
| be = param.boundaryExtension; |
|
|
| if ndims(img)==2 |
| c = 1; |
| N = 1; |
| [nrows ncols c] = size(img); |
| end |
| if ndims(img)==3 |
| [nrows ncols c] = size(img); |
| N = c; |
| end |
| if ndims(img)==4 |
| [nrows ncols c N] = size(img); |
| img = reshape(img, [nrows ncols c*N]); |
| N = c*N; |
| end |
|
|
| [ny nx Nfilters] = size(G); |
| W = w*w; |
| g = zeros([W*Nfilters N]); |
|
|
| |
| img = padarray(img, [be be], 'symmetric'); |
|
|
| img = single(fft2(img)); |
| k=0; |
| for n = 1:Nfilters |
| ig = abs(ifft2(img.*repmat(G(:,:,n), [1 1 N]))); |
| ig = ig(be+1:ny-be, be+1:nx-be, :); |
| |
| v = downN(ig, w); |
| g(k+1:k+W,:) = reshape(v, [W N]); |
| k = k + W; |
| drawnow |
| end |
|
|
| if c == 3 |
| |
| |
| g = reshape(g, [size(g,1)*3 size(g,2)/3]); |
| end |
|
|
|
|
| function y=downN(x, N) |
| |
| |
| |
| |
| |
| |
| |
|
|
| nx = fix(linspace(0,size(x,1),N+1)); |
| ny = fix(linspace(0,size(x,2),N+1)); |
| y = zeros(N, N, size(x,3)); |
| for xx=1:N |
| for yy=1:N |
| v=mean(mean(x(nx(xx)+1:nx(xx+1), ny(yy)+1:ny(yy+1),:),1),2); |
| y(xx,yy,:)=v(:); |
| end |
| end |
|
|
|
|