## Principal Component Analysis (Matlab Code)

### function [evec, eval, meanA] = PCA(A, maxVec)

%Principal Component Analysis

%A has th

[ rows, cols] = size(A);

if(nargin < 2)

maxVec = rows;

end

maxVec = min(maxVec, rows);

meanA = mean(A,2);

A = A – meanA*ones(1, cols);

%[evec eval] = eig(A’*A);

[evec eval] = eig(A*A’);

[eval ind] = sort(-1*diag(eval));

%[eval ind] = sort(diag(eval), ”);

eval = -1*eval(1:maxVec);

evec = evec(:, ind(1:maxVec));

### function num = findTopPCAvec(eval, percent)

%num is number of evals whose sum makes the percent*(total sum of the

%eval).

%eval: eigen values; sorted in descending order

%percent: percent for which you want to find num

totalV = length(eval);

totalSum = sum(eval);

for num=1:totalV

if( round(100*sum(eval(1:num)))/totalSum >= percent)

return;

end

end

### function [coffs] = calcPcaCoff( evec, data, meanVec)

%calculates the cofficients of the data

% Evec; each eigen vector is in the col

% data: each feature vector is col vector

% meanVec by default is zeros

%Returns

% coffs: each cofficient is

[ rows, cols] = size(data);

if(nargin < 3)

meanVec = zeros(rows,1);

end

coffs = evec’*(data – repmat(meanVec, 1, cols));

Please have a look at following link. It should help understand the basiscs of how to use the information got from using PCA.

http://www.cs.washington.edu/education/courses/cse576/09sp/projects/project3/artifact/tingyou/proj3/index.html

i need the exact comment in matlab to get the output from the third layer where the network contains five layers

in PCA? You are talking about the Neural Network?

i know that but what i need smebdy’s help.if that is not possible can you the syntax of pca for signal compression

Kartic I have never worked on the Signal Compression, but If you can represent signal as the vectors then it will be simply having matrix of singals and using the PCA code.

Please tell me what you actually want to do. Post here what problem you are working on, I will be glad if I could be of any help

sir, i have to reduce the dimension of my inputs,inorder to reduce it am using pca.so could u plz help me in sending out the function which implements PCA.

{Please stop using word sir, it makes me feel responsible and old }

The above code does take the PCA of matrix A, Where A’s columns are the data-vectors i.e. [x1, x2, x3,,…..xn]

Make this matrix A send it to the PCA function given above, it will send you back the eigen vectors and values.

Chose only top 50 percent, lets say this number is p

Now to calculate smaller matrix to represent A,

project each data-vector in A to the eigen-vectors you have calculated in this way for each xi you will have p coefficients.

this will give you p by n matrix instead of n by n matrix

{ you can use the third fuction (calcPcaCoff) given in the above post for calculating the cofficients}

Please note that you should consult some Linear Algebra book to really understand benefits of the eigen vectors and values. It will be beneficial to you for long time.

THanks

I hope it helps you out.

Bye

function [coffs] = calcPcaCoff( evec, data, meanVec)

in the above fun what is the value of data and meanvec

In your case

data is the matrix on which you have calculated the eigen vectors.

meanVec is the mean of the data, which is also returned by the first function.

have some doubts regarding the implementation

1.could u plz explain me the implementation of all the three function( sorry just now going through the basics of matlab)

2.say if my input data is s(large data set) then can i directly apply this to first function or i should perform filtering before applying to it.

3.already i have applied my input to the first function then what is the need of applying once again to third function (data).

4.whether meanA and meanvec are same

Kartic, you should go through the PCA from the Pattern Recognition by Duda and Hart.

Ok, first function is simple calculating the eigen-vectors and eigen values of inputted data. {I dont know what you mean by filtering of data if it is large.}

Second function is used in the case you want to select few top vectors based on their total eigen-values. Note that the eigen values tell us the importance of the eigen-vector.

Third Function is not doing the PCA it is projecting the data on to the eigen-vectors

Now: to really understand what 3rd function is doing you need to know why we do the PCA, what eigen-vectors of data represent.

For which you should see some literature.

eigen-vecotrs of Data: represent the orthogonal directions in which the data is spread, e.g. if our data when plotted is kind of ellipse, then the 2 eigen vectors we will get from PCA would be major and minor axis of such ellipse.

By projecting data on the eigen vectors you are trying to find the coordinates of the data in the eigen-vector space.

e.g. [2,3] generally means go 2 unit in x direction and 3 unit in y-direction. similarly after projections the coefficients we get tells where in the eigen-vector space that data-point lies.

4) yes meanA and meanvec is same and is the mean of the data.

A=0.0918 -0.1479 0.0990

0.0990 -0.1479 0.0918

if A is my input can u get me the coefficients after performing pca to it

Kartic please refer some books and lectures about linear algebra, eigen analysis and PCA

as for you question

1) use first function to get eigen vectors and values

2) use third function to find the coefficients.

this is kartic once again my doubt is each signal of my database contains 4974 data . for example am using only one recording say 4974 whether it is possible for the above pca program to reduce it to six coefficients since my input layer of neural networks takes six values

sir, i need the pca syntax to reduce that matrix which is too large

sir if possible send your phone number to my mail-id because am new to matlab i have so many doubts

function [coffs] = calcPcaCoff( evec, data, meanVec)

in the above function what values should be given to data and meanvec

i have some doubts regarding the implementation

1.could u plz explain me the implementation of all the three function( sorry just now going through the basics of matlab)

2.say if my input data is s(large data set) then can i directly apply this to first function or i should perform filtering before applying to it.

3.already i have applied my input to the first function then what is the need of applying once again to third function (data).

4.whether meanA and meanvec are same

hi….your post is so interesting!!!! I´m working with a 14209 x 2048 matrix and I´m trying to execute a command to extract the principal components, that command is princomp, the problem is when I introduce the data (princomp(matrix)), it shows this error:

??? Error using ==> svd

Input to SVD must not contain NaN or Inf.

Error in ==> princomp at 69

[U,sigma,coeff] = svd(x0,econFlag); % put in 1/sqrt(n-1) later

do you ave any idea about it??? i really need some help….I appreciate your help..thanks and keep making our lives easier :)

From “nput to SVD must not contain NaN or Inf” the input matrix has an element marked as infinity.

You can check if some element is becoming NaN or Inf by using isnan http://www.mathworks.com/access/helpdesk/help/techdoc/ref/isnan.html

If this is happening then check if by mistake the data is getting devided by zero.

If you cannot remove such devision, replace these InF and NaN by very large number, but this not a right way to handle such input.

nice post…

congratulations !!!

Well it’s simply not my code. You can find this code at many places. I just found my self re-writing PCA code everytime I needed something to be done. So I just put it there. Sort for plug and play purposes. :)

I would like to trace the indices of each principal component in the original data matrix. how would i know which indices of the original data matrix is taken as principal component. is it possible to trace and how?

would you please reply a solution.

The eigen vectors might not be existent as a indices of original data. PCA gives you directions where the most of spread of data is, e.g. if you have data whose spread is like egg or ellipse, then the major and minor axis will be Principal components, and we know that the major and minor axis are not always parallel to x and y axis.

Please have a look at http://en.wikipedia.org/wiki/Principal_component_analysis

Do tell if I have not understood your question correctly.

Thanks

sir i need matlab code for image fusion using PCA….

sorry Prashant, I don’t think I have coded it.

hi,

i m working on licemse plate skew detection. i m using pca to find out vertical and horizontal tilt angle…is this prg. working on that…and pls define the term maxVec…i m working on image intensity matrix…pls give me some suggestion…thanks in advance..

You want to use the Eigen Vectors to find the direction of spread?

Try calculating the second order moments and finding their eigen vectors and eigen values. (For that you will not need above code. Simply use eigs or eig in the Matlab.)

Do tell did it helped or not? thanks

if i have 100 training data set pedestrian, each size 32×64 pixel. file names pe00001.pgm – pe00100.pgm

how code deploy pca in matlab??

each image will become a vector.

put all these vectors in the matrix and calculate their PCA.

That’s generally how one does PCA of images. I don’t know about what problem you are trying to solve, if you can tell more, I might be able better help in this regard.

Thanks

Mohsen Ali

Dear Mohsenan

Assalamu alaykum.

I am going to ask some questions about PCA,of course if you will let me.

I have done some sensor module. And this module supports me with a binary data.I wanted to extract features from my data by using PCA. First, I have used only mean and variance as a feature extraction. But when i used NNet my result was not good enough to me. So,now I want to use whole PCA for my data. Will it help me? I mean can i get better features for my experiment. (I am a new user in Matlab)

Thanks in advance!

Fahriddin

Fhariddin,

By using PCA you can do the Dimension Reduction. For example you can use the coefficients obtained by projecting the data on the eigen vectors as your features. The number of eigen vectors can be varied, mostly they are selected on the basis of their eigen value.It will give you more features than what are you using right now.

sorry Fahriddin, I misspelled your name

Hey Mohsenan I want to apply PCA for image change detection algorithm..can help me..i didnt get anything..plz copy the code over here..i want to check the difference or change between two images which are similar with a slight change..plz reply..thanks

How you want to use the PCA for Image Change Detection? Are you trying to do the Background Subtraction? or you are trying to do the object comparison?

Are you doing some kind of training?

I am working on change detection algorithm. i am taking two images which are same but with a slight difference.u can take the image of bandra worli sealink.The image before the sealink and the image after the sealink.and by applying PCA on those two image i should get the difference as the bridge in the output.

ok can u just tell me the code u have written how to execute it..what is the procedure.i am not getting how to use each function.am i suppose to copy those 3function in 3 different editor and then run it..what input i should take for each n every variable.??plz tell me

plzz tell me thanks

hi every one! I want to know that can we scan vehicle # form image thorugh image processing using Matlab?

Also how to compare two images using eigenvalues?

You don’t compare eigen values, but compare coefficients of each image (which we get by projecting each image on the set of eigen vectors).

Hi,

Reply plz,

Can we seperate the Vehicle # form picture thurough Image processing in matlab?

if so then plz send code if you have!!!

Yes, we can but I don’t have code, you will need a code that identifies the location of vehicle #. Rectification of the located rectangle, separation of each digit and recognition.

hi am doing PG project in natural visible database(NVIE database).so i need coding for that in PCA & AAM.can u help me..

can you please further explain your comment? Thanks

Hi Moshenan,

In the first function the input maxVector is the size of the vector of my data?

actually it should represent maximum eigen vectors you want to get, so min of rows or cols.

hi,

The o/p of eig function and the manual result of eigen vectors are not matching. I mean,

values are same but signs are not matching.This is having impact on coff of pca.I tried with two example but coff are different.

Thank you

i want to explain my doubt clearly with example.if the mtrx is 2*2 ie.[1 2;4 3]{matlab representation} .

matlab o/p:

[-0.7071 -0.4472;0.7071 -0.8944] =>eigen vectors

eigen values are -1 and 5.

manual o/p

for eigen value -1 eig space is[-1 1].it satisfies matlab output ie.,0.7071(-1) 0.7071(1)

for eigen value 5 eig space is[1/2 1].it satisfies the value but not the signs ie.,0.8944(1/2) 0.8944(1).ok! the real num is -0.8944.

In this example ok!(i took this example from http://www.khanacademy.org/video/linear-algebra—finding-eigenvectors-and-eigenspaces-example?playlist=Linear+Algebra)

but im conflicting with

other examples.

can i trust eig function in matlab for PCA?I am dealing with data mining and im writing paper.thats why im bit cautious.

why didnt you use covariance function for the first function?

thank you

Yes you can trust eigen function of matlab.

To answer a little more, consider an ellipse such that it major axis coincides with x-axis and minor axis coincides with the y-axis.

You can see that two eigen vectors it will have will be (largest one) along x-axis and other on along y-axis. Why it should be along +ve x-axis

it could be along -ve x-axis also, there is similar spread of data in both directions?

So eigen vectors just represent the axis, multiplying it with -ve just does not make it a different eigen-vector.

Hi,

I want to extract the ECG signal of fetal from the maternal and abdominal ECG .Difference between the maternal and abdominal signal will give the fetal ECG signal.I had already done this project in MATLAB using adaptive filtering method.I want to done my project using PCA.I have only wage idea about this PCA.so kindly please help me to complete my project as soon as possible in PCA.

Will be glad to help, please tell what do you want to know? If you are interested in knowing basics of PCA try Wikipedia and if anything is not clear send a message, I will try to answer as clearly as possible.

CAN U PLEASE SEND ME THE CODE FOR FETAL ECG EXTRACTION USING ADAPTIVE FILTERING METHOD?

I have never worked on this data :(

can u send me the code for foetal ecg extraction using adaptive filtering method

hi, i’m working on crop biophysical parameters retrieval using PROSAIL model. can i use pca code in Matlab for inversion of that model to extract the parameters. i can explain briefly my problem: PROSAIL model simulates reflectance (R) when some variables like Leaf Area Index(LAI), Chlorophyll content, and leaf water are used as input parameters. my objective is to retrieve the above variables from measured reflectance. i have large dataset of reflectance and corresponding values of the parameters. plz tell me the steps for solving this problem and necessary codes if u know.

So given Reflectance you want to find out what are input variables? i.e. you want to find out the relationship b/w the variables and the output?

It looks like regression problem. Tell me onething, do certain value of Reflectance is only simulated with certain combination of input parameters,

or that same value of Reflectance can be simulated with other combination of input parameters?

Does that Reflectance at any time ‘t’ depends upon what was reflectance at time ‘t-1’?

Can u plz give us code for PCA implementation for Face Recognition in Matlab……….

You can use the above code for Face Recognition also, calculate Eigen Basis using all of training images, then calculate cofficients of each Face Class or for each person.

Now build classifier using these cofficients for each class,

A naive solution will be to use the nearest neighbor classifier or you can use the SVM.

can this code be used for classification purposes……

you will have to build a classifier on the coefficients, PCA itself is not classifier.

Hi, I’m biomedical engineering student, i wanna to know about PCA application on mammogram, how to apply it to do feature reduction with MATLAB.

What features are you calculating on the mammogram data?

do you know ARMADA matlab for association rul mining of numric data?

I have never worked in ARMADA, sorry :(

After applying PCA eigen values of 6dimensions of 23 dimensions are zero.For dimension reduction i want to reduce 6 attributes to one based on eigen values.can i use ARMADA to find relation between attributes?

Can you give me a matlab code to study the asymmetrie of a region of interest in a color image after a segmentation??

Hi Pamela,

I don’t have matlab code for that. But google it, there are many ways of studying that. You can use the spread in each direction of the eigen vector. You can flip the region around the eigen vector to see if it’s symmetric across the vector.

Please do share if answer is not what you were looking for and what worked for you.

Thanks

MA

Hi

I’m so sorry for the delay but unfortunately I couldn’t implement the matlab code to plot the principals axes and use eigen vector to judge whether a region symmetric or asymmetric (according axe 1 and axe 2)

I need your help please, thanks

Hi

I’m so sorry for the delay but unfortunately I couldn’t implement the matlab code to plot the principals axes and use eigen vector to judge whether a region symmetric or asymmetric (according axe 1 and axe 2)

I need your help please, thanks

How can i use PCA??

Hello, our group is working on a research on face detection using eigenface. Can the code above be sufficient enough to help me with face detection. I really need your help. We still have a week to work on the codes. Thanks. :)

For the preliminary Face detection yes it will work (face images need to be all aligned)

Hi, Moshi. I also doing face detection using PCA. Can you please help me with this? I already compute the eigenvalue. what should i do next? I stuck here. how to test the input image? thank you.

Why are you doing Face Detection using the PCA??? It’s pretty expansive procedure. {are you talking about the Face Recognition?}

However if you want to do, take the all the images belonging to the face class,

Calculate eigen vectors and eigen values. Then choose top N eigen vectors, now for any new test set that comes, project that to these N eigen vectors and calculate reconstruction error.

This should tell you how much this input image is Face or not.

However I suggest you have a look at the Viola Jone’s face detector.

i need pca algorithm to extract feature in palm

Hi Pamela,

object is symmetric w.r.t some axis or some vector passing through the object.

But this means you are trying to find Reflective Symmetry.

Therefore before you go any further you need to define what the symmetry is in your case.

Have a look at Bilateral Symmetry Detection via Symmetry-Growing by Minsu Cho. Although you don’t need completely what they are doing, but reading first few pages you will understand the concept of symmetry and how to that could be evaluated.

Thanks

Thank you mohsenan for your answer but since I’m a beginner in matlab I couldn’t implement the code. I’ve looked everywhere but I didn’t find any information that can help me acts as a real. I also read Bilateral Symmetry Detection but I haven’t be able to use it with my goal. I really need your help and I will be grateful for you

Pamela I will be happy to provide any help possible, but you need to tell me what you need help with.

Hi thanks mohsenan

I have a segmented image where I detected the region of interest. I now need to draw the two axes (major and minor) in this region and subsequently study its asymmetry according to the two axes

-if the region is asymmetric along a single axis: Show 1

-if the region is asymmetric according to two axes: display 2

if the region is symmetric: display 0

I have never worked on this, but I think following should do.

you have segmented out the region so you know location of all the pixels making up that region.

Use PCA to find the major and minor axis.

now reflect the region around the major axis and see if the object is symmetrical or not.

or w.r.t minor axis. to see if it is symmetrically reflective.

I tried to draw the axes using PCA. Can you correct it please??

I=imread(‘image.jpg’);

[coefs,scores,variances,t2] = princomp([I(2,:);I(1,:)]’);

x = mean(I(2,:),2);

y = mean(I(1,:),2);

%la direction principale de donnees

R=coefs;

%longueur des axes

a=

b=

coefs should have the eigen vector. Use the ‘variances’ to find the eigen vector having maximum eigen vlaue.

eigenvalue and the eigen vector should give you direction and the magnitude of the major axis if the object is elliptical.

I tried to do this

segI=segmentation(I);

[n m] = size(segI);

meanA = mean(segI,2);

segI = segI – meanA*ones(1, cols);

% Compute the eigen values and eigen vectors of the covariance matrix

[evec eval] = eig(segI*segI’);

eigvalue = diag(eigvl);

thanks

function [evec, eval, meanA] = PCA(A, maxVec) HOW do I enter values for the matrices?

Hi

segI=segmentation(I);

[ rows, cols] = size(segI);

if(nargin < 2)

maxVec = rows;

end

maxVec = min(maxVec, rows);

meanA = mean(segI,2);

A = segI – meanA*ones(1, cols);

[evec eval] = eig(A*A’);

[eval ind] = sort(-1*diag(eval));

eval = -1*eval(1:maxVec);

evec = evec(:, ind(1:maxVec));

But I couldn't modify it to plot the two axes

your code appears to be wrong.

You have to make a matrix such as like that

let r and c be the vectors containing row and column coordinates of pixels of your object.

A = [r(:)’; c(:)’];

Now make the elements center.

A = A – repmat(mean(A,[],2))

now calculate the eigen vectors

each vector should be two dimensional.

hi Mohsenan,

i new to matlab and PCA…i tried to run your code, but it doesnt seem to work..how to run it?

Use the first function where A is the data matrix.

Hello again. Can you check out what’s wrong with the code I used?

close all

clear all

clc

A=imread(‘image1.jpg’); %Manual reading/call of image

A=rgb2gray(A); %Convert image to rayscale to ensure 2-dimensionality

A=double(A); %Convert the image to type ‘double’.

[rows,cols]=size(A);

x=rows;

if(nargin < 2)

x;

end

x = min(x, rows);

meanA = mean(A,2); %Mean of the image imatrix

tmimg=uint8(meanA); %Converts into an unsigned 8-bit integer. Values range from 0-255.

A = A – repmat(meanA, 1, cols);

plot(A);

C=A'*A; %Covariance Matrix;

[evec,eval]=eig(C); %Eigenvector

ui=A*evec; %Eigenfaces of the covariance matrix C

[eval ind] = sort(-1*diag(eval));

%[eval ind] = sort(diag(eval), ”);

eval = -1*eval(1:x);

evec = evec(:, ind(1:x));

an error appears in line 29. It says that index exceeds matrix dimensions. :( Help. PLease!

Which one is line 29????

how much is the difference in the matrix dimension and the element you are trying to access?

can u please tell me whether the above program can be used for visual feature extraction

It could be used for the Feature Selection.

Sorry I should be more clear, PCA could be used for the feature extraction. Yes. But mainly it is used for the selection of the basis that are more representative of data. You can use use projection of the data on these basis as the feature extraction.

Line 29 is either of these two:

eval = -1*eval(1:x);

evec = evec(:, ind(1:x));

aand what do you mean by your second question. Im sorry Im just an amateur in coding

Moshi, I cannot run the code right now, so I will ask you questions so that you can solve this problem.

1) Please verify what line the error is coming.

2) what is x? if it’s some number it might be increasing either size of ind. Just for checking decrease the x to 2 or 3 see if it works, now check where the x’s value is more than size of matrix or size or array ind.

Index exceeds matrix dimensions.

Error in Whatever (line 22)

eval = -1*eval(1:x);

I tried to create my codes, could you check if i’m doing the right thing?

Here’s the code:

function [signals,PC,V] = pca1(m)

T=CreateDatabase(‘Users\joselle\Desktop\FaceDetection\Database1’);

A=imread(‘marc.jpg’);

A=rgb2gray(A);

A=double(A);

%%%%Sobel edge detection%%%%%%%%%

for i=1:size(A,1)-2

for j=1:size(A,2)-2

Gx=((2*A(i+2,j+1)+A(i+2,j)+A(i+2,j+2))-(2*A(i,j+1)+A(i,j)+A(i,j+2))); %Sobel mask for x-direction:

Gy=((2*A(i+1,j+2)+A(i,j+2)+A(i+2,j+2))-(2*A(i+1,j)+A(i,j)+A(i+2,j))); %Sobel mask for y-direction:

A(i,j)=sqrt(Gx.^2+Gy.^2); %The gradient of the image

%B(i,j)=abs(Gx)+abs(Gy);

end

end

figure,imshow(A); title(‘MARC TAN’);

%%%%%%%%%%%%%%%%%%%%%%%%

[M,N] = size(A);

mn = mean(A,2);

A = A – repmat(mn,1,N); % subtract off the mean for each dimension

covariance = 1 / (N-1) * A * A’; % calculate the covariance matrix

[PC, V] = eig(covariance); % find the eigenvectors and eigenvalues

V = diag(V); % extract diagonal of matrix as vector

[junk, rindices] = sort(-1*V); % sort the variances in decreasing order

V = V(rindices);

PC = PC(:,rindices);

signals = PC’ * A; % project the original data set

so is the code you pasted working?

For the database:

function T = CreateDB(TrainDatabasePath)

% File management

TrainFiles = dir(‘Users\joselle\Desktop\FaceDetection\Database1′);

Train_Number = 0;

for i = 1:size(TrainFiles,1)

if not(strcmp(TrainFiles(i).name,’.’)|strcmp(TrainFiles(i).name,’..’)|strcmp(TrainFiles(i).name,’Thumbs.db’))

Train_Number = Train_Number + 1; % Number of all images in the training database

end

end

% Construction of 2D matrix from 1D image vectors

T = [];

for i = 1 : Train_Number

str = int2str(i);

str = strcat(‘\’,str,’.jpg’);

str = strcat(‘flynn’,str);

img = imread(str);

img = rgb2gray(img);

[irow icol] = size(img);

temp = reshape(img’,irow*icol,1); % Reshaping 2D images into 1D image vectors

T = [T temp]; %’T’ grows after each turn

end

Thank you!

Also, in your code, what does maxvec mean?

this one keeps coming up

>> Whatever

Undefined function or variable ‘maxVec’.

Error in Whatever (line 9)

maxVec = min(maxVec, rows);

Im so sorry for flooding you with questions.

Yes, its working. Anyway, I have figured out how to run your code succesfully. Thank you!

Sorry to bother you again, as I have read in the older threads, you came to mention that this code can be used for feature extraction. Can you explain to me how does it work here?

Im working on face detection using eigenface method wherein my input would come from an image that have undergone sobel detection. Then I have to select the features of the face, example, nose, eyes, mouth and cheeks. Then I have to get the distances between these features. I should produce an output which are the values of these points which will later on be used in HMM for facial recognition.

Sobel Detection you mean convolution with the sobel operator or the edge detection?

When you say features of nose, eyes, mouth and cheeks, you mean features of the bounding box around them? do you already know that bounding box or you want to detect it?

the edge detection. Then after detection, the image would undergo PCA for facial feature extraction.

for the facial features, I want to detect where is the nose eyes mouth and cheeks and their corresponding values. :)

can I have your email address ? or skype maybe?

sorry Moshi Iquin for late reply, we can communicate on the blog much better I think. In this way if someone else wants to provide much better information they can.

FOr mouth, nose and eyes I see you point but for cheeks I have feeling it might not work.

You can do this e.g. for nose, you have to have images where bounding box represent nose. Take all those bounding boxes and calculate eigen vecotrs. Now by reconstruction error you can check whether that particular portion is nose or not.

But there will be many false positives on the image, that could be reduced by making sure that where the nose is detected eyes and mouth are detected in the neighborhood.

Instead of PCA on the RGB or Gray scale image, I will suggest calculating HOG.

hi

i want to extract features of an input image and match with the database image.

how can i apply the fuzzy C-means clustering technique for that?

plz help…

thanx…

Does this helps

http://en.wikipedia.org/wiki/Fuzzy_clustering#Fuzzy_c-means_clustering

or you are looking for code??

Hello,

Can you please show us how to get the matrix A for PCA, Where A’s columns are the data-vectors i.e. [x1, x2, x3,,…..xn]. Please help, since am a newbie.

Thank You

Shirah, do you have images?? I don’t understand your question fully, are you looking for the loop something like below??

A = [];

for i=1:N

Im = imreadh(imgName{i});

A = [A Im(:)];

end

hello .. we are working on detection and classification of brain tumor .. detection is done .. for classification i need to create a knowledge base using pca and then have to apply svm classifier .. can i get the source code for tis ? plz plz

hello can u plz show me the matalb coding for creating the knowledge base of features of mri images of brain ? and then to classify the images using svm classifier .. plz plz

thank you

Prerana what do you mean by the knowledge base?

like i have to tabulate the main features like entropy contrast of normal brain images and store it in a database so that i can compare the abnormalities of brain tumors with tis database.. knowledge base is nothing but the databse of extracted features.. so i need to know how to extract the features if images using pca

thank you

Prerana, can you tell me given one image how will you calculate entropy contrast?

PCA is not used for feature extraction, but mainly dimentality reduction, although eigen vectors are considered as basis vectors representing important features of the dataset. But PCA is not feature extraction.

Just calculate the eigen vectors and eigen values of the matrix whose each column is vectorized image.

after that project your training images on the Eigen Vectors, you will get the coefficient vector for each image. Train SVM by taking these coefficient vectors as your feature vectors.

hi,

can you please provide me code for b-spline interpolation method…

thanks in advance.

you can use interp2

Hello,

Can you please give the code to create the matrix training set. Thank You.

matrix used for the calculation of eigen vectors will consist of feature vectors, which depend upon how you selected and calculate feature vectors.

Hi, thanks for replying. In fact am stuck in extracting the feature vectors. Well, am working on dorsal hand vein and i have already got a thin version of the vein patterns. Now i have to process it where am stuck.

It will be very helpful if you provide some codes that will extract the pixel or coordinates of the vein patterns. Thank You.

Hi, can you help me with face detection using PCA? how to do matching for test image?

Why are you doing Face Detection using the PCA??? It’s pretty expansive procedure. {are you talking about the Face Recognition?}

However if you want to do, take the all the images belonging to the face class,

Calculate eigen vectors and eigen values. Then choose top N eigen vectors, now for any new test set that comes, project that to these N eigen vectors and calculate reconstruction error.

This should tell you how much this input image is Face or not.

However I suggest you have a look at the Viola Jone’s face detector.

Yeah, i am doing Face Detection.. for any input image, i want to detect whether there is a face or not. no need to recognize the face belong to whom.

btw, how to project the new test image to the training eigenvector? and how to calculate the reconstruction error? would you mind to share the code?

projection on eigen vector is just taking dot product of input with each eigen vector.

If you have choosen N eigen vectors this should give you N cofficients.

Using these cofficients and eigen vectors you can find reconsturction error {telling you how much information of input vector could be represented by using your eigen vectors }

Have a look at following

http://www.cs.washington.edu/education/courses/cse576/09sp/projects/project3/artifact/tingyou/proj3/index.html

sir is this program can be used for ecg signal analysis. matrix of my data is 1×2500.

Hi Kiruthiga

1×2500 is vector not a matrix or you mean it is one row of your matrix?

it is a vector then?

hi,

i want to extract features like mean,entropy,energy and contrast by using pca..how can i do it…???

please help me..it is for my project..

mean is just average of your feature points, regarding contrast, can you explain your question more please?

please help me sir

I have 8 images(50×50). I save these images inside an array named images[8*2500] where the number 2500 represents the amount of pixels for an image and the number 8 the total amount of images that I have. I would like to use these images for a neuron network however 2500* 8 is a big number to give as an input and thus I would like to decrease this number but retain as much information as possible. I heard that you can do this with the princomp function which implements PCA. The problem is that I haven’t found any example on how to use this function, and especially for my case. If I run

[COEFF, SCORE, latent] = princomp(images);

it runs fine but how can I then get the array that is the input of neural network?

Hala, I am not sir first of all :) it makes me feel old.

Second, what you are trying to achieve is called Dimensionality reduction. Google it and you will find other ways of doing that. PCA is quite common one but depends upon what do you mean by “information”.

However let me give you some steps of doing that using the code listed above

[evec, eval, meanA] = PCA(A, maxVec)

you can get the Eigen Vectors and their values. These are now our new basses, but we want only some of them. So we use eigen values (eval) to pick the ones. For example if you want only top 10 just sort eval in decreasing order and with that index pick the respective eigen-vectors.

Output of our function above is already sorted, so you just need to do evec= evec(:, 1:10).

Now let evec be new matrix; you can mean subtract your data and then project that into new basses.

dataNew = evec’*MeanSubtractedData.

To perform all these you can use third and last function.

Now Lets Come to Most Serious Issue. YOU DONT HAVE DATA. you have only 8 images, each of 2500 dimension. These are too small number to do the dimensionality reduction or to train your Neural Network. You need to get more examples.

thanks a lot

i have 100 image to train network and 100 for test

but i will take from each image 8 partial image then i will apply the pca

i read the answer but i did’t understand what u mean

“To perform all these you can use third and last function.”

which function?

the functions in the above blog entry on which you are commenting.

excuse me

but what the different between

coffs by apply

function [coffs] = calcPcaCoff( evec, data, meanVec)

and coeff

[COEFF, SCORE, latent] = princomp(images)

it’s the same??

is princomp matlab function?

please have a look at the matlab’s explanation of princomp’s functionality.

and MeanSubtractedData is coffs by apply

function [coffs] = calcPcaCoff( evec, data, meanVec)

yes princomp matlab function

thanks

hi .. please help me

I have 8 images(50×50). I save these images inside an array named images[2500*8]

so when i apply PCA i have evec 2500*2500. is that true?

or i must apply PCA for images’

thanks

hello

how can i extract feature from images

what kind of features you want to extract?