import os from PIL import Image, ImageOps, ImageFilter, ImageStat import numpy as np
def channel_split(image): imageR, imageG, imageB = image.split() x, y = image.size Rchannel = np.zeros((y, x, 3), dtype="uint8") Bchannel = np.zeros((y, x, 3), dtype="uint8") Gchannel = np.zeros((y, x, 3), dtype="uint8")
Rchannel[:, :, 0] = imageR; Bchannel[:, :, 1] = imageG; Gchannel[:, :, 2] = imageB;
Rchannel = Image.fromarray(Rchannel) Bchannel = Image.fromarray(Bchannel) Gchannel = Image.fromarray(Gchannel)
def compensate_RB(image, flag): imager, imageg, imageb = image.split() minR, maxR = imager.getextrema() minG, maxG = imageg.getextrema() minB, maxB = imageb.getextrema()
imageR = np.array(imager, np.float64) imageG = np.array(imageg, np.float64) imageB = np.array(imageb, np.float64) x, y = image.size
for i in range(0, y): for j in range(0, x): imageR[i][j] = (imageR[i][j] - minR) / (maxR - minR) imageG[i][j] = (imageG[i][j] - minG) / (maxG - minG) imageB[i][j] = (imageB[i][j] - minB) / (maxB - minB)
meanR = np.mean(imageR) meanG = np.mean(imageG) meanB = np.mean(imageB)
if flag == 0: for i in range(y): for j in range(x): imageR[i][j] = int((imageR[i][j] + (meanG - meanR) * (1 - imageR[i][j]) * imageG[i][j]) * maxR) imageB[i][j] = int((imageB[i][j] + (meanG - meanB) * (1 - imageB[i][j]) * imageG[i][j]) * maxB)
for i in range(0, y): for j in range(0, x): imageG[i][j] = int(imageG[i][j] * maxG)
if flag == 1: for i in range(y): for j in range(x): imageR[i][j] = int((imageR[i][j] + (meanG - meanR) * (1 - imageR[i][j]) * imageG[i][j]) * maxR)
for i in range(0, y): for j in range(0, x): imageB[i][j] = int(imageB[i][j] * maxB) imageG[i][j] = int(imageG[i][j] * maxG)
compensateIm = np.zeros((y, x, 3), dtype="uint8") compensateIm[:, :, 0] = imageR; compensateIm[:, :, 1] = imageG; compensateIm[:, :, 2] = imageB;
compensateIm = Image.fromarray(compensateIm)
return compensateIm
def gray_world(image): imager, imageg, imageb = image.split() imagegray = image.convert('L') imageR = np.array(imager, np.float64) imageG = np.array(imageg, np.float64) imageB = np.array(imageb, np.float64) imageGray = np.array(imagegray, np.float64) x, y = image.size
meanR = np.mean(imageR) meanG = np.mean(imageG) meanB = np.mean(imageB) meanGray = np.mean(imageGray)
for i in range(0, y): for j in range(0, x): imageR[i][j] = int(imageR[i][j] * meanGray / meanR) imageG[i][j] = int(imageG[i][j] * meanGray / meanG) imageB[i][j] = int(imageB[i][j] * meanGray / meanB)
whitebalancedIm = np.zeros((y, x, 3), dtype="uint8") whitebalancedIm[:, :, 0] = imageR; whitebalancedIm[:, :, 1] = imageG; whitebalancedIm[:, :, 2] = imageB;
return Image.fromarray(whitebalancedIm)
def sharpen(wbimage, original): smoothed_image = wbimage.filter(ImageFilter.GaussianBlur) smoothedr, smoothedg, smoothedb = smoothed_image.split() imager, imageg, imageb = wbimage.split() imageR = np.array(imager, np.float64) imageG = np.array(imageg, np.float64) imageB = np.array(imageb, np.float64) smoothedR = np.array(smoothedr, np.float64) smoothedG = np.array(smoothedg, np.float64) smoothedB = np.array(smoothedb, np.float64) x, y = wbimage.size
for i in range(y): for j in range(x): imageR[i][j] = 2 * imageR[i][j] - smoothedR[i][j] imageG[i][j] = 2 * imageG[i][j] - smoothedG[i][j] imageB[i][j] = 2 * imageB[i][j] - smoothedB[i][j]
sharpenIm = np.zeros((y, x, 3), dtype="uint8") sharpenIm[:, :, 0] = imageR; sharpenIm[:, :, 1] = imageG; sharpenIm[:, :, 2] = imageB;
return Image.fromarray(sharpenIm)
def hsv_global_equalization(image): hsvimage = image.convert('HSV') Hue, Saturation, Value = hsvimage.split() equalizedValue = ImageOps.equalize(Value, mask=None) x, y = image.size equalizedIm = np.zeros((y, x, 3), dtype="uint8") equalizedIm[:, :, 0] = Hue; equalizedIm[:, :, 1] = Saturation; equalizedIm[:, :, 2] = equalizedValue; hsvimage = Image.fromarray(equalizedIm, 'HSV') rgbimage = hsvimage.convert('RGB')
return rgbimage
def average_fusion(image1, image2): image1r, image1g, image1b = image1.split() image2r, image2g, image2b = image2.split() image1R = np.array(image1r, np.float64) image1G = np.array(image1g, np.float64) image1B = np.array(image1b, np.float64) image2R = np.array(image2r, np.float64) image2G = np.array(image2g, np.float64) image2B = np.array(image2b, np.float64) x, y = image1R.shape
for i in range(x): for j in range(y): image1R[i][j] = int((image1R[i][j] + image2R[i][j]) / 2) image1G[i][j] = int((image1G[i][j] + image2G[i][j]) / 2) image1B[i][j] = int((image1B[i][j] + image2B[i][j]) / 2)
fusedIm = np.zeros((x, y, 3), dtype="uint8") fusedIm[:, :, 0] = image1R; fusedIm[:, :, 1] = image1G; fusedIm[:, :, 2] = image1B;
return Image.fromarray(fusedIm)
def process_image(image_path, output_folder): image = Image.open(image_path) compensated_image = compensate_RB(image, flag=0) gray_world_image = gray_world(image) sharpened_image = sharpen(gray_world_image, image) hsv_equalized_image = hsv_global_equalization(sharpened_image) fused_image = average_fusion(compensated_image, hsv_equalized_image)
output_path = os.path.join(output_folder, os.path.basename(image_path)) fused_image.save(output_path)
def process_images_in_folder(input_folder, output_folder): for filename in os.listdir(input_folder): if filename.lower().endswith(('.png', '.jpg', '.jpeg', '.tiff', '.bmp', '.gif')): image_path = os.path.join(input_folder, filename) process_image(image_path, output_folder)
input_folder = '/Users/wuleihuan/Desktop/testimages' output_folder = '/Users/wuleihuan/Desktop/testimages/augmented'
if not os.path.exists(output_folder): os.makedirs(output_folder)
process_images_in_folder(input_folder, output_folder)
|