-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolor_processing.py
More file actions
139 lines (110 loc) · 4.14 KB
/
color_processing.py
File metadata and controls
139 lines (110 loc) · 4.14 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt
import os
import shutil
github_path = '/Users/justingubbens/Documents/GitHub/Segmentation_Random_Augmentation'
raw_path = os.path.join('raw_data')
output_path = os.path.join('processed_data')
os.makedirs(output_path, exist_ok=True)
def f_awb_retinex(I0):
# Split the image into its color channels
R0 = I0[:, :, 0].astype(np.float64)
G0 = I0[:, :, 1].astype(np.float64)
B0 = I0[:, :, 2].astype(np.float64)
m, n = R0.shape
# Find maximum values
Rmax = np.max(R0)
Gmax = np.max(G0)
Bmax = np.max(B0)
# Square of maximum values
R2max = Rmax**2
G2max = Gmax**2
B2max = Bmax**2
# Average values
Rave = np.mean(R0)
Gave = np.mean(G0)
Bave = np.mean(B0)
# Square of average values
R2ave = Rave**2
G2ave = Gave**2
B2ave = Bave**2
# Coefficients
Ar = np.array([[R2ave, Rave], [R2max, Rmax]])
Ab = np.array([[B2ave, Bave], [B2max, Bmax]])
B = np.array([Gave, Gmax])
# Solve for xr and xb
xr = np.linalg.solve(Ar, B)
xb = np.linalg.solve(Ab, B)
# Initialize R1 and B1
R1 = np.zeros_like(R0)
B1 = np.zeros_like(B0)
# Apply the transformation
R1 = np.clip(xr[0] * R0**2 + xr[1] * R0, 0, 255)
B1 = np.clip(xb[0] * B0**2 + xb[1] * B0, 0, 255)
# Convert back to uint8
R1 = np.uint8(R1)
B1 = np.uint8(B1)
G1 = np.uint8(G0)
# Stack channels to create the final image
I1 = np.stack([R1, G1, B1], axis=-1)
return I1
# Example usage
def useWhitescale(path):
I0 = cv.imread(path)
I1 = f_awb_retinex(I0)
return I1
# Histogram Equalization
def equalize_color_image(img):
# Split the image into its color channels
channels = cv.split(img)
# Apply histogram equalization to each channel
equalized_channels = [cv.equalizeHist(channel) for channel in channels]
# Merge the equalized channels back into a color image
equalized_img = cv.merge(equalized_channels)
return equalized_img
# Darken an image for segmentation
def darken_color_image(img, factor=0.8):
factor = np.clip(factor, 0, 1)
# Darken the image
darkened_img = np.clip(img * factor, 0, 255).astype(np.uint8)
return darkened_img
def equalize_image_files(folder_path, to_dir):
os.makedirs(to_dir, exist_ok = True)
for root, dirs, files in os.walk(folder_path):
for filename in files:
if filename.endswith('.jpg') or filename.endswith('.png'):
img_path = os.path.join(root, filename)
print(filename)
img = cv.imread(img_path)
whitescaled = useWhitescale(img_path)
equalized = equalize_color_image(whitescaled)
cv.imwrite(f'{to_dir}/{filename}', equalized)
def darken_equalize_image_files(folder_path, to_dir):
os.makedirs(to_dir, exist_ok = True)
for root, dirs, files in os.walk(folder_path):
for filename in files:
if filename.endswith('.jpg') or filename.endswith('.png'):
img_path = os.path.join(root, filename)
print(filename)
img = cv.imread(img_path)
whitescaled = useWhitescale(img_path)
equalized = equalize_color_image(whitescaled)
darkened = darken_color_image(equalized)
cv.imwrite(f'{to_dir}/{filename}', darkened)
def duplicate_text_files(folder_path, to_dir):
os.makedirs(to_dir, exist_ok = True)
for root, dirs, files in os.walk(folder_path):
for filename in files:
if filename.endswith('.txt'):
original_file = os.path.join(root, filename)
new_file = os.path.join(to_dir, filename)
shutil.copy2(original_file, new_file)
#equalize_image_files(raw_path, os.path.join(output_path, 'images'))
#darken_equalize_image_files(raw_path, os.path.join(output_path, 'images'))
#duplicate_text_files(raw_path, os.path.join(output_path, 'labels'))
img_path = 'raw_background.jpg'
img = cv.imread(img_path)
whitescaled = useWhitescale(img_path)
equalized = equalize_color_image(whitescaled)
cv.imwrite('equalized_background.png', equalized)