quick pixelwise euclidean difference

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

quick pixelwise euclidean difference

Lauri Love (nsh)
i'm coding a project which requires quick comparison of two images per pixel by colorspace (distance is the sum of the squares of the differences of RGB values over the pixels of the images), as a fitness function (i am trying to recreate/extend roger alsing's genetic mona lisa code in python).

creating an absolute difference image takes about 1/100th of a second on my (eeepc) hardware, but i can't figure out a way to reduce the difference image to an integer of float value quickly. it takes about the order of 5 seconds to iterate through all the pixels and sum the RGB values to get the manhattan distance. "manually" calculating the manhattan or euclidean distances between the two images is even slower. 

can anyone suggest a way to quickly get either distance from two images?

sincerely and gratefully, 

-nsh

_______________________________________________
Image-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/image-sig
Reply | Threaded
Open this post in threaded view
|

Re: quick pixelwise euclidean difference

Lauri Love (nsh)
Ok, I found a reasonable solution using the ImageStat module to get the sum of pixels of the difference image. This gives the manhattan distance when divided by the pixel count and averaged over RGB.

In [151]: def idiff(im1,im2):
    return array(ImageStat.Stat(ImageChops.difference(im1,im2)).sum).mean()/(im1.size[0] * im1.size[1])
   .....: 

In [153]: timeit("idiff(anna,f37)",20)
Out[153]: 0.023978149890899657

Let me know if there is any faster solution short of coding in some distance functions to the C modules (which I did consider, before be reminded of my pitiful ignorance...)

Yours, 

-nsh

On Sat, Dec 5, 2009 at 7:56 PM, Lauri Love (nsh) <[hidden email]> wrote:
i'm coding a project which requires quick comparison of two images per pixel by colorspace (distance is the sum of the squares of the differences of RGB values over the pixels of the images), as a fitness function (i am trying to recreate/extend roger alsing's genetic mona lisa code in python).

creating an absolute difference image takes about 1/100th of a second on my (eeepc) hardware, but i can't figure out a way to reduce the difference image to an integer of float value quickly. it takes about the order of 5 seconds to iterate through all the pixels and sum the RGB values to get the manhattan distance. "manually" calculating the manhattan or euclidean distances between the two images is even slower. 

can anyone suggest a way to quickly get either distance from two images?

sincerely and gratefully, 

-nsh


_______________________________________________
Image-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/image-sig
Reply | Threaded
Open this post in threaded view
|

Re: quick pixelwise euclidean difference

Chris Barker
Lauri Love (nsh) wrote:
> Let me know if there is any faster solution short of coding in some
> distance functions to the C modules

You might do better with numpy and/or scipy.ndimage.

You can convert a PIL RGB image to a numpy array very easily. I think
it's as simple as:

import numpy as np

arr = np.asarray(pil_image)


Then you have a WxHx3 array of uint8 values you can do all sorts of math on.

-Chris



--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

[hidden email]
_______________________________________________
Image-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/image-sig
Reply | Threaded
Open this post in threaded view
|

bug in PIL 1.1.7 Image.split()

jalopyuser
Just stumbled over this bug.  Shouldn't split() do a load() first?  This
used to work in 1.1.6.

Bill

% python
Python 2.5.1 (r251:54863, Feb  6 2009, 19:02:12)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import Image
>>> Image.VERSION
'1.1.7'
>>> im = Image.open("/u/extensions/vCardParser/vcardimage.png")
>>> im.mode
'RGBA'
>>> im.size
(200, 138)
>>> im.split()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Python/2.5/site-packages/PIL/Image.py", line 1497, in split
    if self.im.bands == 1:
AttributeError: 'NoneType' object has no attribute 'bands'
>>>
_______________________________________________
Image-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/image-sig
Reply | Threaded
Open this post in threaded view
|

Re: bug in PIL 1.1.7 Image.split()

Fredrik Lundh
Good catch. The breakage is caused by a 1.1.7 fix that treats
single-band images as a special case; unfortunately, the fix checks
that attribute before actually loading the image...  I'll provide a
patch later, but you can of course work around it by adding an
explicit load to your code.

Thanks /F

On Thu, Dec 10, 2009 at 10:48 AM, Bill Janssen <[hidden email]> wrote:

> Just stumbled over this bug.  Shouldn't split() do a load() first?  This
> used to work in 1.1.6.
>
> Bill
>
> % python
> Python 2.5.1 (r251:54863, Feb  6 2009, 19:02:12)
> [GCC 4.0.1 (Apple Inc. build 5465)] on darwin
> Type "help", "copyright", "credits" or "license" for more information.
>>>> import Image
>>>> Image.VERSION
> '1.1.7'
>>>> im = Image.open("/u/extensions/vCardParser/vcardimage.png")
>>>> im.mode
> 'RGBA'
>>>> im.size
> (200, 138)
>>>> im.split()
> Traceback (most recent call last):
>  File "<stdin>", line 1, in <module>
>  File "/Library/Python/2.5/site-packages/PIL/Image.py", line 1497, in split
>    if self.im.bands == 1:
> AttributeError: 'NoneType' object has no attribute 'bands'
>>>>
> _______________________________________________
> Image-SIG maillist  -  [hidden email]
> http://mail.python.org/mailman/listinfo/image-sig
>
_______________________________________________
Image-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/image-sig
Reply | Threaded
Open this post in threaded view
|

Re: bug in PIL 1.1.7 Image.split()

Fredrik Lundh
A patch for this can be found here:

    http://hg.effbot.org/pil-2009-raclette/changeset/fb7ce579f5f9

</F>

On Fri, Dec 11, 2009 at 12:55 AM, Fredrik Lundh <[hidden email]> wrote:

> Good catch. The breakage is caused by a 1.1.7 fix that treats
> single-band images as a special case; unfortunately, the fix checks
> that attribute before actually loading the image...  I'll provide a
> patch later, but you can of course work around it by adding an
> explicit load to your code.
>
> Thanks /F
>
> On Thu, Dec 10, 2009 at 10:48 AM, Bill Janssen <[hidden email]> wrote:
>> Just stumbled over this bug.  Shouldn't split() do a load() first?  This
>> used to work in 1.1.6.
>>
>> Bill
>>
>> % python
>> Python 2.5.1 (r251:54863, Feb  6 2009, 19:02:12)
>> [GCC 4.0.1 (Apple Inc. build 5465)] on darwin
>> Type "help", "copyright", "credits" or "license" for more information.
>>>>> import Image
>>>>> Image.VERSION
>> '1.1.7'
>>>>> im = Image.open("/u/extensions/vCardParser/vcardimage.png")
>>>>> im.mode
>> 'RGBA'
>>>>> im.size
>> (200, 138)
>>>>> im.split()
>> Traceback (most recent call last):
>>  File "<stdin>", line 1, in <module>
>>  File "/Library/Python/2.5/site-packages/PIL/Image.py", line 1497, in split
>>    if self.im.bands == 1:
>> AttributeError: 'NoneType' object has no attribute 'bands'
>>>>>
>> _______________________________________________
>> Image-SIG maillist  -  [hidden email]
>> http://mail.python.org/mailman/listinfo/image-sig
>>
>
_______________________________________________
Image-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/image-sig