title: Calculating and Plotting Principal Components using Principal Component Analysis (PCA) in Matlab
body: <p>I have an image. I need to identify the axis along which the variance of the image is the smallest. A bit of reading and searching led me to the conclusion that Principal Component Analysis(PCA) is the best alternative. Can anyone help me with orienting the image with respect to its principal axis? Since i am recently introduced to matlab i find it a bit difficult. An example of the image is below. I am trying to rotate the image so that i can generate the histogram. </p>
  
  <p><img src="https://i.stack.imgur.com/pBNRe.png" alt="enter image description here"></p>
  
  <p>I haven't used PCA as yet my current code is as shown below</p>
  
  <pre><code>enter code here
  I2='image'
  I11= bwlabel(I2);
  OBB = imOrientedBox(I11);
  obbsize=[];
  for i=1:size(OBB,1)
     obbsize=[obbsize,OBB(i,3)*OBB(i,4)];
  end
  [a,i]=max(obbsize);
  I11=(imrotate(I2,OBB(i,5)));
  imshow(I11,[])
  
  [pks,locs] =findpeaks(sum(I11,2));
  [M1,Indx1] = max(pks);
  imshow(I11(1:locs(Indx1),1:size(I11,2)),[])
  </code></pre>