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)

No comments:

Post a Comment

Myriad

Visitors