Monday, June 13, 2016

Image Processing - Histogram Equalization - Part 2

Draw Histograms

# draw flattened histogram
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image
%matplotlib inline

im = Image.open("sample.jpg")
im_arr = np.array(im)
print im_arr.shape

plt.figure(figsize=(15,5))
plt.subplot(1,2,1)
plt.imshow(im_arr)
plt.axis('off')

plt.subplot(1,2,2)
im_r = im_arr[:,:,0].flatten() # 3d (r,g,b) to 1d (stacked)
im_g = im_arr[:,:,1].flatten()
im_b = im_arr[:,:,2].flatten()
imhist, bins = np.histogram(im_r, 256, normed=True) #bins are a sequence of data range
plt.plot(imhist, label="Red")
imhist, bins = np.histogram(im_g, 256, normed=True)
plt.plot(imhist, label="Green")
imhist, bins = np.histogram(im_b, 256, normed=True)
plt.plot(imhist, label="Blue")
plt.legend(loc=1)

Histogram Equalization on Grayscale Image

# histogram equalization
def histeq(im, nbr_bins=256):
    imhist,bins = np.histogram(im.flatten(),nbr_bins,normed=True)
    cdf = imhist.cumsum() # cumulative distribution function (cumulative sum)
    cdf = 255 * cdf / cdf[-1] # normalization (brilliant! cdf[-1] is the sum of all)
    im2 = np.interp(im.flatten(), bins[:-1], cdf) # (x', x, fx) linear interpolate x' accordingg to x -> f(x)
    return im2.reshape(im.shape), cdf

im_gray = np.array(im.convert('L')) # interpolate in grayscale
im2, cdf = histeq(im_gray) # increase the dark area

plt.figure(figsize=(15,10))
plt.subplot(2,2,1)
plt.imshow(im_gray,cmap='gray')

plt.subplot(2,2,2)
plt.imshow(im2,cmap='gray')

plt.subplot(2,2,3)
plt.plot(cdf, label="Cumulative Distribution Function")
plt.legend(loc=2)

imhist2, bins2 = np.histogram(im2, 256, normed=True)
plt.subplot(2,2,4)
plt.plot(imhist2, label="Equalized Historgram")
plt.legend(loc=1)

Histogram Equalization on Colored Image

im = Image.open("Luncheon.jpg")
im_arr = np.array(im)

img_r = im_arr[:,:,0]
img_g = im_arr[:,:,1]
img_b = im_arr[:,:,2]

im_r_eq, cdf = histeq(img_r)
im_g_eq, cdf = histeq(img_g)
im_b_eq, cdf = histeq(img_b)

im_eq = np.zeros(im_arr.shape, 'uint8')
print im_r_eq.shape
im_eq[:,:,0] = im_r_eq
im_eq[:,:,1] = im_g_eq
im_eq[:,:,2] = im_b_eq
print im_eq.shape

plt.figure(figsize=(15,10))
plt.subplot(2,3,1)
plt.imshow(im_r_eq, cmap="gray")
plt.axis('off')

plt.subplot(2,3,2)
plt.imshow(im_g_eq, cmap="gray")
plt.axis('off')

plt.subplot(2,3,3)
plt.imshow(im_b_eq, cmap="gray")
plt.axis('off')

ime = Image.fromarray(im_eq)
plt.subplot(2,3,4)
plt.imshow(ime)
plt.axis('off')

plt.subplot(2,3,5)
plt.imshow(im)
plt.axis('off')

Monday, June 6, 2016

Matplotlib: Image Processing - Part 1

Remapping Images

from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

n_row, n_col = 1, 5
plt.figure(figsize=(n_col*3,n_row*3),dpi=100)

img = Image.open("sample.jpg")
plt.subplot(n_row, n_col, 1)
plt.imshow(img)
plt.axis('off')

# gray
img2 = np.array(img.convert('L'))
plt.subplot(n_row, n_col, 2)
plt.imshow(img2, cmap='gray')
plt.axis('off')

# inverse
img3 = 255 - img2
plt.subplot(n_row, n_col, 3)
plt.imshow(img3, cmap='gray')
plt.axis('off')

# map to 100 ... 200
img4 = (100.0/255)*img2 + 100
plt.subplot(n_row, n_col, 4)
plt.imshow(img4, cmap='gray')
plt.axis('off')

# squared
img5 = 255.0 * (img2/255.0) **2
plt.subplot(n_row, n_col, 5)
plt.imshow(img5, cmap='gray')
plt.axis('off')

Plot multiple functions together

from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

f1 = np.array(range(255))
# f1 = np.linspace(0,255,255,endpoint=True)
f2 = 255 - f1
f3 = (100.0/255)*f1 + 100
f4 = 255.0*(f1/255.0)**2

plt.figure(figsize=(8,5))
plt.plot(f1,f1,label="f(x)=x")
plt.plot(f1,f2,label="f(x)=255-x")
plt.plot(f1,f3,label="f(x)=(100.0/255)*x + 100")
plt.plot(f1,f4,label="f(x)=255.0*(x/255.0)**2")
plt.axis([0,260, 0, 260])
plt.legend(loc=2)

print min(f1), max(f1)
print min(f2), max(f2)
print min(f3), max(f3)
print min(f4), max(f4)

Myriad

Visitors