function [filtIm] = bandpass2D(Image, largerPixel, smallerPixel) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %A basic implementation of a 2-D bandpass filter as a first pass for %processing gi ca videos. Made from 2 butterworth filters % Inputs: % - Image: The image to be filtered % - largerPixel: The upper limit of features to be filtered in pixels % - smallerPixel: the lower limit of the features to be filtered in % pixels % Output: % - filtIm: the filtered image %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% norder = 2; %% Find the next power of 2 from the dimensions of the original image. [nx ny] = size(Image); np2nx = nextpow2(nx); np2ny = nextpow2(ny); sqdim = max([np2nx np2ny]); % Set up the padding for the image so that the total dimensions of the % figure are the next closest power of two to the original image padx = floor((2^sqdim - nx)/2); pady = floor((2^sqdim - ny)/2); padImage = padarray(Image, [padx pady], 'symmetric'); [px py] = size(padImage); %% Check that the dimensions are even: % if not add an extra row/column of zeros and save for the conversion back if iseven(px) else padImage = cat(1, zeros(1, py), padImage); padx = padx +1; end [px py] = size(padImage); if iseven(py) else padImage = cat(2, zeros(px, 1), padImage); pady = pady+1; end % Perform the fft on the padded image fftI = fft2(padImage); fftI = fftshift(fftI); %% Generate bandpass filter parameters % Calculate the frequency value for the pixels of the fft image [f1,f2] = freqspace(size(fftI), 'meshgrid'); dist = sqrt(f1.^2 + f2.^2); wU = (1/smallerPixel)*2; wL = (1/largerPixel)*2; % Generate the low pass and high pass Butterworth filters filterLP = 1./(1 + (dist./wL).^(2*norder)); filterLP = 1-filterLP; filterHP = 1./(1 + (dist./wU).^(2*norder)); %% Generate the bandpass filter and apply it filterBP = filterLP.*filterHP; % Filter and calculate the inverse fourier transform filtIm = fftI.*filterBP; filtIm = ifftshift(filtIm); filtIm = ifft2(filtIm); filtIm = real(filtIm)+mean(Image(:)); % undo the padding filtIm = filtIm(padx+1:padx+nx, pady+1:pady+ny); end