-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathImageProcessing.py
55 lines (48 loc) · 1.88 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
import numpy as np
import cv2
def find_nearest_above(my_array, target):
diff = my_array - target
mask = np.ma.less_equal(diff, -1)
# We need to mask the negative differences
# since we are looking for values above
if np.all(mask):
c = np.abs(diff).argmin()
return c # returns min index of the nearest if target is greater than any value
masked_diff = np.ma.masked_array(diff, mask)
return masked_diff.argmin()
def hist_match(original, specified):
oldshape = original.shape
original = original.ravel()
specified = specified.ravel()
# get the set of unique pixel values and their corresponding indices and counts
s_values, bin_idx, s_counts = np.unique(original, return_inverse=True,return_counts=True)
t_values, t_counts = np.unique(specified, return_counts=True)
# Calculate s_k for original image
s_quantiles = np.cumsum(s_counts).astype(np.float64)
s_quantiles /= s_quantiles[-1]
# Calculate s_k for specified image
t_quantiles = np.cumsum(t_counts).astype(np.float64)
t_quantiles /= t_quantiles[-1]
# Round the values
sour = np.around(s_quantiles*255)
temp = np.around(t_quantiles*255)
# Map the rounded values
b=[]
for data in sour[:]:
b.append(find_nearest_above(temp,data))
b= np.array(b,dtype='uint8')
return b[bin_idx].reshape(oldshape)
def blend_with_mask_matrix(src1, src2, mask):
res_channels = []
for c in range(0, src1.shape[2]):
a = src1[:, :, c]
b = src2[:, :, c]
m = mask[:, :]
#Alpha blending
res = cv2.add(
cv2.multiply(b, cv2.divide(np.full_like(m, 255) - m, 255.0, dtype=cv2.CV_32F), dtype=cv2.CV_32F),
cv2.multiply(a, cv2.divide(m, 255.0, dtype=cv2.CV_32F), dtype=cv2.CV_32F),
dtype=cv2.CV_8U)
res_channels += [res]
res = cv2.merge(res_channels)
return res