In [1]:
# contrast-stretching spatial filter
from __future__ import division # float/real division
from matplotlib import pyplot as plt
from matplotlib import image
import numpy as np
%matplotlib inline
img = image.imread('MP.tif')[...,0]
plt.figure(figsize=(12,12))
plt.imshow(img, cmap='gray')
Out[1]:
In [2]:
histogram = np.histogram(img.flatten(), bins=range(256))
plt.xlim((0,255))
plt.plot(histogram[0])
Out[2]:
In [3]:
# page 85 in gw - variables
# use ranges with data from histogram
r1 = 1
s1 = 1
c1 = s1/r1 # slope
# enhance 25-50
r2 = 150
s2 = 254
c2 = (s2-s1)/(r2-r1)
# r2 -> rmax
c3 = (255-s2)/(255-r2)
In [4]:
# mask for values below r1/above r2
below = img < r1
above = img > r2
contrast_img = np.zeros(img.shape)
# rmin -> r1
contrast_img[below] = img[below] * c1
# r1 -> r2, ~ means NOT
contrast_img[~below & ~above] = (img[~below & ~above] - r1) * c2
# r2 -> rmax
contrast_img[above] = (img[above] - r2) * c3 + s2
In [5]:
plt.figure(figsize=(12,12))
plt.imshow(contrast_img, cmap='gray')
Out[5]:
In [6]:
histogram = np.histogram(contrast_img.flatten(), bins=range(256))
plt.xlim((0,255))
plt.plot(histogram[0])
Out[6]:
In [ ]: