Repository navigation
Expand file tree
/
Copy pathchapter1.py
More file actions
142 lines (126 loc) · 4.64 KB
/
Copy pathchapter1.py
File metadata and controls
142 lines (126 loc) · 4.64 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
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
131
132
133
134
135
136
137
138
139
140
141
142
import cv2
import numpy as np
the_img = cv2.imread("lena.ppm")
gray_img = cv2.imread("lena.ppm", cv2.IMREAD_GRAYSCALE)
cv2.namedWindow("OriginImage")
cv2.imshow("OriginImage", the_img)
cv2.imshow("OriginImage", gray_img)
cv2.waitKey(1000)
print("ori", gray_img)
print("start!")
# 输出图像
def print_img(img, name):
cv2.namedWindow(name)
cv2.imshow(name, img)
cv2.waitKey(5000)
print("finished!")
# 降低图像灰度级数
def Grayscale_modify(img, target_scale):
print("start modification")
size = img.shape
scale = 2**8 // 2**target_scale
scale_matrix = np.full(size, scale, dtype=np.uint8)
# print(img)
dst_img = img // scale_matrix * scale_matrix
dst_img = dst_img.astype(np.int8)
# print(dst_img)
print_img(dst_img, "grayscale decrease")
# Grayscale_modify(gray_img, 3)
# 图像内插
# 最近邻内插法
def nearest_neighbor_interpolation(img, times):
print("start interpolation")
size = img.shape
dst_size = (int(size[0] * times), int(size[1] * times))
dst = np.zeros(dst_size, dtype=np.uint8)
for i in range(dst_size[0]):
for j in range(dst_size[1]):
if (i/times) - (i / times // 1) > 0.5:
dst_x = int(i / times // 1) + 1
else:
dst_x = int(i / times // 1)
if (j / times) - (j / times // 1) > 0.5:
dst_y = int(j / times // 1) + 1
else:
dst_y = int(j / times // 1)
dst[i][j] = img[dst_x][dst_y]
if dst[i, j] < 0:
print(i, j, dst_x, dst_y)
exit()
print(dst[0][0], dst[0][0] > 0)
print_img(dst, "nearest neighbor interpolation")
print("nni", dst)
# nearest_neighbor_interpolation(gray_img, 1.5)
# 双线性内插
def bilinear_interpolation(img, times):
print("start interpolation")
size = img.shape
dst_size = (int(size[0] * times), int(size[1] * times))
dst = np.zeros(dst_size, dtype=np.uint8)
for i in range(dst_size[0]):
for j in range(dst_size[1]):
ori_x = i / times
ori_y = j / times
if ori_x > size[0] - 1:
ori_x -= 1
if ori_y > size[1] - 1:
ori_y -= 1
neighbors = np.zeros((2,2), dtype=np.int)
neighbors[0][0], neighbors[0][1], neighbors[1][0], \
neighbors[1][1] = int(img[int(ori_x)][int(ori_y)]), \
int(img[int(ori_x)][int(ori_y)+1]), int(img[int(ori_x)+1][int(ori_y)]), \
int(img[int(ori_x)+1][int(ori_y)+1])
r1 = abs(neighbors[1][0] - neighbors[0][0]) * (ori_x - int(ori_x)) + neighbors[0][0]
r2 = abs(neighbors[1][1] - neighbors[1][0]) * (ori_x - int(ori_x)) + neighbors[1][0]
dst_value = (r2 - r1) * (ori_y - int(ori_y)) + r1
dst[i][j] = dst_value
print("bili", dst)
print(np.shape(dst))
print_img(dst, "bilinear_interpolation")
# bilinear_interpolation(gray_img, 1.5)
# 图像平均
def add_salt_noise(img, percentage):
row, column = img.shape
noise_salt = np.random.randint(0, 256, (row, column))
noise_salt = np.where(noise_salt < percentage * 256, 255, 0)
img.astype("float")
noise_salt.astype("float")
salt = img + noise_salt
salt = np.where(salt > 255, 255, salt)
salt.astype("uint8")
print_img(img, "noised")
return img
def img_average_denoise(img):
salt_noise_percentage = 0.04
img = add_salt_noise(img, salt_noise_percentage)
img.astype(np.uint32)
img_shape = img.shape
for i in range(1, img_shape[0] - 1):
for j in range(1, img_shape[1] - 1):
counter = 0
sum = img[i, j]
for x in [-1, 0, 1]:
for y in [-1, 0, 1]:
if i + x >= 0 & j + y >=0:
sum += img[i+x, j+y]
counter += 1
img[i, j] = img[i, j] // counter
img.astype(np.uint8)
print_img(img, "average")
# img_average_denoise(gray_img)
# 仿射变换
def affine_transformation_flip(img, flip_angle):
img_shape = img.shape
affine_matix = [[np.cos(flip_angle), np.sin(flip_angle), 0],
[-np.sin(flip_angle), np.cos(flip_angle), 0],
[0, 0, 1]]
dst_img = np.zeros(img_shape, dtype=np.uint8)
for i in range(img_shape[0]):
for j in range(img_shape[1]):
transfom_matrix = np.array([i, j, 1])
dst_matrix = transfom_matrix.dot(affine_matix)
dst_img[int(dst_matrix[0])][int(dst_matrix[1])] = img[i][j]
print_img(dst_img, "affine transformation flip")
affine_transformation_flip(gray_img, np.pi/2)
# 图像配准
# 傅里叶变换