[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

using PIL for PCA analysis

devnew@gmail.com

2/21/2008 7:41:00 AM

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
5 Answers

Bronner, Gregory

2/21/2008 2:36:00 PM

0

Since nobody has responded to this:

I know nothing about PIL, but you can do this using numpy and scipy
fairly easily, and you can transform PIL arrays into Numpy arrays pretty
quickly as well.



-----Original Message-----
From: devnew@gmail.com [mailto:devnew@gmail.com]
Sent: Thursday, February 21, 2008 2:41 AM
To: python-list@python.org
Subject: using PIL for PCA analysis

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

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

This message is intended only for the personal and confidential use of the designated recipient(s) named above. If you are not the intended recipient of this message you are hereby notified that any review, dissemination, distribution or copying of this message is strictly prohibited. This communication is for information purposes only and should not be regarded as an offer to sell or as a solicitation of an offer to buy any financial product, an official confirmation of any transaction, or as an official statement of Lehman Brothers. Email transmission cannot be guaranteed to be secure or error-free. Therefore, we do not represent that this information is complete or accurate and it should not be relied upon as such. All information is subject to change without notice.

--------
IRS Circular 230 Disclosure:
Please be advised that any discussion of U.S. tax matters contained within this communication (including any attachments) is not intended or written to be used and cannot be used for the purpose of (i) avoiding U.S. tax related penalties or (ii) promoting, marketing or recommending to another party any transaction or matter addressed herein.

devnew@gmail.com

2/21/2008 2:54:00 PM

0

On Feb 21, 7:35 pm, "Bronner, Gregory" <gregory.bron...@lehman.com>
wrote:
you can do this using numpy and scipy
> fairly easily, and you can transform PIL arrays into Numpy arrays pretty
> quickly as well.
>

i can use numpy ndarray or matrix once i have a PIL array with
elements in the correct format(ie a single number for each pixel
instead of a tuple of integers)
it is the image data extraction step that is giving me the problem

ie i want PIL to return an image as something like
[-4413029.0, -1.0463919E7,... -5201255.0]
instead of
[(188, 169, 155), (96, 85, 81),.. (176, 162, 153)]

Any PIL experts please help
dn

Paul McGuire

2/21/2008 2:57:00 PM

0

On Feb 21, 1:41 am, "dev...@gmail.com" <dev...@gmail.com> wrote:
> 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

I'm surprised PIL doesn't have a grayscale conversion, but here is one
that can manipulate your RGB values:

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

# following approx from http://www.dfanning.com/ip_tips/color...
grayscale = lambda (R,G,B) : int(0.3*R + 0.59*G + 0.11*B)
print [ [ grayscale(rgb) for rgb in row ] for row in sampledata ]

prints (reformatted to match your sampledata):

[
[173, 87, 165],
[159, 103, 143],
[181, 87, 143],
[156, 117, 155]
]

harryos

2/26/2008 4:19:00 PM

0

>Paul McGuire wrote
> # following approx fromhttp://www.dfanning.com/ip_tips/color...
> grayscale = lambda (R,G,B) : int(0.3*R + 0.59*G + 0.11*B)
> print [ [ grayscale(rgb) for rgb in row ] for row in sampledata ]


Paul
in PIL handbook ,they mention a Luma transform on page15, under the
im.convert() section..
L = R * 299/1000 + G * 587/1000 + B * 114/1000
is that not similar to what you mentioned?(I am newbie in this area..)
if i want to do an array of PIL image data i can use
img=Image.open("myimg.jpg") .convert("L")
pixelarray=img.getdata()

thus i guess i can build a matrix of a set of images
is there something wrong in the way i do this above?may be i can use
that to find covariance matrix for the set of images?

H

sheldonlg

6/12/2014 11:37:00 AM

0

On 6/12/2014 4:27 AM, news wrote:
> On Wed, 11 Jun 2014 14:35:24 +0000 (UTC), Shelly
> <sheldonlg@thevillages.net> wrote:
>
>> On 6/11/2014 10:22 AM, news wrote:
>>> On Wed, 11 Jun 2014 12:00:03 +0000 (UTC), Shelly
>>> <sheldonlg@thevillages.net> wrote:
>>>
>>>> On 6/11/2014 1:45 AM, news wrote:
>>>>> On Tue, 10 Jun 2014 14:25:54 +0000 (UTC), Shelly
>>>>> <sheldonlg@thevillages.net> wrote:
>>>>>
>>>>>> On 6/10/2014 9:27 AM, news wrote:
>>>>>>> On Mon, 9 Jun 2014 13:40:36 +0000 (UTC), Shelly
>>>>>>> <sheldonlg@thevillages.net> wrote:
>>>>>>>
>>>>>>>> On 6/9/2014 9:23 AM, news wrote:
>>>>>>>>> On Fri, 6 Jun 2014 17:16:14 +0000 (UTC), Shelly
>>>>>>>>> <sheldonlg@thevillages.net> wrote:
>>>>>>>>>>>
>>>>>>>>>>> Thank you. We agree yet again.
>>>>>>>>>>
>>>>>>>>>> "Yet again" implies (at least in American English) that this is
>>>>>>>>>> frequently the case. That is not what happens here. I usually disagree
>>>>>>>>>> with you, but not in this case. So a "This time we agree" would have
>>>>>>>>>> conveyed the appropriate message with the proper implications.
>>>>>>>>>
>>>>>>>>> What was that you were saying about sarcasm not working on the Net?
>>>>>>>>
>>>>>>>> I didn't detect any.
>>>>>>>
>>>>>>> Then you agree with my original statement?
>>>>>>
>>>>>> No, and I fail to see how my comments lead to your conclusion. I was
>>>>>> VERY clear in what I said. Pity you failed to understand it, but I
>>>>>> can't help that.
>>>>>
>>>>> Did you really think I was saying we were in constant agreement???
>>>>
>>>> No, and I repeat:
>>>>
>>>> =========
>>>> "Yet again" implies (at least in American English) that this is
>>>> frequently the case.
>>>> =========
>>>
>>> No, it doesn't. It simply means one more time. There may have only
>>> been two such instances.
>>
>> Do you understand the word "imply"?

Yes, I do.

im?ply
strongly suggest the truth or existence of (something not expressly stated).

in?fer
deduce or conclude (information) from evidence and reasoning rather than
from explicit statements.

The only difference is that imply denotes the intent whereas infer
denotes the result. Now

im?pli?ca?tion
the conclusion that can be drawn from something, although it is not
explicitly stated.

also refers to the result, so I will revise what I wrote above to be
consistent with my thought pattern at the time:

The implication of "yet again" (at least in American English) that this
is that this is frequently the case.

Happy now?


>
> I certainly understand the difference between "imply" and "infer":
> I imply something by saying something, you infer something from what I
> said. It's easy to get that wrong, I've done it myself.
>
>> English is a wonderful language in
>> that it has twice the vocabulary of any other language.
>
> Indeed. American is quite similar.
>
>> This allows us
>> to have nuances conveyed with our choice of words. The
>> implication/nuance conveyed by "yet again" is that it is one more
>> occurrence in a large set of these and is usually the case.
>
> What you really mean is that your inference is such.
>
> Ah, 'usually'...but not here.

Here I used implication, so it correctly conveys what I was trying to
say. I referred to the result, not the intent.

>
>> Yes,
>> technically it could mean that there was just one other time.
>
> Actually there was more than one.

True, but irrelevant to what I just stated.

>
>> However
>> that is not the *implication*, and *that*, the *implication* is what I
>> was talking about.
>
> See above. I didn't imply it, but somehow you inferred it.

Not just "somehow". Whether or not it was your intent, that is the
typical result.

>
>>>> And once again you need to learn English. "frequently" does not mean
>>>> "constant".
>>>
>>> I didn't use the word 'frequently'. It's not me who needs to learn
>>> English. I am not responsible for what you infer from my statements.
>>
>> Duh! Maybe you also need to learn how to read for comprehension. I was
>> the one who said "frequently". You are the one who then took this as
>> "constant" when you said "Did you really think I was saying we were in
>> constant agreement?".
>
> Duh! Maybe you need to learn not to infer things that were never
> implied.

If you make statements that naturally lead to that "inference" because
that is the "implication" most commonly derived, then I have nothing to
learn.

>
> Seriously, though, every exchange really doesn't need to be a
> confrontation...is this a New York thing?

It seems to be more of a South African thing. (BTW, all of my adult
life has been away from New York).

--
Shelly