Performance for image processing in Python

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

Performance for image processing in Python

Son Hua-2
Hi everyone,

I'm quite new Python here. Recently I wrote an image mosaicking
application in Python (using Numpy library). It turns out that the
performance is quite terrible. For example, a mosaic of 7 images, each
800x600, the output size is about 8000x2000. Each output pixel is
sampled from the source images using bilinear interpolation according to
the inverse homography. The running time is nearly 1 hour.

State-of-the-art commercial mosaicking software (written in C) runs the
above examples in less than 10 seconds!!!

I wonder how is other Python image processing applications' performance?
Do we always need to implement slow functions in C? E.g., I suppose to
reimplement the bilinear interpolation in C, as it is called million
times in Python, which is slow.

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

Re: Performance for image processing in Python

Guy K. Kloss-2
On Mon, 19 Apr 2010 14:31:41 Son Hua wrote:

> I'm quite new Python here. Recently I wrote an image mosaicking
> application in Python (using Numpy library). It turns out that the
> performance is quite terrible. [...]
>
> State-of-the-art commercial mosaicking software (written in C) runs the
> above examples in less than 10 seconds!!!
>
> I wonder how is other Python image processing applications' performance?
> Do we always need to implement slow functions in C? E.g., I suppose to
> reimplement the bilinear interpolation in C, as it is called million
> times in Python, which is slow.
Well, as you stated: You seem to be fairly new to Python. Just *using* NumPy
does not guarantee good performance. The trick is to *use it right*. One
example in C is for example also to use the right order of iterating over
multi dimensional arrays to get the right performance. Otherwise you may be
playing against the cache, which is fairly common on huge data structures.

So, one thing you ought to look for in your code is that you're for example
*not* iterate over the elements of an array (pixelx), but use operations that
are to be applied to the whole array at once. You can use these with most
basic and more complex operations, e. g. multiply/divide/add/subtract a
scalar, apply certain NumPy provided functions (g. g. sin() or log()), apply
dot-product multiplication, etc.

Doing that, you can probably eliminate orders of magnitude of slowdown
already.

HTH,

Guy

--
Guy K. Kloss
Institute of Information and Mathematical Sciences
Te Kura Pūtaiao o Mōhiohio me Pāngarau
Massey University, Albany (North Shore City, Auckland)
473 State Highway 17, Gate 1, Mailroom, Quad B Building
voice: +64 9 414-0800 ext. 9266   fax: +64 9 441-8181
[hidden email] http://www.massey.ac.nz/~gkloss

_______________________________________________
Image-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/image-sig

signature.asc (204 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Performance for image processing in Python

Chris Barker
In reply to this post by Son Hua-2
Son Hua wrote:
> I'm quite new Python here. Recently I wrote an image mosaicking
> application in Python (using Numpy library). It turns out that the
> performance is quite terrible. For example, a mosaic of 7 images, each
> 800x600, the output size is about 8000x2000. Each output pixel is
> sampled from the source images using bilinear interpolation according to
> the inverse homography. The running time is nearly 1 hour.
>
> State-of-the-art commercial mosaicking software (written in C) runs the
> above examples in less than 10 seconds!!!

Note that "State-of-the-art" image processing software is likely not
only written in C, but also highly optimized -- taking into account
cache sizes, using special instructions, etc, so you've not going to get
close with naively written code. Still a factor of ten or so maybe.

> I wonder how is other Python image processing applications' performance?
> Do we always need to implement slow functions in C?

in a word, yes -- if you are looping trough pixels in python, it's going
to be painfully slow - python simply is not designed for that kind of thing.

> E.g., I suppose to
> reimplement the bilinear interpolation in C, as it is called million
> times in Python, which is slow.

have you looked at the ndarray package? or PIL's interpolation for that
matter? both are written in C.

If they don't have the algorithm you need, as a another poster pointed
out, smart use of numpy will help a lot (essentially,l you are getting
the inner loop in c via numpy)

If you have not idea how to do that, read up a bit on scipy.org, and
then post a question to the numpy list.

Option 3 is Cython -- it lets you write C code in an almost-python
syntax, and it understand numpy arrays, to it's really easy to re-write
that inner loop in C.

-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