import pylab
from PIL import Image, ImageEnhance
from skimage import img_as_ubyte, img_as_float
import numpy as np
def plot_image(image, title=""):
# pylab.gray()
pylab.title(title, size=20)
pylab.imshow(image, cmap='gray')
# image.show()
pylab.axis('off')
def plot_hist(r, g, b, title=''):
r, g, b = img_as_ubyte(r), img_as_ubyte(g), img_as_ubyte(b)
pylab.hist(np.array(r).ravel(), bins=256,
range=(0, 256), color='r', alpha=0.5)
pylab.hist(np.array(g).ravel(), bins=256,
range=(0, 256), color='g', alpha=0.5)
pylab.hist(np.array(b).ravel(), bins=256,
range=(0, 256), color='b', alpha=0.5)
pylab.xlabel('pixel value', size=20)
pylab.ylabel('frequency', size=20)
pylab.title(title, size=20)
# pylab.axis('off')
im = Image.open('./9781789343731_Code/images/swans.jpg').convert('L')
pylab.imshow(im)
pylab.show()
pylab.hist(np.array(im).ravel(), bins=256, range=(0, 256), color='g')
pylab.xlabel('pixel values')
pylab.ylabel('Frequency')
pylab.title('Histogram')
pylab.show()
pylab.figure(figsize=(12, 18))
# pylab.gray()
pylab.subplot(2, 2, 1)
plot_image(im, 'original image')
pylab.axis('off')
th = [0, 50, 100, 150, 200]
for i in range(2, 5):
im1 = im.point(lambda x: x>th[i] ,'1')
pylab.subplot(2,2,i)
plot_image(im1, 'binary image threshold= ' + str(th[i]))
pylab.show()
im = Image.open('./9781789343731_Code/images/swans.jpg').convert('L')
im = Image.fromarray(np.clip(im + np.random.randint(-128, 128,
(im.height,im.width)),
0, 255).astype(np.uint8))
pylab.figure(figsize=(12, 18))
pylab.subplot(2, 2, 1)
plot_image(im, 'original image with noise')
th = [0, 50, 100, 150, 200]
for i in range(2, 5):
im1 = im.point(lambda x: x>th[i], '1')
pylab.subplot(2,2, i)
plot_image(im1, 'binary image with threshod ' + str(th[i]))
pylab.show()