-
Notifications
You must be signed in to change notification settings - Fork 0
/
solve_phase
159 lines (136 loc) · 6.6 KB
/
solve_phase
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import sys
import numpy as np
from matplotlib import pyplot as plt
import cv2
freq_1 = 1 / 70
freq_2 = 1 / 64
freq_3 = 1 / 59
waveLength_1 = freq_1
waveLength_2 = freq_2
waveLength_3 = freq_3
waveLength_12 = (waveLength_1 * waveLength_2) / (waveLength_2 - waveLength_1)
waveLength_23 = (waveLength_2 * waveLength_3) / (waveLength_3 - waveLength_2)
def getWrapedPhase(imagePath="./cos_pictures/", Period=70, imgType=".bmp"):
initial_phase = [-2 * np.pi / 3, 0, 2 * np.pi / 3]
imagePathList = []
grayImages = []
for each in range(3):
imagePathList.append(imagePath + "cos_wave" + "_Period_" + str(Period) + "_initial_phase_" + str(
initial_phase[each]) + imgType)
print(imagePathList[each])
img = cv2.imread(imagePathList[each])
img = np.asarray(img, dtype="float32")
# cv2.imshow(str(each),img)
if img.ndim == 3:
grayImages.append(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY))
elif img.ndim == 2:
grayImages.append(img)
# WrapedPhase = np.zeros((img.shape[0], img.shape[1]), dtype="float32")
# for i in range(img.shape[0]):
# for j in range(img.shape[1]):
# WrapedPhase[i][j] =np.arctan2(3 ** 0.5 * (grayImages[0][i][j] - grayImages[2][i][j]), (
# 2 * grayImages[1][i][j] - (grayImages[0][i][j] + grayImages[2][i][j])))
WrapedPhase = np.arctan2((3 ** 0.5) * (grayImages[0] - grayImages[2]),
(2 * grayImages[1] - (grayImages[0] + grayImages[2])))
return WrapedPhase
def showWrapedPhaseMap(WrapedPhase, imshowName="0"):
"""
# show WrapedPhaseMap
:param WrapedPhase:
:return:
"""
WrapedPhaseMap = (WrapedPhase + np.pi) * 255 / (2 * np.pi)
img = np.asarray(WrapedPhaseMap, dtype="uint8")
cv2.imshow("WrapedPhaseMap_" + imshowName, img)
def test(WrapedPhase):
initial_phase = 0
x_axis_cos = np.arange(initial_phase,WrapedPhase.shape[1] , 1)
plt.figure("test")
y_axis = WrapedPhase[700:701, :]
plt.plot(x_axis_cos, y_axis.reshape(WrapedPhase.shape[1]))
# plt.plot(x_axis_cos,np.unwrap(y_axis.reshape(912))) ## plt.plot(x_axis_cos,np.unwrap(2*wraped_phase)/2)
def unWrapPhase_lib(WrapedPhase):
"""
此种方法求解方正的图片没什么问题,但对于倾斜的图像存在很大的问题,而大部分的图片都是倾斜的,
因此此方法在实际应用中并不合适。
:param WrapedPhase:
:param Period:
:return:
"""
unWrapedPhase = np.zeros((WrapedPhase.shape[0], WrapedPhase.shape[1]), dtype="float32")
for each in range(WrapedPhase.shape[0]):
unWrapedPhase[each, :] = np.unwrap(WrapedPhase[each, :])
return unWrapedPhase
def showUnWrapedPhaseMap(unWrapedPhase, Period, imshowName="0"):
"""
# show UnWrapedPhaseMap
:param UnWrapedPhase:
:return:
"""
unWrapedPhaseMap = (unWrapedPhase) * 255 / (Period * 2 * np.pi)
img = np.asarray(unWrapedPhaseMap, dtype="uint8")
cv2.imshow("unWrapedPhaseMap_" + str(Period) + "_" + imshowName, img)
def unWrapPhase_multipleFre(WrapedPhase_1, WrapedPhase_2, waveLength_1, waveLength_2, flag=0):
"""
存在一定的问题,最后一列展开相位后为0,按道理应该为最大值,可用 # test(unWrapedPhase_1)测试
:param WrapedPhase_1:
:param WrapedPhase_2:
:param waveLength_1:
:param waveLength_2:
:param flag:
:return:
"""
if (waveLength_1 > waveLength_2):
print("need waveLength_2>waveLength_1")
sys.exit(-1)
unWrapedPhase = np.zeros((WrapedPhase_1.shape[0], WrapedPhase_1.shape[1]), dtype="float32")
if flag == 0:
for i in range(WrapedPhase_1.shape[0]):
for j in range(WrapedPhase_1.shape[1]):
if WrapedPhase_1[i][j] >= WrapedPhase_2[i][j]:
unWrapedPhase[i][j] = (WrapedPhase_1[i][j] - WrapedPhase_2[i][j]) * waveLength_2 / (
waveLength_2 - waveLength_1)
else:
unWrapedPhase[i][j] = (2 * np.pi + WrapedPhase_1[i][j] - WrapedPhase_2[i][j]) * waveLength_2 / (
waveLength_2 - waveLength_1)
elif flag == 1:
for i in range(WrapedPhase_1.shape[0]):
for j in range(WrapedPhase_1.shape[1]):
if WrapedPhase_1[i][j] >= WrapedPhase_2[i][j]:
unWrapedPhase[i][j] = (WrapedPhase_1[i][j] - WrapedPhase_2[i][j]) * waveLength_1 / (
waveLength_2 - waveLength_1)
else:
unWrapedPhase[i][j] = (2 * np.pi + WrapedPhase_1[i][j] - WrapedPhase_2[i][j]) * waveLength_1 / (
waveLength_2 - waveLength_1)
return unWrapedPhase
if __name__ == "__main__":
# WrapedPhase_5 = getWrapedPhase(imagePath="./cos_pictures/", Period=5)
# showWrapedPhaseMap(WrapedPhase_5, Period=5)
# unWrapedPhase=unWrapPhase_lib(WrapedPhase_5, Period=5)
# showUnWrapedPhaseMap(unWrapedPhase,5)
#
#
# WrapedPhase_6 = getWrapedPhase(imagePath="./cos_pictures/", Period=6)
# showWrapedPhaseMap(WrapedPhase_6, Period=6)
# unWrapedPhase_1 = unWrapPhase_multipleFre(WrapedPhase_6,WrapedPhase_5, 912/6,912/5,1)
# showUnWrapedPhaseMap(unWrapedPhase_1, 5,"12")
# test(unWrapedPhase_1)
WrapedPhase_70 = getWrapedPhase(imagePath="./Htf_7_9_2/", Period=70)
showWrapedPhaseMap(WrapedPhase_70, imshowName="70")
WrapedPhase_64 = getWrapedPhase(imagePath="./Htf_7_9_2/", Period=64)
showWrapedPhaseMap(WrapedPhase_64, imshowName="64")
WrapedPhase_59 = getWrapedPhase(imagePath="./Htf_7_9_2/", Period=59)
showWrapedPhaseMap(WrapedPhase_59, imshowName="59")
unWrapedPhase_1_2 = unWrapPhase_multipleFre(WrapedPhase_70, WrapedPhase_64, waveLength_1, waveLength_2, 0)
unWrapedPhase_1_2_normalization = unWrapedPhase_1_2 * (waveLength_2 - waveLength_1) / waveLength_2
unWrapedPhase_2_3 = unWrapPhase_multipleFre(WrapedPhase_64, WrapedPhase_59, waveLength_2, waveLength_3, 0)
unWrapedPhase_2_3_normalization = unWrapedPhase_2_3 * (waveLength_3 - waveLength_2) / waveLength_3
unWrapedPhase_1_2__2_3 = unWrapPhase_multipleFre(unWrapedPhase_1_2_normalization, unWrapedPhase_2_3_normalization,
waveLength_12, waveLength_23, 0)
showUnWrapedPhaseMap(unWrapedPhase_1_2__2_3, 6, "1_2__2_3")
test(unWrapedPhase_1_2__2_3)
# WrapedPhase_70 = getWrapedPhase(imagePath="./Htf_7_9/", Period=70)
# showWrapedPhaseMap(WrapedPhase_70, Period=70)
# unWrapedPhase_1_2__2_3=unWrapPhase_lib(WrapedPhase_70, Period=70)
# showUnWrapedPhaseMap(unWrapedPhase_1_2__2_3, 70, "1_2__2_3")
# test(unWrapedPhase_1_2__2_3)