Home > Machine Learning, matlab > Principal Component Analysis (Matlab Code)

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

About these ads
  1. kartic
    May 5, 2009 at 10:32 am

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

    • mohsenam
      May 5, 2009 at 4:17 pm

      in PCA? You are talking about the Neural Network?

      • kartic
        May 7, 2009 at 4:54 am

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

      • mohsenam
        May 7, 2009 at 6:18 pm

        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

      • kartic
        May 8, 2009 at 6:58 am

        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.

      • mohsenam
        May 8, 2009 at 5:19 pm

        {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

      • kartic
        May 8, 2009 at 7:15 pm

        function [coffs] = calcPcaCoff( evec, data, meanVec)
        in the above fun what is the value of data and meanvec

      • mohsenam
        May 8, 2009 at 7:21 pm

        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.

      • kartic
        May 11, 2009 at 9:50 am

        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

      • mohsenam
        May 11, 2009 at 6:17 pm

        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.

      • kartic
        May 12, 2009 at 6:25 pm

        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

      • mohsenam
        May 12, 2009 at 6:37 pm

        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.

      • kartic
        May 18, 2009 at 6:28 pm

        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

      • kartic
        May 8, 2009 at 7:00 am

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

  2. kartic
    May 7, 2009 at 5:02 am

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

  3. kartic
    May 8, 2009 at 7:13 pm

    function [coffs] = calcPcaCoff( evec, data, meanVec)
    in the above function what values should be given to data and meanvec

  4. kartic
    May 11, 2009 at 9:49 am

    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

  5. Marcela
    March 13, 2010 at 5:31 pm

    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 :)

  6. mohsenam
    March 13, 2010 at 7:59 pm

    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.

  7. Ciro Macedo
    May 10, 2010 at 11:57 pm

    nice post…
    congratulations !!!

    • mohsenam
      May 17, 2010 at 4:16 pm

      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. :)

  8. muhammad
    June 11, 2010 at 2:05 am

    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.

    • mohsenam
      June 16, 2010 at 4:50 am

      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

  9. prashant
    August 13, 2010 at 5:24 am

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

  10. mohsenam
    September 4, 2010 at 8:07 pm

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

  11. ndm
    September 27, 2010 at 8:47 am

    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..

    • mohsenam
      October 5, 2010 at 7:42 pm

      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

  12. firman
    November 16, 2010 at 8:50 am

    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??

    • mohsenam
      November 17, 2010 at 6:05 pm

      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

  13. December 6, 2010 at 1:43 pm

    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

    • mohsenam
      December 22, 2010 at 3:41 am

      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.

      • mohsenam
        December 23, 2010 at 11:26 pm

        sorry Fahriddin, I misspelled your name

  14. Supriya
    January 3, 2011 at 7:02 pm

    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

    • mohsenam
      January 3, 2011 at 7:13 pm

      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?

  15. Supriya
    March 13, 2011 at 3:11 pm

    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

  16. Supriya
    March 13, 2011 at 3:11 pm

    plzz tell me thanks

  17. sohail Khan
    March 20, 2011 at 6:55 am

    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?

    • mohsenam
      March 22, 2011 at 12:47 pm

      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).

  18. sohail Khan
    March 22, 2011 at 11:42 am

    Hi,
    Reply plz,
    Can we seperate the Vehicle # form picture thurough Image processing in matlab?
    if so then plz send code if you have!!!

    • mohsenam
      March 22, 2011 at 12:45 pm

      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.

  19. August 19, 2011 at 6:36 am

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

    • mohsenam
      August 20, 2011 at 10:24 pm

      can you please further explain your comment? Thanks

  20. idem
    November 15, 2011 at 9:52 am

    Hi Moshenan,

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

    • mohsenam
      November 15, 2011 at 2:13 pm

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

  21. sowmya
    November 25, 2011 at 3:50 pm

    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

  22. sowmya
    November 25, 2011 at 4:57 pm

    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

    • mohsenam
      November 27, 2011 at 7:27 pm

      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.

  23. geethu
    January 23, 2012 at 5:07 am

    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.

    • mohsenam
      January 26, 2012 at 9:02 pm

      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.

    • January 5, 2013 at 9:56 am

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

      • mohsenam
        January 24, 2013 at 11:54 pm

        I have never worked on this data :(

    • January 5, 2013 at 9:58 am

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

      geethu :
      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.

  24. nilimesh mridha
    February 22, 2012 at 11:01 am

    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.

    • mohsenam
      February 23, 2012 at 3:08 pm

      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’?

  25. Asha Shelke
    February 23, 2012 at 11:29 am

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

    mohsenam :in PCA? You are talking about the Neural Network?

  26. February 23, 2012 at 11:30 am

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

    • mohsenam
      February 23, 2012 at 3:00 pm

      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.

  27. yaqoob
    February 26, 2012 at 9:27 am

    can this code be used for classification purposes……

    • mohsenam
      February 26, 2012 at 6:06 pm

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

    • Maha
      June 27, 2012 at 4:23 pm

      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.

      • mohsenam
        June 27, 2012 at 7:07 pm

        What features are you calculating on the mammogram data?

  28. sowmya
    May 28, 2012 at 3:20 pm

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

    • mohsenam
      June 27, 2012 at 7:10 pm

      I have never worked in ARMADA, sorry :(

  29. sowmya
    May 28, 2012 at 3:40 pm

    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?

  30. Pamela
    September 20, 2012 at 7:32 am

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

    • mohsenam
      September 20, 2012 at 11:55 pm

      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

      • Pamela
        October 9, 2012 at 3:56 pm

        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

      • Pamela
        October 9, 2012 at 4:01 pm

        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

  31. Pamela
    September 20, 2012 at 7:35 am

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

    How can i use PCA??

  32. Moshi Iquin
    September 27, 2012 at 10:42 pm

    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. :)

    • mohsenam
      October 6, 2012 at 10:03 pm

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

    • Nurmala
      June 10, 2013 at 12:28 pm

      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.

      • mohsenam
        June 11, 2013 at 1:42 pm

        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.

  33. kowsihan
    October 9, 2012 at 10:59 am

    i need pca algorithm to extract feature in palm

  34. Pamela
    October 9, 2012 at 3:57 pm

    Pamela :
    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

    • mohsenam
      October 10, 2012 at 4:01 pm

      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

      • Pamela
        October 12, 2012 at 7:15 pm

        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

      • mohsenam
        October 13, 2012 at 3:23 am

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

  35. Pamela
    October 13, 2012 at 9:44 am

    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

    • mohsenam
      October 15, 2012 at 12:32 am

      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.

      • Pamela
        October 15, 2012 at 11:38 am

        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=

      • mohsenam
        October 15, 2012 at 12:00 pm

        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.

  36. Pamela
    October 17, 2012 at 1:06 pm

    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

  37. Moshi Iquin
    October 17, 2012 at 7:33 pm

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

  38. Pamela
    October 17, 2012 at 9:53 pm

    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

  39. mohsenam
    October 17, 2012 at 10:06 pm

    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.

  40. syada
    November 3, 2012 at 7:11 am

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

    • mohsenam
      November 3, 2012 at 11:26 am

      Use the first function where A is the data matrix.

  41. Moshi Iquin
    November 14, 2012 at 5:37 pm

    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!

    • mohsenam
      January 28, 2013 at 5:56 pm

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

  42. subin
    January 28, 2013 at 5:23 pm

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

    • mohsenam
      January 28, 2013 at 5:27 pm

      It could be used for the Feature Selection.

    • mohsenam
      January 28, 2013 at 5:33 pm

      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.

  43. Moshi Iquin
    January 29, 2013 at 1:51 am

    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

    • mohsenam
      January 29, 2013 at 2:03 am

      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.

  44. Moshi Iquin
    January 31, 2013 at 7:30 am

    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

    • mohsenam
      January 31, 2013 at 8:25 am

      so is the code you pasted working?

  45. Moshi Iquin
    January 31, 2013 at 7:31 am

    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!

  46. Moshi Iquin
    January 31, 2013 at 8:48 am

    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.

  47. Moshi Iquin
    February 7, 2013 at 2:41 am

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

  48. Moshi Iquin
    February 8, 2013 at 3:45 pm

    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.

    • mohsenam
      February 8, 2013 at 3:55 pm

      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?

  49. Moshi Iquin
    February 9, 2013 at 2:33 am

    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?

    • mohsenam
      June 11, 2013 at 1:53 pm

      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.

  50. Parul Chauhan
    March 21, 2013 at 2:11 pm

    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…

  51. shirah
    March 24, 2013 at 8:57 pm

    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

    • mohsenam
      June 11, 2013 at 1:57 pm

      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

  52. prerana
    April 1, 2013 at 6:01 pm

    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

  53. prerana
    April 1, 2013 at 6:05 pm

    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

    • mohsenam
      April 2, 2013 at 11:24 pm

      Prerana what do you mean by the knowledge base?

      • prerana
        April 3, 2013 at 10:15 am

        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

      • mohsenam
        April 4, 2013 at 7:34 pm

        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.

    • mohsenam
      April 4, 2013 at 7:36 pm

      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.

  54. April 2, 2013 at 6:26 am

    hi,
    can you please provide me code for b-spline interpolation method…
    thanks in advance.

    • mohsenam
      April 2, 2013 at 11:23 pm

      you can use interp2

  55. Shirah
    April 3, 2013 at 11:12 am

    Hello,

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

    • mohsenam
      April 4, 2013 at 7:29 pm

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

      • shirah
        April 5, 2013 at 5:52 pm

        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.

  56. Nurmala
    June 11, 2013 at 3:28 am

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

    • mohsenam
      June 11, 2013 at 1:58 pm

      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.

  57. Nurmala
    June 11, 2013 at 3:06 pm

    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?

  58. kiruthiga
    October 30, 2013 at 4:13 pm

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

    • mohsenam
      October 30, 2013 at 5:13 pm

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

    • mohsenam
      March 24, 2014 at 5:00 pm

      it is a vector then?

  59. Biruntha
    March 21, 2014 at 7:20 am

    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..

    • mohsenam
      March 24, 2014 at 4:59 pm

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

  60. hala
    September 16, 2014 at 1:04 pm

    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?

    • mohsenam
      September 17, 2014 at 3:34 am

      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.

  61. hala
    September 17, 2014 at 1:33 pm

    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?

    • mohsenam
      September 17, 2014 at 2:31 pm

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

  62. hala
    September 17, 2014 at 7:28 pm

    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??

    • mohsenam
      September 18, 2014 at 6:08 am

      is princomp matlab function?

    • mohsenam
      September 18, 2014 at 8:00 am

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

  63. hala
    September 17, 2014 at 7:54 pm

    and MeanSubtractedData is coffs by apply
    function [coffs] = calcPcaCoff( evec, data, meanVec)

  64. hala
    September 18, 2014 at 6:33 pm

    yes princomp matlab function
    thanks

  65. hala
    September 24, 2014 at 5:56 am

    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

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: