-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimageProcessing.py
More file actions
86 lines (71 loc) · 3.2 KB
/
imageProcessing.py
File metadata and controls
86 lines (71 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
from cv2 import cv2
import numpy as np
import math
import glob
import os
import shutil
from PIL import Image, ImageEnhance
def gamma_correct(imgs_direc,processed_imgs):
#'imgs_direc' directory where the photos without correction are
#'processed_imgs' directory where the photos with the correction are going to be saved
i=0
if not os.path.exists(processed_imgs):
os.mkdir(processed_imgs)
acum = 0
for file in os.listdir(imgs_direc):
filename = f"{imgs_direc}/{i}.jpg"
image = cv2.imread(filename)
# Converts the photos from RGB to HSV and calculates the avarage V value for each photo
# to later apply the gamma correction needed
hsvImage = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
v_avg = cv2.mean(hsvImage)
nameProcessed = f"{processed_imgs}/{i}.jpg"
if round(v_avg[2]) >= 150:
gamma_corr_img = np.array(255 * (image / 255)**1.1, dtype='uint8')
cv2.imwrite(nameProcessed, gamma_corr_img)
elif round(v_avg[2]) < 150 and round(v_avg[2]) >= 90:
gamma_corr_img = image
cv2.imwrite(nameProcessed, gamma_corr_img)
elif round(v_avg[2]) < 90 and round(v_avg[2]) >= 85:
gamma_corr_img = np.array(255 * (image / 255)**0.95, dtype='uint8')
cv2.imwrite(nameProcessed, gamma_corr_img)
elif round(v_avg[2]) < 85 and round(v_avg[2]) >= 70:
gamma_corr_img = np.array(255 * (image / 255)**0.9, dtype='uint8')
cv2.imwrite(nameProcessed, gamma_corr_img)
elif round(v_avg[2]) < 70 and round(v_avg[2]) >= 60:
gamma_corr_img = np.array(255 * (image / 255)**0.85, dtype='uint8')
cv2.imwrite(nameProcessed, gamma_corr_img)
elif round(v_avg[2]) < 60 and round(v_avg[2]) >= 50:
gamma_corr_img = np.array(255 * (image / 255)**0.8, dtype='uint8')
cv2.imwrite(nameProcessed, gamma_corr_img)
elif round(v_avg[2]) < 50 and round(v_avg[2]) >= 55:
gamma_corr_img = np.array(255 * (image / 255)**0.75, dtype='uint8')
cv2.imwrite(nameProcessed, gamma_corr_img)
else:
gamma_corr_img = np.array(255 * (image / 255)**0.7, dtype='uint8')
cv2.imwrite(nameProcessed, gamma_corr_img)
print('Brightness: ', round(v_avg[2]), 'Photo:', i)
acum = acum + round(v_avg[2])
i = i + 1
def Histogram_EQ(processed_imgs,eq_direc):
if not os.path.exists(eq_direc):
os.mkdir(eq_direc)
i = 0
for file in os.listdir(processed_imgs):
print('Equalizing photo: ',i)
filename = f"{processed_imgs}/{i}.jpg"
img_eq = cv2.imread(filename, 1)
img_eq = cv2.cvtColor(img_eq, cv2.COLOR_HSV2RGB)
R, G, B = cv2.split(img_eq)
#apply CLAHE to each RGB channel
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
output2_R = clahe.apply(R)
output2_G = clahe.apply(G)
output2_B = clahe.apply(B)
#Merge the channels again
img_eq = cv2.merge((output2_R, output2_G, output2_B))
img = Image.open(filename)
nameEq = f"{eq_direc}/{i}.jpg"
img.save(nameEq)
i +=1
shutil.rmtree(processed_imgs)