Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. from __future__ import (division, absolute_import, print_function, unicode_literals)
  2. import glob
  3. import os,sys
  4. import cv2 as cv
  5. import numpy as np
  6. from pandas import DataFrame as df
  7. import pandas as pd
  8. from matplotlib import pyplot as plt
  9.  
  10.  
  11. ########
  12. #Globals
  13. #########
  14.  
  15. img_dir = "path_to_images/*.*"
  16.  
  17. ###########
  18. #Functions
  19. ###########
  20.  
  21. def white_balance(img):
  22. result = cv.cvtColor(img, cv.COLOR_BGR2LAB)
  23. avg_a = np.average(result[:, :, 1])
  24. avg_b = np.average(result[:, :, 2])
  25. result[:, :, 1] = result[:, :, 1] - ((avg_a - 128) * (result[:, :, 0] / 255.0) * 1.1)
  26. result[:, :, 2] = result[:, :, 2] - ((avg_b - 128) * (result[:, :, 0] / 255.0) * 1.1)
  27. result = cv.cvtColor(result, cv.COLOR_LAB2BGR)
  28. return result
  29.  
  30.  
  31. ## Get all the images in the specified dir:
  32. images = sorted(glob.glob(img_dir))
  33.  
  34.  
  35.  
  36.  
  37. #Reading images in folder
  38. for image in images:
  39. img = cv.imread(os.path.join(img_dir, image)) #Read images one by one
  40.  
  41. ################
  42. #Pre-processing#
  43. ################
  44. #Performing white balancing (gray world)
  45. #final = np.hstack((img, white_balance(img))) #white balancing; showing Original and filtered.
  46. final = white_balance(img) #white balance on a single image.
  47.  
  48.  
  49. #Saving images:
  50. i = 0
  51. cv.imwrite("path_to_results/preproc%04i.jpg" %i, final) #save images in sequence (numbering)
  52. i += 1 #numbering variable
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement