3

Image processing algorithms often require pixel values within a neighborhood of individual image locations. However, the neighborhood for boundary points is incomplete and needs special handling with various methods, e.g., mirror-reflecting, periodic, etc.

For linear operation, an efficient way is to turn the algorithm into a kernel and do 2D convolution. The convolution routine usual has boundary handling methods built in.

For arbitrary (non-linear) algorithms, one possibility is to pad the image first, gather all neighborhoods at once (e.g., with pad+neighbor), and do your operation in a vectorized way. However, this can be memory heavy as the doc suggested.

Loop through pixels and handle boundaries case by case is another way. But this can be slow in Python (though feasible in C/C++), and to provide all this mirror/periodic stuff seems to be cumbersome...

How to do this efficiently (and perhaps Pythonic) when implementing you own algorithms in Python?

And is there some function that returns the neighborhood of a specified pixel, and with specified boundary handling method?

Community
  • 1
  • 1
herrlich10
  • 6,212
  • 5
  • 31
  • 35

2 Answers2

2

The most pythonic and efficient way to do handle boundary conditions during image processing would be to use an existing library. On top of PIL and ImageMagick, I would also recommend OpenCV.

By itself, Python doesn't provide any image processing functions, so any questions about the neighborhood of a specific pixel are library-specific. Pick a library that you like and play around with it - if you can't work out how that library handles boundary conditions, come back and ask again.

mpenkov
  • 21,621
  • 10
  • 84
  • 126
  • 1
    I totally agree with this. Just to add a note - if you really want to do things yourself, you could probably use [numpy](http://www.numpy.org/) which is what underlies OpenCV for python and is a pre-requisite for it. It is more or less as fast as C at doing array manipulations and processing. – KobeJohn Oct 14 '13 at 08:01
  • I'm just wondering what is the general strategy regarding boundary handling when people try to implement pixel-wise neighborhood-based algorithm in Python. The algorithm may not be directly available in existing libraries. I'm aware of numpy, scipy, PIL, as well as Python bindings for opencv and ImageMagick. But often times, the relevant implementation is not written in Python. An inspiring package I found is scikit-image as linked in the question. – herrlich10 Oct 15 '13 at 01:46
  • You already have the right idea: the common strategy is to replicate parts of the image, or include conditional logic to handle boundary conditions. They both have disadvantages (memory usage, efficiency) but if you're really worried about such things, then you probably shouldn't be implementing that in Python in the first place (i.e. use an existing image processing library). – mpenkov Oct 15 '13 at 03:47
1

PIL is one of the best known libraries to handle image processing. You might also want to take a look at ImageMagick, which i think has lots of cool image transformation features inbuilt.

Vivek
  • 910
  • 2
  • 9
  • 26