-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstandard_filter.py
More file actions
104 lines (93 loc) · 2.9 KB
/
standard_filter.py
File metadata and controls
104 lines (93 loc) · 2.9 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import cv2
import numpy as np
import math
'''
完结
目标实现
有滤镜基色卡,转换公式
b, g, r = src_img[i][j]
x = int(g / 4 + int(b / 32) * 64)
y = int(r / 4 + int(b % 32) * 64)
已实现:
凸透镜
凹透镜为C++实现不知道可否连接
'''
def filter_convex(src_img): # 凸透镜,中心点选择原始图片中点
"""
实现凸透镜效果
:param src_img:
:return:
"""
row = src_img.shape[0]
col = src_img.shape[1]
channel = src_img.shape[2]
new_img = np.zeros([row, col, channel], dtype=np.uint8)
center_x = row/2
center_y = col/2
# radius=math.sqrt(center_x*center_x+center_y*center_y)/2
radius = min(center_x, center_y)
for i in range(row):
for j in range(col):
distance = ((i-center_x)*(i-center_x)+(j-center_y)*(j-center_y))
new_dist = math.sqrt(distance)
new_img[i, j, :] = src_img[i, j, :]
if distance <= radius**2:
new_i = np.int(np.floor(new_dist*(i-center_x)/radius+center_x))
new_j = np.int(np.floor(new_dist*(j-center_y)/radius+center_y))
new_img[i, j, :] = src_img[new_i, new_j, :]
cv2.imshow("new", new_img)
cv2.waitKey()
# return new_img
def azimuthAngle(x1, y1, x2, y2):
"""
忘记什么函数
:param x1:
:param y1:
:param x2:
:param y2:
:return:
"""
angle = 0.0
dx = x2 - x1
dy = y2 - y1
if x2 == x1:
angle = math.pi / 2.0
if y2 == y1:
angle = 0.0
elif y2 < y1:
angle = 3.0 * math.pi / 2.0
elif x2 > x1 and y2 > y1:
angle = math.atan(dx / dy)
elif x2 > x1 and y2 < y1:
angle = math.pi / 2 + math.atan(-dy / dx)
elif x2 < x1 and y2 < y1:
angle = math.pi + math.atan(dx / dy)
elif x2 < x1 and y2 > y1:
angle = 3.0 * math.pi / 2.0 + math.atan(dy / -dx)
return angle * 180 / math.pi
def filter_concave(img):
row = img.shape[0]
col = img.shape[1]
channel = img.shape[2]
new_img = np.zeros([row, col, channel], dtype=np.uint8)
center_x = row // 2
center_y = col // 2
for i in range(row):
for j in range(col):
theta = azimuthAngle(center_x, center_y, i, j)
x = [i-center_x, j-center_y]
R = np.linalg.norm(x=x) * 8 # 范数
new_x = center_x + int(R*math.cos(theta))
new_y = center_y + int(R*math.sin(theta))
if new_x < 0: new_x = 0
elif new_x >= col: new_x = col - 1
if new_y < 0: new_y = 0
elif new_y >= row: new_y = row - 1
# 差一点东西不会了
def main():
path = 'D:/study/TP/1.jpg'
src_img = cv2.imread(path)
filter_convex(src_img) # 凸透镜
# filter_concave(src_img) # 凹透镜
if __name__ == '__main__':
main()