Calculating and plotting principal components using basic component analysis (PCA) in Matlab

I have an image. I need to determine the axis along which the variance of the image is smallest. A little reading and searching has led me to conclude that Basic Component Analysis (PCA) is the best alternative. Can anyone help me orient the image in relation to its main axis? Since I recently got acquainted with Matlab, this is a bit difficult for me. Below is a sample image. I am trying to rotate an image so that I can generate a histogram.

enter image description here

I haven't used PCA yet, but my current code is shown below

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)),[])

      

+3


source to share


1 answer


Plot the PCA transformation matrix using. C is your transformation, or your rotation matrix, which will transform it to your largest deviations.

[C,~,~,~,explained] = pca( data );

      

Remove PC if you want to truncate components (say 1-5 components). If you don't need to truncate / reduce the size, ignore this step.

C = C(:,1:5);

      

Create transformed data using transformation C

. The data will now be in a new transformed space with size first being the largest variance, second dim second largest, and so on. Since you are looking for the smallest deviation , i.e. the last

tfData = data * C;

      



Treat your data appropriately in this new transformed space. To get the inverse transformation and return it to its original space use the following.

origAxisData = tfData * C';

      

The transpose operation C'

is the same as the inverse operation inv(C)

for the inverse transform, since it is orthogonal as described here . However, transposition is much faster than reverse, especially for larger sizes.

You can plot your main component / axes / core by plotting C columns as follows.

for i = 1:length(end)
    figure; plot( C(:,1) );
end

      

+2


source







All Articles