MATLAB Feature Extraction experiment process

Source: Internet
Author: User
Tags clear screen in degrees

First, standardize the images in the face database and cut out the face area based on the two eyes, as shown in:

The MATLAB program is as follows:

% Function Description: The face size normalization manual click to automatically cut the image as the specified size function unitery (height, width, m) % wdith is the width of the cut image. Height is the length of the cut image. M indicates the number of images. Clear; close all; Height = 490; % set image size width = 640; M = 12; % set numbers of image % set path of image mydir = 'H: \ CMU emojis library \ Cohn-kanade \ s010 \ 001 \ '; % set suffix dirs = Dir ([mydir ,'*. PNG ']); for I = 1: m STR = [mydir, dirs (I ). name]; EVAL ('oriimg = imread (STR); '); % execute the string to read IMG % into the image figure, imshow (oriimg), [x, y] = ginput (2); % manually obtain the human eye E1, E2 coordinate d = x ()-X ); % calculate the width between two eyes. d ox = sum (X)/2; Oy = sum (y)/2; % calculate the center O (OX, Oy) of E1 and E2) i1 = imcdrop (oriimg, [ox-0.9 * D Oy-0.5 * D 1.8 * D 2 * D]); % cut face STR = strcat ('H: \ CMU emoticons library \ Cohn-kanade \ s010 \ 001 \ Standard \ ',int2str( I }'.bmp '); EVAL ('imwrite (I1, STR );'); % execute a string to read IMG % into image % to save the normalized Face Image close all; End

 

After standardization, the task is to partition up and down, and cut off evenly.

Shape:

The MATLAB program is as follows:

function ShangXiaFenKuai    mydir = 'C:\Users\user\Desktop\Database\CMU\test_test\';    DIRS = dir([mydir,'*bmp']);    for i=1:18    if ~DIRS(i).isdir        filename = [mydir,DIRS(i).name];        disp(filename)        I = imread(filename);        ImageUp = I(1:50,:);        ImageDown = I(51:100,:);        savefileUp = [mydir,'Up\',DIRS(i).name];        imwrite(ImageUp,savefileUp);        savefileDown = [mydir,'Down\',DIRS(i).name];        imwrite(ImageDown,savefileDown);    end    i = i + 1;    endend

Use functions in MATLAB to extract Gabor Wavelet features

Two M files are involved here.

Main. m

Mydir = 'C: \ Users \ User \ Desktop \ data \ '; for I = for J = dir = running mydir,num2str( I #,'-', num2str(j#,'.bmp']; Im = imread (DIR ); % or = rgb2gray (IM); Gim = im2double (IM); disp (DIR); [EIM, OIM, aim] = spatialgabor (Gim, 4.8, 150, 0.4, 0.2, 1); SaveFile = [mydir, 'gabor \ ', num2str(ifolder, '-', num2str(jfolder, '.bmp']; imwrite (aim, SaveFile ); test of endend % Gabor parameter % % Ori = imread ('d: \*. JPG '); OR = rgb2gray (ORI) Gim = im2single (OR); for I = for J = 30: 20: 150 for X = 0.1: 0.1: 1 For Y = 0.1: 0.1: 1 [EIM, OIM, aim] = spatialgabor (Gim, I, j, X, Y, 1); SaveFile = ['d: \ Ba \ alipay'] imwrite (aim, SaveFile ); end end % %

Spatialgabor. m

% SPATIALGABOR - applies single oriented gabor filter to an image%% Usage:%  [Eim, Oim, Aim] =  spatialgabor(im, wavelength, angle, kx, ky, showfilter)%% Arguments:%         im         - Image to be processed.%         wavelength - Wavelength in pixels of Gabor filter to construct%         angle      - Angle of filter in degrees.  An angle of 0 gives a%                      filter that responds to vertical features.%         kx, ky     - Scale factors specifying the filter sigma relative%                      to the wavelength of the filter.  This is done so%                      that the shapes of the filters are invariant to the%                      scale.  kx controls the sigma in the x direction%                      which is along the filter, and hence controls the%                      bandwidth of the filter.  ky controls the sigma%                      across the filter and hence controls the%                      orientational selectivity of the filter. A value of%                      0.5 for both kx and ky is a good starting point.%         showfilter - An optional flag 0/1.  When set an image of the%                      even filter is displayed for inspection.% % Returns:%         Eim - Result from filtering with the even (cosine) Gabor filter%         Oim - Result from filtering with the odd (sine) Gabor filter%         Aim - Amplitude image = sqrt(Eim.^2 + Oim.^2)%% Peter Kovesi  % School of Computer Science & Software Engineering% The University of Western Australia% pk at csse uwa edu au% http://www.csse.uwa.edu.au/~pk%% October 2006function [Eim, Oim, Aim] = spatialgabor(im, wavelength, angle,kx,ky, showfilter)    if nargin == 5        showfilter = 0;    end    im = double(im);    [rows, cols] = size(im);    newim = zeros(rows,cols);    % Construct even and odd Gabor filters    sigmax = wavelength*kx;    sigmay = wavelength*ky;        sze = round(3*max(sigmax,sigmay));    [x,y] = meshgrid(-sze:sze);    evenFilter = exp(-(x.^2/sigmax^2 + y.^2/sigmay^2)/2)...    .*cos(2*pi*(1/wavelength)*x);        oddFilter = exp(-(x.^2/sigmax^2 + y.^2/sigmay^2)/2)...    .*sin(2*pi*(1/wavelength)*x);        evenFilter = imrotate(evenFilter, angle, 'bilinear');    oddFilter = imrotate(oddFilter, angle, 'bilinear');        % Do the filtering    Eim = filter2(evenFilter,im); % Even filter result    Oim = filter2(oddFilter,im);  % Odd filter result    Aim = sqrt(Eim.^2 + Oim.^2);  % Amplitude     %if showfilter % Display filter for inspection        %figure(1), imshow(evenFilter,[]); title('filter');     end    

After the Gabor feature map is obtained, the dimension is reduced. Here, PCA and DCT are used for dimensionality reduction.

PCA. m

Function exepca1 () CLC; % clear screen % % 18 A large matrix % % % dirtest = 'd: \ train \ test-6-3 \ Gabor \ '; dirs_test = Dir ([dirtest ,'*. BMP ']); for I = If ~ Dirs_test (I ). isdir % to be loaded and processed. Here you can get string_test = [dirtest, dirs_test (I ). name]; disp (string_test); % read image image_src_test = imread (string_test); % The following zscore function is processed by row, think of the row vector as a sample % convert the image matrix into a column vector % first transpose the image, then expand by column, and then get the row vector in the transpose !! I1 = image_src_test '; vector_test = I1 (:); image_vector_test = vector_test'; % image_vector = image_src (:); % form a new large matrix if I = 1 image_matrix_test = image_vector_test; else image_matrix_test = [image_matrix_test; image_vector_test]; end image_double_test = im2single (image_matrix_test ); % % merge a large matrix % % % Mydir = 'd: \ train-6-5 \ Gabor-90 \ 'dirs = Dir ([mydir ,'*. BMP ']); for I = If ~ Dirs (I ). isdir % is to be loaded and processed. Here you can get string = [mydir, dirs (I ). name]; disp (string); % read image image_src = imread (string); % The following zscore function is processed by row, think of the row vector as a sample % convert the image matrix into a column vector % first transpose the image, then expand by column, and then get the row vector in the transpose !! I1 = image_src '; vector = I1 (:); image_vector = vector'; % image_vector = image_src (:); % form a new large matrix if I = 1 image_matrix = image_vector; flag = 2; else image_matrix = [image_matrix; image_vector]; end image_double = im2single (image_matrix ); % % xz = zscore (image_double); % evaluate the 'feature value ', 'difference', 'contribution rate ', and 'accumulation contribution rate'. The base of the main requirement is a set of feature values [coeff, score, late NT] = princomp (xz); % [coeff, latent] = pcacov (COV (xz); explained = 100 * latent/sum (latent); [m, n] = size (image_double); Result = cell (n + 1, 4); Result (1, :) = {'feature value ', 'difference', 'contribution rate ', 'accumulative contribution rate '}; Result (2: end, 1) = num2cell (latent); Result (2: End-1, 2) = num2cell (-diff (latent )); result (2: end, 3: 4) = num2cell ([explained, cumsum (explained)]); % % This sentence is restored !!!! Pca_train = image_double * coeff; % the first 100 elements after restoration are taken as the representative !!! Pca400_train = pca_train (:,); filename = require mydir,'imagepca_train.txt ']; disp (filename); dlmwrite (filename, pca400_train ); % % pca_test = image_double_test * coeff; pca400_test = pca_test (:,); filename_test = nvidirtest,'imagepca_test.txt ']; disp (filename_test); dlmwrite (filename_test, pca400_test, '',); End

 

DCT. m

 

% Evaluate the result of DCT transformation retain the first 300 dimension function dctclc; flag = 1; filename = 'C: \ Users \ User \ Desktop \ database \ Jaffe \ train \ 8 \ up \ '; featurepath = 'C: \ Users \ User \ Desktop \ mydoc \ expressiondata \ Jaffe \ DCT \ TXT \ DCT-train-Up.txt '; for I = for J = mydir = Invalid filename,num2str( I .pdf, '-', num2str(j),'.jpg']; I = imread (mydir); disp (mydir); % I = I '; A = dct2 (I); % imwrite (,''); B = zigzag2dto1d (a); if flag = 1 matrix = B (); flag = 2; else matrix = [matrix; B ()]; end end dlmwrite (featurepath, matrix, '',); % disp (matrix); End

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.