0% found this document useful (0 votes)
240 views5 pages

Image Feature Extraction Based On PCA

This document contains the source code for performing two-dimensional principal component analysis (2DPCA) in MATLAB. It includes functions to calculate the mean of the training data, subtract it from all data points, calculate the 2DPCA covariance matrix, compute its eigenvectors and eigenvalues, project the training and test data into the reduced dimensional feature space, and return the projection matrices and transformed data. The code takes in training and test datasets along with image height, width, and number of eigenvectors to use.

Uploaded by

Sanjana Kuril
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
240 views5 pages

Image Feature Extraction Based On PCA

This document contains the source code for performing two-dimensional principal component analysis (2DPCA) in MATLAB. It includes functions to calculate the mean of the training data, subtract it from all data points, calculate the 2DPCA covariance matrix, compute its eigenvectors and eigenvalues, project the training and test data into the reduced dimensional feature space, and return the projection matrices and transformed data. The code takes in training and test datasets along with image height, width, and number of eigenvectors to use.

Uploaded by

Sanjana Kuril
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

TDPCA.m in TDPCA.

rar

function [eigvectors, eigvalues, meanData, newTrainData, newTestData] = TDPCA(trainData, testData, height, width, numvecs) %2DPCA Two Dimensional Principal component analysis % % % % % % % % % % % % % % % % % % % % % % Reference paper: J.Yang,D.Zhang,A.F.Frangi,and J.Yang.Two-dimensional pca:A new approach to a appearance-based face represenation and recognition. IEEE Trans.on PAMI,2004 Written by Zhonghua Shen ([email protected]), 2006.07 meanData: Mean of all the data. newTrainData: The data after projection (mean removed) newTestData: The data after projection (mean removed) eigvectors: Each column of this matrix is a eigenvector of the matrix defined in 2DPCA eigvalues: Eigenvalues of the convariance matrix trainData: Rows of vectors of training data points testData: Rows of vectors of testing data points height: height of the image matrix width: width of the image matrix numvecs: the needed number of eigenvectors Usage: [eigvectors, eigvalues, meanData, newTrainData, newTestData] =

TDPCA(trainData, testData, height, width, numvecs)

convariance

% Check arguments if nargin ~= 5 error('usage: [eigvectors, eigvalues, meanData, newTrainData, newTestData] = TDPCA(trainData, testData, height, width, numvecs)'); end; [nSam,nFea] = size(trainData); fprintf(1,'Computing average matrix...\n'); meanDataVector = mean(trainData); meanData = reshape(meanDataVector,height,width); fprintf(1,'Calculating matrix differences from avg and 2DPCA covariance matrix L...\n');

TDPCA.m in TDPCA.rar

L = zeros(width,width); ddata = zeros(nSam,nFea); for i = 1:nSam ddata(i,:) = trainData(i,:)-meanDataVector; dummyMat = reshape(ddata(i,:),height,width); L = L + dummyMat'*dummyMat; end; L = L/nSam; L = (L + L')/2;

fprintf(1,'Calculating eigenvectors of L...\n'); [eigvectors,eigvalues] = eig(L); fprintf(1,'Sorting eigenvectors according to eigenvalues...\n'); [eigvectors,eigvalues] = sortem(eigvectors,eigvalues); eigvalues = diag(eigvalues); fprintf(1,'Normalize Vectors to unit length, kill vectors corr. to tiny evalues...\n'); num_good = 0; for i = 1:size(eigvectors,2) eigvectors(:,i) = eigvectors(:,i)/norm(eigvectors(:,i)); if eigvalues(i) < 0.00001 % Set the vector to the 0 vector; set the value to 0. eigvalues(i) = 0; eigvectors(:,i) = zeros(size(eigvectors,1),1); else num_good = num_good + 1; end; end; if (numvecs > num_good) fprintf(1,'Warning: numvecs is %d; only %d exist.\n',numvecs,num_good); numvecs = num_good; end; eigvectors = eigvectors(:,1:numvecs); if nargout == 5 fprintf(1,'Feature extraction and calculating new training and testing data...\n'); newTrainData = zeros(nSam,height*numvecs); for i = 1:nSam

TDPCA.m in TDPCA.rar

dummyMat = reshape(ddata(i,:),height,width); newTrainData(i,:) = reshape(dummyMat*eigvectors,1,height*numvecs); end %testData nt = size(testData,1); newTestData = zeros(nt,height*numvecs); tdata = zeros(size(testData)); for i = 1:nt tdata(i,:) = testData(i,:)-meanDataVector; dummyMat = reshape(tdata(i,:),height,width); newTestData(i,:) = reshape(dummyMat*eigvectors,1,height*numvecs); end; end;

Image Feature Extraction based on PCA..


function [XP,W,eigv,sumeigv]=pca(X,m) %input: % X [d * n] after truncation hyperspectral data % M [1 * 1] After the dimensionality reduction of data dimension %output: % W [d * m] transformation matrix % Eigv [1 * m] input data covariance matrix eigenvalues (from the large inverted small order) % XP [m * n] transformed principal component weight %load X.mat; K=cov(X'); [U, D] = eig (K); [Eigv, index] = sort (diag (D), 'descend'); U = U (:, index); % compute eigenvectors of cov(X) %m=6; if m> length (eigv) disp('Desired data dimension is too big.'); V = U; else V = []; for i=1:m eigv if (i)> 0 V = [V, U (:, i) / sqrt (eigv (i))]; end end end eigv eigv = (1: m); sumeigv = sum (eigv (m +1: end)); W = V / norm (V); XP=W'*X; return; ...

TDPCA.m in TDPCA.rar

pca source for feature extraction using Matlab language, pca that Principal Comp

% A revised PCA for face recognition in Matlab code, in fact, I feel that feature extraction and classification using the coordinates of the feature space % Calc xmean, sigma and its eigen decomposition allsamples =[];% all training images for i = 1:40 for j = 1:5 a = imread (strcat ('D: \ rawdata \ ORL \ s', num2str (i), '\', num2str (j), '. pgm')); % Imshow (a); b = a (1:112 * 92);% b is the row vector 1 N, in which N = 10304, extraction order is then over, that is from top to bottom, left to right b = double (b); allsamples = [allsamples; b];% allsamples is a M * N matrix, allsamples representative of each row of data in a picture, in which M = 200 end end samplemean = mean (allsamples);% average picture, 1 N for i = 1:200 xmean (i,:) = allsamples (i,:)-samplemean;% xmean is an M N matrix, xmean save each line of data is "data for each image - an average picture." end; sigma = xmean * xmean ';% M * M-order matrix [Vd] = eig (sigma); d1 = diag (d); [D2 index] = sort (d1);% in ascending order cols = size (v, 2);% eigenvector matrix of columns for i = 1: cols vsort (:, i) = v (:, index (cols-i +1));% vsort is a M * col (Note: col generally equal to M)-order matrix, is stored in descending order of feature vectors, form a feature vector for each column dsort (i) = d1 (index (cols-i +1));% dsort is stored in descending order of eigenvalues, is a one-dimensional row vector end% complete in descending order % Of 90% of the energy options dsum = sum (dsort); dsum_extract = 0; p = 0; while (dsum_extract / dsum <0.9) p = p + 1; dsum_extract = sum (dsort (1: p)); end i = 1; % (Training phase) calculated the formation of the face coordinate system feature while (i <= p & & dsort (i)> 0) base (:, i) = dsort (i) ^ (-1 / 2) * xmean '* vsort (:, i);% base is the N p order matrix, divided by dsort (i) ^ (1 / 2) is a face image of standardization, see "PCA-based face recognition algorithms" p31 i = i + 1;

TDPCA.m in TDPCA.rar

end % Add by wolfsky is the following two lines of code, the training samples on the projection of the coordinate system, to get a M * p-order matrix allcoor allcoor = allsamples * base; accu = 0; % Test for i = 1:40 for j = 6:10% Read 40 x 5 test images a = imread (strcat ('D: \ rawdata \ ORL \ s', num2str (i), '\', num2str (j), '. pgm')); b = a (1:10304); b = double (b); tcoor = b * base;% calculate the coordinates, is 1 p order matrix for k = 1:200 mdist (k) = norm (tcoor-allcoor (k ,:)); end; % Third-order neighbors [Dist, index2] = sort (mdist); class1 = floor (index2 (1) / 5) +1; class2 = floor (index2 (2) / 5) +1; class3 = floor (index2 (3) / 5) +1; if class1 ~ = class2 & & class2 ~ = class3 class = class1; elseif class1 == class2 class = class1; elseif class2 == class3 class = class2; end; if class == i accu = accu +1; end; end; end; accuracy = accu/200% recognition rate of output

You might also like