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

Filip Wasilewski filip at ftv.pl
Sat Jun 10 03:13:42 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?

You can get the mean value of surrounding cells by filtering.

import numpy
from scipy import signal
im = numpy.ones((10,10), dtype='d') * range(10)
fi = numpy.ones((3,3), dtype='d') / 8
fi[1,1]=0
print fi
#[[ 0.125  0.125  0.125]
# [ 0.125  0.     0.125]
# [ 0.125  0.125  0.125]]

signal.convolve2d(im, fi, mode='same', boundary='symm') # or correlate2d in this case


Also check help(signal.convolve2d) for information on various
parameters this function takes.


cheers,
fw





More information about the Numpy-discussion mailing list