Advertisement
Guest User

Untitled

a guest
Aug 8th, 2016
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PyCon 3.27 KB | None | 0 0
  1. # coding=utf8
  2. from PIL import ImageFilter, Image, ImageStat, ImageEnhance
  3. import PIL
  4. import numpy
  5. import numpy as np
  6. import os
  7. import math
  8. import cv2
  9.  
  10. filepath = '/Users/Ikaros/Downloads/20160729_ζ™šδΈŠ'
  11. image_enhance_parameter = {
  12.     "brightness": 2,
  13.     "contrast": 2
  14. }
  15.  
  16.  
  17. def brightness(image):
  18.     stat = ImageStat.Stat(image)
  19.     if len(stat.mean) == 3:
  20.         r, g, b = stat.mean
  21.         return math.sqrt(0.241 * (r**2) + 0.691 * (g**2) + 0.068 * (b**2))
  22.     else:
  23.         return stat.mean[0]
  24.  
  25.  
  26. def histeq(im,nbr_bins=256):
  27.  
  28.     # get image histogram
  29.     imhist,bins = numpy.histogram(numpy.array(im).reshape(-1),nbr_bins,normed=True)
  30.     cdf = imhist.cumsum() #cumulative distribution function
  31.     cdf = 255 * cdf / cdf[-1] #normalize
  32.  
  33.     # use linear interpolation of cdf to find new pixel values
  34.     im2 = numpy.interp(numpy.array(im).reshape(-1),bins[:-1],cdf)
  35.  
  36.     return im2.reshape(numpy.array(im).shape), cdf
  37.  
  38.  
  39. def denoise_image(image):
  40.     return image.filter(ImageFilter.MedianFilter(3))
  41.  
  42.  
  43. def enhance_image(image):
  44.     # image = image.convert("L")
  45.     contrast_converter = ImageEnhance.Contrast(image)
  46.     image = contrast_converter.enhance(image_enhance_parameter['contrast'])
  47.  
  48.     brightness_converter = ImageEnhance.Brightness(image)
  49.     image = brightness_converter.enhance(image_enhance_parameter['brightness'])
  50.  
  51.     return image
  52.  
  53.  
  54. def binarize_image(numpy_array_like_image, threshold=65,
  55.                    output_value_above_threshold=None, output_value_below_threshold=None):
  56.     if output_value_above_threshold is None:
  57.         output_value_above_threshold = numpy_array_like_image
  58.     if output_value_below_threshold is None:
  59.         output_value_below_threshold = numpy_array_like_image
  60.     return numpy.where(numpy_array_like_image > threshold,
  61.                        output_value_above_threshold, output_value_below_threshold)
  62.  
  63.  
  64. def adjust_gamma(image, gamma=1.0):
  65.     # build a lookup table mapping the pixel values [0, 255] to
  66.     # their adjusted gamma values
  67.     invGamma = 1.0 / gamma
  68.     table = np.array([((i / 255.0) ** invGamma) * 255
  69.                       for i in np.arange(0, 256)]).astype("uint8")
  70.  
  71.     # apply gamma correction using the lookup table
  72.     return cv2.LUT(image, table)
  73.  
  74.  
  75. def main():
  76.     filelist = [os.path.join(filepath, file) for file in os.listdir(filepath)]
  77.     file_count = 1
  78.     for f in filelist:
  79.         if '.jpg' not in f and '.JPG' not in f:
  80.             continue
  81.         img = Image.open(f)
  82.  
  83.         # car_region for 720x576
  84.         # car_region = (50,240,450,400)
  85.  
  86.         # car_region for 1024x768
  87.         car_region = (50, 300, 640, 580)
  88.         img = img.crop(car_region)
  89.         # print "The brightness value of", f, "is", brightness(img)
  90.  
  91.  
  92.         img = img.convert("L")
  93.  
  94.         cv_image = numpy.asarray(img)
  95.         # cv_image = cv2.fastNlMeansDenoising(cv_image, None, 5)
  96.         cv2.denoise_TVL1(cv_image, cv_image)
  97.  
  98.         img = enhance_image(Image.fromarray(cv_image))
  99.  
  100.         cv_image = adjust_gamma(numpy.asarray(img), 1.5)
  101.         cv2.imwrite(os.path.join(filepath, "%02d" % file_count + ".jpg"), cv_image)
  102.  
  103.         print f, '-->', os.path.join(filepath, "%02d" % file_count + ".jpg")
  104.         file_count += 1
  105.  
  106.  
  107. if __name__ == '__main__':
  108.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement