├── GaborArray.jpg ├── README.md ├── LICENSE.md ├── gaborFilterBank.m └── gaborFeatures.m /GaborArray.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mhaghighat/gabor/HEAD/GaborArray.jpg -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Gabor Feature Extraction 2 | 3 | The first function named "gaborFilterBank.m" generates a custom-sized Gabor filter bank. It creates a UxV cell array, whose elements are MxN matrices; each matrix being a 2-D Gabor filter. The second function named "gaborFeatures.m" extracts the Gabor features of an input image. It creates a column vector, consisting of the Gabor features of the input image. The feature vectors are normalized to zero mean and unit variance. At the end of each file there is a Show section that plots the filters and shows the filtered images. These are only for illustration purpose, and you can comment them as you wish. 4 | 5 | 6 | More details can be found in: 7 | 8 | M. Haghighat, S. Zonouz, M. Abdel-Mottaleb, "CloudID: Trustworthy cloud-based and cross-enterprise biometric identification," Expert Systems with Applications, vol. 42, no. 21, pp. 7905-7916, 2015. 9 | http://dx.doi.org/10.1016/j.eswa.2015.06.025 10 | 11 | 12 | (C) Mohammad Haghighat, University of Miami 13 | haghighat@ieee.org 14 | PLEASE CITE THE ABOVE PAPER IF YOU USE THIS CODE. 15 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | Copyright (c) 2015, Mohammad Haghighat All rights reserved. 2 | 3 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 4 | 5 | Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 6 | Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 7 | 8 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 9 | -------------------------------------------------------------------------------- /gaborFilterBank.m: -------------------------------------------------------------------------------- 1 | function gaborArray = gaborFilterBank(u,v,m,n) 2 | 3 | % GABORFILTERBANK generates a custum Gabor filter bank. 4 | % It creates a u by v cell array, whose elements are m by n matrices; 5 | % each matrix being a 2-D Gabor filter. 6 | % 7 | % 8 | % Inputs: 9 | % u : No. of scales (usually set to 5) 10 | % v : No. of orientations (usually set to 8) 11 | % m : No. of rows in a 2-D Gabor filter (an odd integer number, usually set to 39) 12 | % n : No. of columns in a 2-D Gabor filter (an odd integer number, usually set to 39) 13 | % 14 | % Output: 15 | % gaborArray: A u by v array, element of which are m by n 16 | % matries; each matrix being a 2-D Gabor filter 17 | % 18 | % 19 | % Sample use: 20 | % 21 | % gaborArray = gaborFilterBank(5,8,39,39); 22 | % 23 | % 24 | % 25 | % Details can be found in: 26 | % 27 | % M. Haghighat, S. Zonouz, M. Abdel-Mottaleb, "CloudID: Trustworthy 28 | % cloud-based and cross-enterprise biometric identification," 29 | % Expert Systems with Applications, vol. 42, no. 21, pp. 7905-7916, 2015. 30 | % 31 | % 32 | % 33 | % (C) Mohammad Haghighat, University of Miami 34 | % haghighat@ieee.org 35 | % PLEASE CITE THE ABOVE PAPER IF YOU USE THIS CODE. 36 | 37 | 38 | 39 | if (nargin ~= 4) % Check correct number of arguments 40 | error('There must be four input arguments (Number of scales and orientations and the 2-D size of the filter)!') 41 | end 42 | 43 | 44 | %% Create Gabor filters 45 | % Create u*v gabor filters each being an m by n matrix 46 | 47 | gaborArray = cell(u,v); 48 | fmax = 0.25; 49 | gama = sqrt(2); 50 | eta = sqrt(2); 51 | 52 | for i = 1:u 53 | 54 | fu = fmax/((sqrt(2))^(i-1)); 55 | alpha = fu/gama; 56 | beta = fu/eta; 57 | 58 | for j = 1:v 59 | tetav = ((j-1)/v)*pi; 60 | gFilter = zeros(m,n); 61 | 62 | for x = 1:m 63 | for y = 1:n 64 | xprime = (x-((m+1)/2))*cos(tetav)+(y-((n+1)/2))*sin(tetav); 65 | yprime = -(x-((m+1)/2))*sin(tetav)+(y-((n+1)/2))*cos(tetav); 66 | gFilter(x,y) = (fu^2/(pi*gama*eta))*exp(-((alpha^2)*(xprime^2)+(beta^2)*(yprime^2)))*exp(1i*2*pi*fu*xprime); 67 | end 68 | end 69 | gaborArray{i,j} = gFilter; 70 | 71 | end 72 | end 73 | 74 | 75 | %% Show Gabor filters (Please comment this section if not needed!) 76 | 77 | % Show magnitudes of Gabor filters: 78 | figure('NumberTitle','Off','Name','Magnitudes of Gabor filters'); 79 | for i = 1:u 80 | for j = 1:v 81 | subplot(u,v,(i-1)*v+j); 82 | imshow(abs(gaborArray{i,j}),[]); 83 | end 84 | end 85 | 86 | % Show real parts of Gabor filters: 87 | figure('NumberTitle','Off','Name','Real parts of Gabor filters'); 88 | for i = 1:u 89 | for j = 1:v 90 | subplot(u,v,(i-1)*v+j); 91 | imshow(real(gaborArray{i,j}),[]); 92 | end 93 | end 94 | -------------------------------------------------------------------------------- /gaborFeatures.m: -------------------------------------------------------------------------------- 1 | function featureVector = gaborFeatures(img,gaborArray,d1,d2) 2 | 3 | % GABORFEATURES extracts the Gabor features of an input image. 4 | % It creates a column vector, consisting of the Gabor features of the input 5 | % image. The feature vectors are normalized to zero mean and unit variance. 6 | % 7 | % 8 | % Inputs: 9 | % img : Matrix of the input image 10 | % gaborArray : Gabor filters bank created by the function gaborFilterBank 11 | % d1 : The factor of downsampling along rows. 12 | % d2 : The factor of downsampling along columns. 13 | % 14 | % Output: 15 | % featureVector : A column vector with length (m*n*u*v)/(d1*d2). 16 | % This vector is the Gabor feature vector of an 17 | % m by n image. u is the number of scales and 18 | % v is the number of orientations in 'gaborArray'. 19 | % 20 | % 21 | % Sample use: 22 | % 23 | % img = imread('cameraman.tif'); 24 | % gaborArray = gaborFilterBank(5,8,39,39); % Generates the Gabor filter bank 25 | % featureVector = gaborFeatures(img,gaborArray,4,4); % Extracts Gabor feature vector, 'featureVector', from the image, 'img'. 26 | % 27 | % 28 | % 29 | % Details can be found in: 30 | % 31 | % M. Haghighat, S. Zonouz, M. Abdel-Mottaleb, "CloudID: Trustworthy 32 | % cloud-based and cross-enterprise biometric identification," 33 | % Expert Systems with Applications, vol. 42, no. 21, pp. 7905-7916, 2015. 34 | % 35 | % 36 | % 37 | % (C) Mohammad Haghighat, University of Miami 38 | % haghighat@ieee.org 39 | % PLEASE CITE THE ABOVE PAPER IF YOU USE THIS CODE. 40 | 41 | 42 | if (nargin ~= 4) % Check correct number of arguments 43 | error('Please use the correct number of input arguments!') 44 | end 45 | 46 | if size(img,3) == 3 % Check if the input image is grayscale 47 | warning('The input RGB image is converted to grayscale!') 48 | img = rgb2gray(img); 49 | end 50 | 51 | img = double(img); 52 | 53 | 54 | %% Filter the image using the Gabor filter bank 55 | 56 | % Filter input image by each Gabor filter 57 | [u,v] = size(gaborArray); 58 | gaborResult = cell(u,v); 59 | for i = 1:u 60 | for j = 1:v 61 | gaborResult{i,j} = imfilter(img, gaborArray{i,j}); 62 | end 63 | end 64 | 65 | 66 | %% Create feature vector 67 | 68 | % Extract feature vector from input image 69 | featureVector = []; 70 | for i = 1:u 71 | for j = 1:v 72 | 73 | gaborAbs = abs(gaborResult{i,j}); 74 | gaborAbs = downsample(gaborAbs,d1); 75 | gaborAbs = downsample(gaborAbs.',d2); 76 | gaborAbs = gaborAbs(:); 77 | 78 | % Normalized to zero mean and unit variance. (if not applicable, please comment this line) 79 | gaborAbs = (gaborAbs-mean(gaborAbs))/std(gaborAbs,1); 80 | 81 | featureVector = [featureVector; gaborAbs]; 82 | 83 | end 84 | end 85 | 86 | 87 | %% Show filtered images (Please comment this section if not needed!) 88 | 89 | % % Show real parts of Gabor-filtered images 90 | % figure('NumberTitle','Off','Name','Real parts of Gabor filters'); 91 | % for i = 1:u 92 | % for j = 1:v 93 | % subplot(u,v,(i-1)*v+j) 94 | % imshow(real(gaborResult{i,j}),[]); 95 | % end 96 | % end 97 | % 98 | % % Show magnitudes of Gabor-filtered images 99 | % figure('NumberTitle','Off','Name','Magnitudes of Gabor filters'); 100 | % for i = 1:u 101 | % for j = 1:v 102 | % subplot(u,v,(i-1)*v+j) 103 | % imshow(abs(gaborResult{i,j}),[]); 104 | % end 105 | % end 106 | --------------------------------------------------------------------------------