-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
130 lines (93 loc) · 3.02 KB
/
main.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
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
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib.widgets import Slider
import cv2 as cv
FILE_NAME = 'res/mountain-and-lake.jpg'
# https://matplotlib.org/3.3.1/gallery/widgets/slider_demo.html
# https://sodocumentation.net/matplotlib/topic/6983/animations-and-interactive-plotting
# img:
# image in rbg
#
# satadj:
# 1.0 means no change. Under it converts to greyscale
# and about 1.5 is immensely high
def saturate(img, satadj):
imghsv = cv.cvtColor(img, cv.COLOR_RGB2HSV).astype("float32")
(h, s, v) = cv.split(imghsv)
s = s*satadj
s = np.clip(s,0,255)
imghsv = cv.merge([h,s,v])
imgrgb = cv.cvtColor(imghsv.astype("uint8"), cv.COLOR_HSV2RGB)
# assume: return rgb
return imgrgb
def brightness(img, exp_adj):
imghsv = cv.cvtColor(img, cv.COLOR_RGB2HSV).astype("float32")
(h, s, v) = cv.split(imghsv)
v = v*exp_adj
v = np.clip(v,0,255)
imghsv = cv.merge([h,s,v])
imgrgb = cv.cvtColor(imghsv.astype("uint8"), cv.COLOR_HSV2RGB)
# assume: return rgb
return imgrgb
def plt_hist(ax, img, color):
colors = ['b', 'g', 'r']
k = colors.index(color)
histogram = cv.calcHist([img],[k],None,[256],[0,256])
plt_handle, = ax.plot(histogram, color=color)
return plt_handle
def main():
fig, ax = plt.subplots(1, 2,figsize=(27.0,27.0))
ax1 = ax[0] # The histogram
ax2 = ax[1] # The image
ax2.set_xlim(0.0,1280.0)
fig.suptitle('Image toner', fontsize=16)
# Calculate the initial value for the image
img = cv.imread(cv.samples.findFile(FILE_NAME)) # assume: BGR
img = cv.cvtColor(img, cv.COLOR_BGR2RGB) # plt assumes RGB
# Draw the image
# Take the handle for later
imobj = ax2.imshow(img)
# Axes for the saturation and brightness
ax_sat = plt.axes([0.25, .03, 0.50, 0.02])
ax_exp = plt.axes([0.25, 0.01, 0.50, 0.02])
# Slider
sat_slider = Slider(ax_sat, 'Saturation', 0, 20, valinit=1)
exp_slider = Slider(ax_exp, 'Brightness', -10, 10, valinit=1)
# Histogram
colors = ('r', 'g', 'b')
lines = []
for k,color in enumerate(colors):
histogram = cv.calcHist([img],[k],None,[256],[0,256])
line, = ax1.plot(histogram,color=color)
lines.append(line)
def update_sat(val):
newimg = img
# update image
newimg = saturate(newimg, val)
newimg = brightness(newimg, exp_slider.val)
imobj.set_data(newimg)
# update also the histogram
colors = ('r', 'g', 'b')
for k,color in enumerate(colors):
histogram = cv.calcHist([newimg],[k],None,[256],[0,256])
lines[k].set_ydata(histogram)
# redraw canvas while idle
fig.canvas.draw_idle()
def update_exp(val):
newimg = img
newimg = saturate(newimg, sat_slider.val)
newimg = brightness(newimg, val)
imobj.set_data(newimg)
# update also the histogram
colors = ('b', 'g', 'r')
for k,color in enumerate(colors):
histogram = cv.calcHist([newimg],[k],None,[256],[0,256])
lines[k].set_ydata(histogram)
# redraw canvas while idle
fig.canvas.draw_idle()
# call update function on slider value change
sat_slider.on_changed(update_sat)
exp_slider.on_changed(update_exp)
plt.show()
main()