Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. import os, glob, sys
  2. import numpy as np
  3. import cv2
  4. import matplotlib.pyplot as plt
  5. import skimage.filters as filters
  6. import seaborn as sns
  7.  
  8. def brightness_and_contrast_adjustment(image, alpha=1.999, beta=-30):
  9. image = alpha * image + beta
  10. image = np.clip(image, 0, 255)
  11. return image.astype(np.uint8)
  12.  
  13. def gamma_correction(image, gamma=2.2):
  14. image = ((image/255)**(1/gamma)) * 255
  15. return image.astype(np.uint8)
  16.  
  17. clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
  18. def preprocess_image(image):
  19. image = brightness_and_contrast_adjustment(image)
  20. kernel = np.ones((2, 2),np.uint8)
  21. image = cv2.erode(image, kernel, iterations=1)
  22. image = clahe.apply(image)
  23. return image.astype(np.uint8)
  24.  
  25. paths = glob.glob("sample/*.jpg")
  26. n_images = 5
  27. fig, ax = plt.subplots(n_images, 2, figsize=(30, 10*n_images))
  28. for i, path in enumerate(paths):
  29. image = cv2.imread(path)
  30. image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  31. preprocessed_image = preprocess_image(image)
  32. ax[i, 0].imshow(image, cmap="gray")
  33. ax[i, 1].imshow(preprocessed_image, cmap="gray")
  34. if i == n_images-1:
  35. break
  36. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement