img_preprocess.py 826 B

12345678910111213141516171819202122232425262728293031
  1. #this script is for image preprocess. ICM is time confused to do
  2. #hsv and rgb stretching, so just replace with rgb stretching,
  3. import cv2
  4. import numpy as np
  5. clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
  6. def gray_stretching(image):
  7. #stretching gray image with single channel.
  8. Imax=np.max(image)
  9. Imin=np.min(image)
  10. MAX=255
  11. MIN=0
  12. image=(image - Imin) / (Imax - Imin)* (MAX - MIN) + MIN
  13. #return image.astype('uint8')
  14. return image
  15. def BGR_stretching(image):
  16. #accept a BGR image read from cv2, split channel and do stretch seperately.
  17. B,G,R=cv2.split(image)
  18. '''
  19. B=gray_stretching(B)
  20. G=gray_stretching(G)
  21. R=gray_stretching(R)
  22. '''
  23. B=clahe.apply(B)
  24. G=clahe.apply(G)
  25. R=clahe.apply(R)
  26. image=cv2.merge([B,G,R]).astype('uint8')
  27. return image