-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilters.py
99 lines (88 loc) · 2.69 KB
/
filters.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
"""
Functions for adding filters to the image
"""
import numpy as np
from math import pi as pi
from color_model import *
from bilateralFilter import bilateralFilterFast
def adjustBrightness(factor, img):
"""
Adjust image brightness
Add factor (-255, 255) to all image pixels and clip in uint8 range
"""
image = np.array(img, copy=True).astype(np.uint32)
image = image + factor
image = np.clip(image, 0, 255)
image = image.astype(np.uint8)
return image
def negativeImage(checker, img):
"""
Reverse the values of all pixels in the image
"""
if checker == False:
return img
else:
image = np.array(img, copy = True).astype(np.uint8)
image = 255 - image
return image
def grayscaleImage(checker, img):
"""
Transform the image to grayscale, a weighted average of r, g, b channels
"""
if checker == False:
return img
else:
image = np.array(img, copy = True).astype(np.uint8)
image[:, :, 0] = image[:, :, 1] = image[:, :, 2] = \
0.2126 * image[:, :, 0] + 0.7152 * image[:, :, 1] + 0.0722*image[:, :, 1]
return image
def adjustContrast(factor, img):
"""
Adjust Contrast: multiply every pixel by factor (0, 2) and clip to uint8 range
"""
image = np.array(img, copy=True).astype(np.uint32)
image = image * factor
image = np.clip(image, 0, 255)
image = image.astype(np.uint8)
return image
def changeHue(factor, img):
"""
Change hue
Must first convert to hls color space
Factor is a number from 0 to 180 and represents the hue shift in degrees
"""
# transform to hls and extract channels
hls = rgb_to_hls(img)
h = hls[:, :, 0]
l = hls[:, :, 1]
s = hls[:, :, 2]
# add factor to hue channel, merge and transform to rgb
hnew = cv2.add(h, factor)
hls = cv2.merge([hnew, l, s])
rgb = hls_to_rgb(hls)
return rgb.astype(np.uint8)
def changeSaturation(factor, img):
"""
Change saturation
Must first convert to hsv color space
Factor is a number from 0 to 2
"""
# transform to hsv and extract channels
hsv = rgb_to_hsv(img)
h = hsv[:, :, 0]
s = hsv[:, :, 1]
v = hsv[:, :, 2]
# multiply saturation channel by factor, merge and transform to hsv
snew = cv2.multiply(s, factor)
hsv = cv2.merge([h, snew, v])
rgb = hsv_to_rgb(hsv)
return rgb.astype(np.uint8)
def bilateralFilter(img, sigma_s, sigma_b):
"""
Bilateral Filter
Call Cython Module to speedup computations
"""
new_img = bilateralFilterFast(img, sigma_s, sigma_b)
new_img = np.asarray(new_img).astype(np.uint8) # float32 to uint8
print("Bilateral Filter Done")
return new_img