PCA - RGB transformation

318 views
Skip to first unread message

jan.vo...@gmail.com

unread,
Mar 7, 2013, 8:17:52 PM3/7/13
to accor...@googlegroups.com
Hi César,

I am trying to run the PCA for a RGB image (to get 3 principal components from 3 correlated intensity bands). The whole PCA included in the framework is awesome and works really smoothly but I can't find a way how to transpose the principal components (or principal bands) back to the RGB (original space). Is there anything I'm missing?

Thanks for the reply.

Jan

César

unread,
Mar 7, 2013, 8:49:11 PM3/7/13
to accor...@googlegroups.com
Hi Jan!

You can try to run one of the image converters (such as ArrayToImage) in the ComponentsMatrix of the principal component analysis object. However, for this to work properly I guess you may need first to convert the ComponentsMatrix into a jagged array by using the static Matrix.ToArray() extension method.

Something like this:

double[][] components = pca.ComponentsMatrix.ToArray();
foreach (double[] comp in components)
{
   
Bitmap image; new ArrayToImage().Convert(comp, out image);
   
// show image
}


However I am not completely sure if this is really the way to do it (I also didn't test the code, please consider it as pseudocode).

Or by the way you may also want to try extracting them from the transpose of the ComponentsMatrix. To do so you can specify transpose: true in the ToArray extension method at the first line.

Please let me know if this works. If it does not, we can figure it out!

Best regards,
Cesar

César

unread,
Mar 7, 2013, 8:50:32 PM3/7/13
to accor...@googlegroups.com
Ermm.. Sorry, I just realized I may have misunderstood your question. If what I said doesn't makes sense in your application, please let me know.

jan.vo...@gmail.com

unread,
Mar 7, 2013, 9:01:46 PM3/7/13
to accor...@googlegroups.com
Thanks for quick response!

I think we don't understand each other, let me reformulate my problem:

1. I have an image containing three channels. I treat those channels as three different data sets and run the PCA. (Note that possible values of each element in the data set is from 0 to 255).

2. The output of PCA - principal components - has however values that are sometimes negative, sometimes greater that 255. I guess it's because the values are in principal component space. I need to somehow transfer them back to original domain (so I am able to do some other work with them...).

Good example of what I want to do is on page 230 in this book - http://goo.gl/2gupQ.

Does it makes sense now?

Thanks again for any ideas!


jan.vo...@gmail.com

unread,
Mar 19, 2013, 12:43:09 AM3/19/13
to accor...@googlegroups.com
Hi César,

just checking if you had chance to look at my problem? So far I figured out that subtracting mean vector from PCA.Results leads to almost good values, however some values of intensity are are still negative. Any ideas would be great!

Thank you.
Jan

César

unread,
Mar 19, 2013, 12:41:36 PM3/19/13
to accor...@googlegroups.com
Hi Jan,

You can attempt to normalize the values using Accord.Math.Tools.Scale. You can compute the maximum value, the minimum value, then call Scale to enforce the values are always between 0 and 1 (or 0 and 255). Perhaps this could be of some help. Use AnalysisMethod.Standardize in the PCA constructor as well to make sure the values are properly normalized to the same magnitude.

I hadn't replied either because I was thinking of a more general solution to be added to the framework, sorry about that. But I guess the scaling could work. 

Best regards,
Cesar

César

unread,
Mar 19, 2013, 12:42:15 PM3/19/13
to accor...@googlegroups.com
By the way, the Maximum and Mininum can also be computed using the Matrix.Max and Matrix.Min extension methods. Hope it helps!

Regards,
Cesar
Reply all
Reply to author
Forward
0 new messages