Reputation: 6480
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?
Upvotes: 3
Views: 1303
Reputation: 21904
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.
Upvotes: 2
Reputation: 920
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.
Upvotes: 1