-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMLBfilter.py
More file actions
81 lines (69 loc) · 2.4 KB
/
MLBfilter.py
File metadata and controls
81 lines (69 loc) · 2.4 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
import cv2
import numpy as np
'''
完结
实现:
1.铅笔画、彩铅画、卡通画、复古滤镜
2.夕阳滤镜
'''
def hue(img): # 各种滤镜
w = img.shape[0]
h = img.shape[1]
# 改变这三个值改变色调
b = 1
g = 0.8
r = 0.8
for i in range(w):
for j in range(h):
img[i, j, 0] = int(img[i, j, 0] * b) if int(img[i, j, 0] * b) <= 255 else 255
img[i, j, 1] = int(img[i, j, 1] * g) if int(img[i, j, 1] * g) <= 255 else 255
img[i, j, 2] = int(img[i, j, 2] * r) if int(img[i, j, 2] * r) <= 255 else 255
return img
def old_pic(image):
"""
复古风格
:param image:
:return:
"""
rows, cols, channals = image.shape
for r in range(rows):
for c in range(cols):
B = image.item(r, c, 0)
G = image.item(r, c, 1)
R = image.item(r, c, 2)
image[r, c, 0] = np.uint8(min(max(0.272 * R + 0.534 * G + 0.131 * B, 0), 255))
image[r, c, 1] = np.uint8(min(max(0.349 * R + 0.686 * G + 0.168 * B, 0), 255))
image[r, c, 2] = np.uint8(min(max(0.393 * R + 0.769 * G + 0.189 * B, 0), 255))
return image
def pencil(image): # 铅笔画
dst_gray, dst_color = cv2.pencilSketch(image, sigma_s=50, sigma_r=0.15, shade_factor=0.04)
return dst_gray, dst_color
def cartoon(image): # 卡通画
dst_comic = cv2.stylization(image, sigma_s=60, sigma_r=0.07)
return dst_comic
if __name__ == '__main__':
path = 'D:/study/TP/1.jpg'
save_path = 'D:/study/TP/res/1res.jpg'
img = cv2.imread(path)
img_filter = hue(img) # 滤镜 通过该变rgb数值实现
cv2.namedWindow("res", cv2.WINDOW_NORMAL)
cv2.imshow("res", img_filter)
cv2.waitKey(0)
# img_res = cartoon(img) # 卡通画
# cv2.namedWindow("res", cv2.WINDOW_NORMAL)
# cv2.imshow("res", img_res)
# cv2.waitKey(0)
#
# res_gray, img_color = pencil(img) # 铅笔画 与 有色铅笔画
# cv2.namedWindow("res", cv2.WINDOW_NORMAL)
# cv2.imshow("res", res_gray)
# cv2.waitKey(0)
# cv2.namedWindow("res", cv2.WINDOW_NORMAL)
# cv2.imshow("res", img_color)
# cv2.waitKey(0)
#
# res_old = old_pic(img) # 复古风格
# cv2.namedWindow("res", cv2.WINDOW_NORMAL)
# cv2.imshow("res", res_old)
# cv2.waitKey(0)
# cv2.imwrite(save_path, img_color)