202610281809
This commit is contained in:
1 parent
bd8f5458bf
commit
f94ef31434
5 files changed
+213
-89
No files matched your search
Vendored
+4
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"python.analysis.autoImportCompletions": true,
|
||||
"python.analysis.languageServerMode": "default"
|
||||
}
|
||||
+58
@@ -0,0 +1,58 @@
|
||||
import cv2
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
# 1. 读取图像 (请把 'spectrum.jpg' 替换成你第一张图的文件名)
|
||||
img_path = r"C:\Users\peng0\Downloads\DownloadsIMG_0438(1).JPG"
|
||||
img = cv2.imread(img_path)
|
||||
|
||||
if img is None:
|
||||
print("找不到图像,请检查路径!")
|
||||
else:
|
||||
# OpenCV 默认读取是 BGR 格式,转换为 RGB 用于显示
|
||||
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
||||
|
||||
# 2. 转换到 HSV 色彩空间,并提取亮度 (Value) 通道
|
||||
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
|
||||
v_channel = hsv[:, :, 2] # 亮度通道,0-255
|
||||
|
||||
# 3. 提取光谱的垂直剖面
|
||||
# 因为光带基本水平,我们选取图像中间的一列(或者中间几列求平均)作为剖面
|
||||
height, width = v_channel.shape
|
||||
mid_col_start = int(width * 0.4)
|
||||
mid_col_end = int(width * 0.6)
|
||||
|
||||
# 对中间这段区域按行求平均,得到一维的亮度分布
|
||||
# 这样能减少单列像素的噪点影响
|
||||
brightness_profile = np.mean(
|
||||
v_channel[:, mid_col_start:mid_col_end], axis=1)
|
||||
|
||||
# 同时也把 RGB 三个通道的剖面提取出来
|
||||
r_profile = np.mean(img_rgb[:, mid_col_start:mid_col_end, 0], axis=1)
|
||||
g_profile = np.mean(img_rgb[:, mid_col_start:mid_col_end, 1], axis=1)
|
||||
b_profile = np.mean(img_rgb[:, mid_col_start:mid_col_end, 2], axis=1)
|
||||
|
||||
# 4. 绘图
|
||||
plt.figure(figsize=(10, 6))
|
||||
|
||||
# 画总亮度曲线
|
||||
plt.plot(brightness_profile, np.arange(height),
|
||||
label='Overall Brightness', color='black', linewidth=2)
|
||||
|
||||
# 画出 RGB 三条通道的强度,展示不同颜色的分布
|
||||
plt.plot(b_profile, np.arange(height),
|
||||
label='Blue Channel', color='blue', alpha=0.7)
|
||||
plt.plot(g_profile, np.arange(height),
|
||||
label='Green Channel', color='green', alpha=0.7)
|
||||
plt.plot(r_profile, np.arange(height),
|
||||
label='Red Channel', color='red', alpha=0.7)
|
||||
|
||||
# 5. 图表设置
|
||||
plt.gca().invert_yaxis() # 图像坐标系 y 轴是向下的,反转一下与图片对应
|
||||
plt.title('Pseudo-Spectrum Analysis of the Captured Image')
|
||||
plt.xlabel('Pixel Intensity (0-255)')
|
||||
plt.ylabel('Vertical Position (Y-axis)')
|
||||
plt.legend()
|
||||
plt.grid(True, alpha=0.3)
|
||||
|
||||
plt.show()
|
||||
+88
-40
@@ -2,6 +2,7 @@ from spectro import wavelen
|
||||
import matplotlib.pyplot as plt
|
||||
from math import pi, cos, acos, sqrt
|
||||
from color import wavelength_to_rgb
|
||||
|
||||
grating_c = 1600 * 10**-9
|
||||
|
||||
arrow_light = []
|
||||
@@ -13,8 +14,8 @@ light_len = 30
|
||||
raw_source_deg = [pi/2, pi*2/3]
|
||||
raw_target_deg = [pi/2, pi*2/3]
|
||||
"""
|
||||
raw_source_deg = [pi/2, pi/3] # beta=90°, gamma=120°
|
||||
raw_target_deg = [pi/2, pi/6] # beta=90°, gamma=100°
|
||||
raw_source_deg = [pi / 2, pi / 3] # beta=90°, gamma=120°
|
||||
raw_target_deg = [pi / 2, pi / 6] # beta=90°, gamma=100°
|
||||
diffraction_order = 1
|
||||
|
||||
|
||||
@@ -25,53 +26,102 @@ def calc_degs(vec):
|
||||
tmp += l**2
|
||||
tmp = sqrt(tmp)
|
||||
for l in vec:
|
||||
degs.append(acos(l/tmp))
|
||||
degs.append(acos(l / tmp))
|
||||
return degs
|
||||
|
||||
|
||||
delta_min = -800
|
||||
delta_max = 1000
|
||||
# deltas = [-712, -641, -499, -475, -433, -367, -294, -
|
||||
# 282, -251, -231, -207, -176, -143, -130, -89, -62, 12, 25] # Ne
|
||||
deltas = [-734, -662, -263, -135, -75, 133, 183, 210, 242, 289,
|
||||
334, 358, 381, 424, 471, 513, 608, 665, 702, 708, 730] # Ar
|
||||
# deltas = [48, 63, 265, 775, 880] # Hg
|
||||
# deltas = range(-600, 600, 1)
|
||||
Ne_deltas = [
|
||||
-712,
|
||||
-641,
|
||||
-499,
|
||||
-475,
|
||||
-433,
|
||||
-367,
|
||||
-294,
|
||||
-282,
|
||||
-251,
|
||||
-231,
|
||||
-207,
|
||||
-176,
|
||||
-143,
|
||||
-130,
|
||||
-89,
|
||||
-62,
|
||||
12,
|
||||
25,
|
||||
] # Ne
|
||||
Ar_deltas = [
|
||||
-734,
|
||||
-662,
|
||||
-263,
|
||||
-135,
|
||||
-75,
|
||||
133,
|
||||
183,
|
||||
210,
|
||||
242,
|
||||
289,
|
||||
334,
|
||||
358,
|
||||
381,
|
||||
424,
|
||||
471,
|
||||
513,
|
||||
608,
|
||||
665,
|
||||
702,
|
||||
708,
|
||||
730,
|
||||
] # Ar
|
||||
Hg_deltas = [48, 63, 265, 775, 880] # Hg
|
||||
H_deltas = [-406, 300, 611, 765]
|
||||
full_deltas = range(delta_min, delta_max)
|
||||
deltas = full_deltas
|
||||
|
||||
for i in deltas:
|
||||
delta_z = i/100
|
||||
delta_z = i / 100
|
||||
|
||||
scattered_source = [
|
||||
sqrt(1-cos(raw_source_deg[0])**2-cos(raw_source_deg[1])**2)*light_len,
|
||||
sqrt(1 - cos(raw_source_deg[0]) ** 2 - cos(raw_source_deg[1]) ** 2) * light_len,
|
||||
cos(raw_source_deg[0]) * light_len,
|
||||
cos(raw_source_deg[1]) * light_len
|
||||
cos(raw_source_deg[1]) * light_len,
|
||||
]
|
||||
scattered_source_deg = calc_degs(scattered_source)
|
||||
arrow_light.append([
|
||||
-scattered_source[0],
|
||||
-scattered_source[1],
|
||||
-scattered_source[2]+delta_z,
|
||||
scattered_source[0],
|
||||
scattered_source[1],
|
||||
scattered_source[2],
|
||||
])
|
||||
arrow_light.append(
|
||||
[
|
||||
-scattered_source[0],
|
||||
-scattered_source[1],
|
||||
-scattered_source[2] + delta_z,
|
||||
scattered_source[0],
|
||||
scattered_source[1],
|
||||
scattered_source[2],
|
||||
]
|
||||
)
|
||||
scattered_target = [
|
||||
-sqrt(1-cos(raw_target_deg[0])**2-cos(raw_target_deg[1])**2)*light_len,
|
||||
-sqrt(1 - cos(raw_target_deg[0]) ** 2 - cos(raw_target_deg[1]) ** 2)
|
||||
* light_len,
|
||||
cos(raw_target_deg[0]) * light_len,
|
||||
cos(raw_target_deg[1]) * light_len - delta_z
|
||||
cos(raw_target_deg[1]) * light_len - delta_z,
|
||||
]
|
||||
scattered_target_deg = calc_degs(scattered_target)
|
||||
arrow_light.append([
|
||||
0,
|
||||
0,
|
||||
delta_z,
|
||||
scattered_target[0],
|
||||
scattered_target[1],
|
||||
scattered_target[2],
|
||||
])
|
||||
arrow_light.append(
|
||||
[
|
||||
0,
|
||||
0,
|
||||
delta_z,
|
||||
scattered_target[0],
|
||||
scattered_target[1],
|
||||
scattered_target[2],
|
||||
]
|
||||
)
|
||||
|
||||
color = wavelength_to_rgb(
|
||||
wavelen(raw_source_deg[1], scattered_target_deg[2], diffraction_order, grating_c))
|
||||
wavelen(
|
||||
raw_source_deg[1], scattered_target_deg[2], diffraction_order, grating_c
|
||||
)
|
||||
)
|
||||
|
||||
colorlist.append(color)
|
||||
colorlist.append(color)
|
||||
@@ -81,21 +131,19 @@ X, Y, Z, U, V, W = zip(*arrow_light)
|
||||
|
||||
fig = plt.figure(figsize=(10, 9))
|
||||
|
||||
# ================= 1. 上方 3D 图 =================
|
||||
ax = fig.add_subplot(211, projection="3d")
|
||||
ax.quiver(X, Y, Z, U, V, W, colors=colorlist,
|
||||
arrow_length_ratio=0, linewidth=1.5)
|
||||
ax.quiver(X, Y, Z, U, V, W, colors=colorlist, arrow_length_ratio=0, linewidth=1.5)
|
||||
|
||||
ax.set_xlim([-light_len, light_len])
|
||||
ax.set_ylim([-light_len, light_len])
|
||||
ax.set_zlim([-light_len, light_len])
|
||||
ax.set_xlabel('X Label')
|
||||
ax.set_ylabel('Y Label')
|
||||
ax.set_zlabel('Z Label')
|
||||
ax.set_xlabel("X Label")
|
||||
ax.set_ylabel("Y Label")
|
||||
ax.set_zlabel("Z Label")
|
||||
|
||||
# ================= 2. 下方离散光谱图 =================
|
||||
plt.rcParams['font.sans-serif'] = ['SimHei']
|
||||
plt.rcParams['axes.unicode_minus'] = False
|
||||
plt.rcParams["font.sans-serif"] = ["SimHei"]
|
||||
plt.rcParams["axes.unicode_minus"] = False
|
||||
|
||||
ax_spec = fig.add_subplot(212)
|
||||
ax_spec.set_title("Spectro")
|
||||
@@ -124,7 +172,7 @@ for d in sorted_deltas:
|
||||
ax_spec.axvline(x=d, color=c, linewidth=4)
|
||||
|
||||
# 标注文字
|
||||
ax_spec.text(d, 1.05, str(d), ha='center', va='bottom', fontsize=12)
|
||||
ax_spec.text(d, 1.05, str(d), ha="center", va="bottom", fontsize=12)
|
||||
|
||||
ax_spec.set_xticks([])
|
||||
ax_spec.set_yticks([])
|
||||
|
||||
@@ -19,7 +19,7 @@ light_len = 36
|
||||
|
||||
|
||||
grating_c = 1600 * 10**-9
|
||||
diffraction_orders = [1]
|
||||
diffraction_orders = [-2, -1]
|
||||
# Order that rended earlier might be covered by other orders
|
||||
# So lower order should be placed at the tail
|
||||
|
||||
@@ -35,46 +35,58 @@ for i in range(0, 3, 1):
|
||||
if (i**2+j**2+k**2 <= 5**2):
|
||||
dirs.append((j, i, k, pi/2, pi/2))
|
||||
"""
|
||||
dirs = [(0, 0, 0, pi/2, pi/3)]
|
||||
dirs = [(0, 0, 0, pi / 2, 10 / 180 * pi)]
|
||||
|
||||
for dir in dirs:
|
||||
beta_i = dir[3]
|
||||
gamma_i = dir[4]
|
||||
cos_alpha_i = sqrt(1 - cos(beta_i)**2 - cos(gamma_i)**2)
|
||||
cos_alpha_i = sqrt(1 - cos(beta_i) ** 2 - cos(gamma_i) ** 2)
|
||||
alpha_i = acos(cos_alpha_i)
|
||||
|
||||
for m in diffraction_orders:
|
||||
dir_range = []
|
||||
for wavelen in wavelens:
|
||||
light_i = LightWaveVec(pi * 2 / wavelen,
|
||||
LightDir(alpha_i, beta_i, gamma_i))
|
||||
light_i = LightWaveVec(pi * 2 / wavelen, LightDir(alpha_i, beta_i, gamma_i))
|
||||
|
||||
colorlist.append((0, 0, 0))
|
||||
arrow_light.append([
|
||||
-cos_alpha_i*light_len,
|
||||
-cos(beta_i)*light_len,
|
||||
-cos(gamma_i)*light_len,
|
||||
cos_alpha_i*light_len,
|
||||
cos(beta_i)*light_len,
|
||||
cos(gamma_i)*light_len,
|
||||
])
|
||||
colorlist.append((1, 1, 1))
|
||||
arrow_light.append(
|
||||
[
|
||||
-cos_alpha_i * light_len,
|
||||
-cos(beta_i) * light_len,
|
||||
-cos(gamma_i) * light_len,
|
||||
cos_alpha_i * light_len,
|
||||
cos(beta_i) * light_len,
|
||||
cos(gamma_i) * light_len,
|
||||
]
|
||||
)
|
||||
light_r_list = diffracted_light(light_i, m, grating_c)
|
||||
|
||||
if (light_r_list):
|
||||
if (len(dir_range) > 1):
|
||||
if light_r_list:
|
||||
if len(dir_range) > 1:
|
||||
dir_range.pop()
|
||||
dir_range.append([(light.direction.alpha*180/pi,
|
||||
light.direction.beta*180/pi,
|
||||
light.direction.gamma*180/pi)
|
||||
for light in light_r_list])
|
||||
dir_range.append(
|
||||
[
|
||||
(
|
||||
light.direction.alpha * 180 / pi,
|
||||
light.direction.beta * 180 / pi,
|
||||
light.direction.gamma * 180 / pi,
|
||||
)
|
||||
for light in light_r_list
|
||||
]
|
||||
)
|
||||
for light_r in light_r_list:
|
||||
|
||||
colorlist.append(wavelength_to_rgb(wavelen))
|
||||
arrow_light .append([dir[0], dir[1], dir[2],
|
||||
cos(light_r.direction.alpha)*light_len,
|
||||
cos(light_r.direction.beta)*light_len,
|
||||
cos(light_r.direction.gamma)*light_len,
|
||||
])
|
||||
arrow_light.append(
|
||||
[
|
||||
dir[0],
|
||||
dir[1],
|
||||
dir[2],
|
||||
cos(light_r.direction.alpha) * light_len,
|
||||
cos(light_r.direction.beta) * light_len,
|
||||
cos(light_r.direction.gamma) * light_len,
|
||||
]
|
||||
)
|
||||
print(dir_range)
|
||||
|
||||
X, Y, Z, U, V, W = zip(*arrow_light)
|
||||
@@ -86,7 +98,7 @@ ax.quiver(X, Y, Z, U, V, W, colors=colorlist, arrow_length_ratio=0)
|
||||
ax.set_xlim([-light_len, light_len])
|
||||
ax.set_ylim([-light_len, light_len])
|
||||
ax.set_zlim([-light_len, light_len])
|
||||
ax.set_xlabel('X Label')
|
||||
ax.set_ylabel('Y Label')
|
||||
ax.set_zlabel('Z Label')
|
||||
ax.set_xlabel("X Label")
|
||||
ax.set_ylabel("Y Label")
|
||||
ax.set_zlabel("Z Label")
|
||||
plt.show()
|
||||
+23
-21
@@ -21,7 +21,9 @@ class LightWaveVec:
|
||||
self.direction = wave_dir
|
||||
|
||||
|
||||
def diffracted_light(incident_light, diffraction_order, grating_constant, is_reflected_grating=True):
|
||||
def diffracted_light(
|
||||
incident_light, diffraction_order, grating_constant, is_reflected_grating=True
|
||||
):
|
||||
ans = []
|
||||
|
||||
in_alpha = incident_light.direction.alpha
|
||||
@@ -39,43 +41,43 @@ def diffracted_light(incident_light, diffraction_order, grating_constant, is_ref
|
||||
if math.fabs(cos_gamma) <= 1 + 1e-12:
|
||||
cos_gamma = max(-1.0, min(1.0, cos_gamma))
|
||||
|
||||
if (math.fabs(cos_gamma) <= 1): # to ensure diffracted light can be solved
|
||||
if math.fabs(cos_gamma) <= 1: # to ensure diffracted light can be solved
|
||||
dif_gamma = math.acos(cos_gamma)
|
||||
cos2_alpha = 1 - math.cos(dif_beta)**2 - math.cos(dif_gamma)**2
|
||||
cos2_alpha = 1 - math.cos(dif_beta) ** 2 - math.cos(dif_gamma) ** 2
|
||||
|
||||
# FLOAT ERROR
|
||||
if cos2_alpha >= -1e-12:
|
||||
cos2_alpha = max(cos2_alpha, 0.0)
|
||||
|
||||
if (cos2_alpha >= 0): # to ensure diffracted light can be solved
|
||||
if cos2_alpha >= 0: # to ensure diffracted light can be solved
|
||||
|
||||
if (is_reflected_grating):
|
||||
dif_alpha = math.acos(- math.copysign(
|
||||
math.sqrt(cos2_alpha), math.cos(in_alpha)))
|
||||
if is_reflected_grating:
|
||||
dif_alpha = math.acos(
|
||||
-math.copysign(math.sqrt(cos2_alpha), math.cos(in_alpha))
|
||||
)
|
||||
else:
|
||||
dif_alpha = math.acos(math.copysign(
|
||||
math.sqrt(cos2_alpha), math.cos(in_alpha)))
|
||||
dif_alpha = math.acos(
|
||||
math.copysign(math.sqrt(cos2_alpha), math.cos(in_alpha))
|
||||
)
|
||||
|
||||
ans.append(LightWaveVec(dif_wavenum, LightDir(
|
||||
dif_alpha,
|
||||
dif_beta,
|
||||
dif_gamma
|
||||
)))
|
||||
ans.append(
|
||||
LightWaveVec(dif_wavenum, LightDir(dif_alpha, dif_beta, dif_gamma))
|
||||
)
|
||||
|
||||
# wavelen = 2pi/wavenum
|
||||
cos_gamma = math.cos(incident_light.direction.gamma) + \
|
||||
diffraction_order \
|
||||
* (2*math.pi / incident_light.wavenum) \
|
||||
/ grating_constant
|
||||
cos_gamma = (
|
||||
math.cos(incident_light.direction.gamma)
|
||||
+ diffraction_order * (2 * math.pi / incident_light.wavenum) / grating_constant
|
||||
)
|
||||
try_diffract(cos_gamma)
|
||||
|
||||
return ans
|
||||
|
||||
|
||||
def wavelen(gamma_i, gamma_r, diffraction_order, grating_constant):
|
||||
return math.fabs(grating_constant
|
||||
/ diffraction_order
|
||||
* (math.cos(gamma_r) - math.cos(gamma_i)))
|
||||
return math.fabs(
|
||||
grating_constant / diffraction_order * (math.cos(gamma_r) - math.cos(gamma_i))
|
||||
)
|
||||
|
||||
|
||||
"""
|
||||
|
||||
Reference in new issue
Block a user