Skip to content

Instantly share code, notes, and snippets.

@spotter
Created November 7, 2012 15:19
Show Gist options
  • Select an option

  • Save spotter/4032207 to your computer and use it in GitHub Desktop.

Select an option

Save spotter/4032207 to your computer and use it in GitHub Desktop.
Implementation of the noise function
def noise(image):
""" Compute the noise feature.
Parameters
----------
image: ndarray
Image array (uint8 array).
Returns
-------
out: float
The noise feature as described in [1]
[1] N. Hashimoto et al. Referenceless image quality evaluation for whole slide imaging. J Pathol Inform 2012;3:9.
"""
# Define a sliding window
sl = lambda i : slice(i-1, i+2)
# Testing whether the image is RGB or not
nc = lambda im : 3 if len(im.shape)==3 else 1
# Don't use the center value
idx = ([0, 0, 0, 1, 1, 2, 2, 2],
[0, 1, 2, 0, 2, 0, 1, 2])
height, width = image.shape[:2]
d_square = np.zeros(nc(image))
N = 0.
for r in xrange(1, height-1):
for c in xrange(1, width-1):
# Making sure we can have negative values
window = image[sl(r), sl(c)].astype(int)
m = np.min(abs( window - window[1, 1])[idx], axis=0)
d_square += m**2
N += 1.
return np.mean(d_square / N)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment