[Numpy-discussion] adaptive thresholding: get adacent cells for each pixel

Johannes Loehnert a.u.r.e.l.i.a.n at gmx.net
Sat Jun 10 03:19:43 CDT 2006


Hi,

> I'm just starting with numpy (via scipy) and I'm wanting to perform
> adaptive thresholding
> (http://www.cee.hw.ac.uk/hipr/html/adpthrsh.html) on an image.
> Basically that means that I need to get a threshold for each pixel by
> examining the pixels around it. In numpy this translates to finding
> the adjacent cells for each cell (not including the value of the cell
> we are examining) and getting the mean, or median of those cells.
> 
> I've written something that works, but is terribly slow. How would
> someone with more experience get the adjacent cells for each cell
> minus the cell being examined?

regarding the mean value, you can take a look at
scipy.signal.convolve2d. If you convolve with an array like this:

[[0.125 0.125 0.125]
 [0.125 0.0   0.125]
 [0.125 0.125 0.125]]

you get the 3x3 mean value (btw why leave out the center pixel?).

For the median, I can not think of any good method right now.

Also another method springs to my mind (just substract the top row and
add a new bottom row to the averaging window), but I have no idea how to
do this in an efficient way.

Generally, always try to find a way to process the whole array as one.
If you perform anything on an array elementwise, it will be dead slow.


Best regards,
Johannes




More information about the Numpy-discussion mailing list