using PIL for PCA analysis

devnew at gmail.com devnew at gmail.com
Thu Feb 21 02:41:22 EST 2008


hi guys
i am trying out  PCA analysis using python.I have a set of
jpeg(rgbcolor) images whose pixel data i need to extract and make a
matrix .( rows =num of images and cols=num of pixels)
For this i need to represent an image as an array.
i was able to do this using java's BufferedImage as below

<javacode>
int[] rgbdata = new int[width * height];
image.getRGB(0,0,width,height,rgbdata,0,width);

doubles = new double[rgbdata.length];
int i;
for ( i = 0; i < bytes.length; i++) {
   doubles[i]  = (double)(rgbdata[i]);
}
</javacode>

this doubles[] now represent a single image's pixels

then i can get a matrix of say 4 images ..(each of 4X3 size)
<sampledata>
images[][]  rows=4,cols=12
[
[-4413029.0, -1.0463919E7,... -5201255.0]

[-5399916.0, -9411231.0, ... -6583163.0]

[-3886937.0, -1.0202292E7,... -6648444.0]

[-5597295.0, -7901339.0,... -5989995.0]
]
</sampledata>
i can normalise the above matrix to zeromean and then find covariance
matrix by
images * transpose(images)

my problem is how i can use PIL to do the same thing..if i extract
imagedata using im.getdata()
i will get
<sampledata>
[
[(188, 169, 155), (96, 85, 81),.. (176, 162, 153)]

[(173, 154, 148), (112, 101, 97),.. (155, 140, 133)]

[(196, 176, 167), (100, 83, 76), ... (154, 141, 132)]

[(170, 151, 145), (135, 111, 101), ... (164, 153, 149)]
]
</sampledata>
i donot know how to find covariance matrix from such a matrix..it
would'v been ideal if they were single values instead of tuples..i
can't use greyscale images since the unput images are all rgb jpeg

can someone suggest a solution?
thanks
dn



More information about the Python-list mailing list