Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # coding=utf8
- from PIL import ImageFilter, Image, ImageStat, ImageEnhance
- import PIL
- import numpy
- import numpy as np
- import os
- import math
- import cv2
- filepath = '/Users/Ikaros/Downloads/20160729_ζδΈ'
- image_enhance_parameter = {
- "brightness": 2,
- "contrast": 2
- }
- def brightness(image):
- stat = ImageStat.Stat(image)
- if len(stat.mean) == 3:
- r, g, b = stat.mean
- return math.sqrt(0.241 * (r**2) + 0.691 * (g**2) + 0.068 * (b**2))
- else:
- return stat.mean[0]
- def histeq(im,nbr_bins=256):
- # get image histogram
- imhist,bins = numpy.histogram(numpy.array(im).reshape(-1),nbr_bins,normed=True)
- cdf = imhist.cumsum() #cumulative distribution function
- cdf = 255 * cdf / cdf[-1] #normalize
- # use linear interpolation of cdf to find new pixel values
- im2 = numpy.interp(numpy.array(im).reshape(-1),bins[:-1],cdf)
- return im2.reshape(numpy.array(im).shape), cdf
- def denoise_image(image):
- return image.filter(ImageFilter.MedianFilter(3))
- def enhance_image(image):
- # image = image.convert("L")
- contrast_converter = ImageEnhance.Contrast(image)
- image = contrast_converter.enhance(image_enhance_parameter['contrast'])
- brightness_converter = ImageEnhance.Brightness(image)
- image = brightness_converter.enhance(image_enhance_parameter['brightness'])
- return image
- def binarize_image(numpy_array_like_image, threshold=65,
- output_value_above_threshold=None, output_value_below_threshold=None):
- if output_value_above_threshold is None:
- output_value_above_threshold = numpy_array_like_image
- if output_value_below_threshold is None:
- output_value_below_threshold = numpy_array_like_image
- return numpy.where(numpy_array_like_image > threshold,
- output_value_above_threshold, output_value_below_threshold)
- def adjust_gamma(image, gamma=1.0):
- # build a lookup table mapping the pixel values [0, 255] to
- # their adjusted gamma values
- invGamma = 1.0 / gamma
- table = np.array([((i / 255.0) ** invGamma) * 255
- for i in np.arange(0, 256)]).astype("uint8")
- # apply gamma correction using the lookup table
- return cv2.LUT(image, table)
- def main():
- filelist = [os.path.join(filepath, file) for file in os.listdir(filepath)]
- file_count = 1
- for f in filelist:
- if '.jpg' not in f and '.JPG' not in f:
- continue
- img = Image.open(f)
- # car_region for 720x576
- # car_region = (50,240,450,400)
- # car_region for 1024x768
- car_region = (50, 300, 640, 580)
- img = img.crop(car_region)
- # print "The brightness value of", f, "is", brightness(img)
- img = img.convert("L")
- cv_image = numpy.asarray(img)
- # cv_image = cv2.fastNlMeansDenoising(cv_image, None, 5)
- cv2.denoise_TVL1(cv_image, cv_image)
- img = enhance_image(Image.fromarray(cv_image))
- cv_image = adjust_gamma(numpy.asarray(img), 1.5)
- cv2.imwrite(os.path.join(filepath, "%02d" % file_count + ".jpg"), cv_image)
- print f, '-->', os.path.join(filepath, "%02d" % file_count + ".jpg")
- file_count += 1
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement