3

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.

enter image description here

I haven't used PCA as yet my current code is as 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)),[])
  • What is your input data? The coordinates subject to rotation? Is your image being rotated with respect to the origin, or is there an offset? There isn't enough information to help you solve your problem. Please elaborate. – rayryeng Apr 24 '15 at 15:16
  • Hi, I will update the question. –  Apr 24 '15 at 15:29
  • Ahh ok. So is it my understanding that you have a set of coordinates, and you want to reproject the coordinates onto the basis vectors to remove the rotation? It's pretty unclear from your diagram. Also, some code and/or the raw images that you're using would certainly be useful to help you solve your problem. Right now, I can't do anything. – rayryeng Apr 24 '15 at 16:25
  • Also, you can use `regionprops` and determine the angle of orientation and simply apply `imrotate` in the reverse direction to rotate the image. It's not necessary to use PCA out of the box. – rayryeng Apr 24 '15 at 16:27
  • I added my code and the picture i use. regionprops gives really bad results. –  Apr 24 '15 at 16:35
  • It looks like your shape is subject to 3D rotation. Is that true? – rayryeng Apr 24 '15 at 16:36
  • Okay the PCA issue has been solved before and it's as simple as obtaining a rotation matrix for your projection to your PC... – krisdestruction Apr 24 '15 at 16:36
  • Actually you don't even need a rotation matrix. the `PCA` function gives you the transformation and you can always create an inverse transformation afterwards >.> – krisdestruction Apr 24 '15 at 16:37
  • [coeff,score,latent,tsquared] = pca(___) so what do i use and how? –  Apr 24 '15 at 16:39
  • @rayryeng i m looking for least change in variance in image. hence PCA. –  Apr 24 '15 at 16:40
  • With respect to what? The directionality of the shape? The intensities of the image? – rayryeng Apr 24 '15 at 16:40
  • I posted a short answer as to how to use PCA and return it to the original transformation which appears to be what you want. Can you clarify if this is correct? – krisdestruction Apr 24 '15 at 16:42
  • With respect to least change in variation in intensity of image –  Apr 24 '15 at 16:42
  • @rayryeng Are you familiar with PCA? – krisdestruction Apr 24 '15 at 16:43
  • @krisdestruction - Yes. I tried using it to solve an algorithmic problem I had a couple of weeks ago. It didn't work so I used something else: http://stackoverflow.com/questions/29550785/algorithm-to-group-sets-of-points-together-that-follow-a-direction – rayryeng Apr 24 '15 at 16:44
  • @rayryeng Okay I presumed you knew, just wanted to make sure! – krisdestruction Apr 24 '15 at 16:46
  • @user2307268 The rotation matrix `C` should be self explainatory. Let me know if you need any clarification on the issue. – krisdestruction Apr 24 '15 at 16:46
  • @user2307268 Updated my answer with the **least** amount of variance – krisdestruction Apr 24 '15 at 16:51
  • I have worked with PCA before. But orientation is all new to me. I know the basic theory.@krisdestruction –  Apr 24 '15 at 16:51
  • @user2307268 That's okay, did it solve your issue? I'm not sure what you want with the histogram, but perhaps with this new transformed space, it would solve your issue? Perhaps you can clarify that histogram part. – krisdestruction Apr 24 '15 at 16:52
  • 1
    @krisdestruction - Haha no worries. Good job on your post though. +1. – rayryeng Apr 24 '15 at 16:52
  • @rayryeng Thanks, I tried to find PCA duplicate posts, but I couldn't find any in a limited time. I think this is a good post to refer to in the future! – krisdestruction Apr 24 '15 at 16:53

1 Answers1

2

Construct your PCA transformation matrix using. C is your transformation or your rotation matrix that will transform it to your highest variance directions.

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

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

C = C(:,1:5);

Create the transformed data using the transformation C. The data will now be in the new transformed space with the first dimension being the largest variance, second dim being the second largest variance, etc. Since you are looking for the least variance, that is the last dimension

tfData = data * C;

Process your data accordingly in this new transformed space. To obtain your inverse transformation and put it back to the original space, use the following.

origAxisData = tfData * C';

The transpose operation C' is the same as the inverse operation inv(C) for the inverse transformation as it is orthogonal as described here. However the transpose is much faster to calculate than the inverse, especially for high dimensions.

You can plot your principal component/axes/kernel by plotting the columns of C as follows.

for i = 1:length(end)
    figure; plot( C(:,1) );
end
Community
  • 1
  • 1
krisdestruction
  • 1,950
  • 1
  • 10
  • 20
  • Thanks, I guess I have a bit more work to do and things to understand. Ill update you when i am done. Thanks again. –  Apr 24 '15 at 16:58
  • Sure thing, accept it if it's correct, or let me know if it needs any changes! – krisdestruction Apr 24 '15 at 16:58
  • Since you asked, you can plot the axes with the above code, although that's generally more for higher dimensions (eg. mine is 1000 dimensions). – krisdestruction Apr 24 '15 at 17:00
  • Hi, I wet through PCA. Can i directly apply PCA on an image or do i need to restructure it? –  Apr 27 '15 at 10:33
  • Well that depends on the variance you're trying to capture. If you're trying to get the maximum variance of neighbouring x-y pixels, you will need to extract neighbouring pixel values. Regardless of what approach you want to take, you will need to put the dimensions of variance you want to capture and put it into a feature vector with rows representing each sample dataset, and columns representing feature values in each dimension you are measuring. – krisdestruction Apr 27 '15 at 21:56