-
Notifications
You must be signed in to change notification settings - Fork 1
/
ImageProcessing.py
77 lines (68 loc) · 2.61 KB
/
ImageProcessing.py
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
from skimage import img_as_ubyte
from skimage.exposure import cumulative_distribution
from PIL import Image, ImageEnhance
import numpy as np
import cv2
class Processing:
def channel_correction(img, channel= (0, 1, 2), correction= (1, 1, 1)):
img[:, :, channel] = img[:, :, channel] * correction
return img
def __linear_distribution(image, channel):
image_intensity = img_as_ubyte(image[:,:,channel])
freq, bins = cumulative_distribution(image_intensity)
target_bins = np.arange(255)
target_freq = np.linspace(0, 1, len(target_bins))
new_vals = np.interp(freq, target_freq, target_bins)
linear_dist = new_vals[image_intensity].astype(np.uint8)
return linear_dist
def auto_enhance(image):
img = image.copy()
for channel in range(3):
img[:,:,channel] = Processing.__linear_distribution(img, channel)
img = ImageEnhance.Brightness(Image.fromarray(img)).enhance(0.5)
img = ImageEnhance.Contrast(img).enhance(2)
img = ImageEnhance.Sharpness(img).enhance(10)
return np.array(img)
def changeBrightness(img,value):
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
h, s, v = cv2.split(hsv)
if value >= 0:
lim = 255 - value
v[v > lim] = 255
v[v <= lim] += value
else:
lim = abs(value)
v[v < lim] = 0
v[v >= lim] -= abs(value)
final_hsv = cv2.merge((h, s, v))
img = cv2.cvtColor(final_hsv, cv2.COLOR_HSV2BGR)
return img
def changeBlur(img,value):
kernel_size = (value+1,value+1)
img = cv2.blur(img,kernel_size)
return img
def changeContrast(img,value):
lab= cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
l_channel, a, b = cv2.split(lab)
clahe = cv2.createCLAHE(clipLimit=((value/100)+1))
cl = clahe.apply(l_channel)
limg = cv2.merge((cl,a,b))
img = cv2.cvtColor(limg, cv2.COLOR_LAB2BGR)
return img
def changeSharpness(img,value):
img = ImageEnhance.Sharpness(Image.fromarray(img)).enhance((value + 100) / 100)
return np.array(img)
def changeSaturation(img,value):
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
h, s, v = cv2.split(hsv)
if value >= 0:
lim = 255 - value
s[s > lim] = 255
s[s <= lim] += value
else:
lim = abs(value)
s[s < lim] = 0
s[s >= lim] -= abs(value)
final_hsv = cv2.merge((h, s, v))
img = cv2.cvtColor(final_hsv, cv2.COLOR_HSV2BGR)
return img