如何利用强度颜色映射筛选偏振颜色映射数据并叠加其渐变?
实现带强度筛选的偏振图叠加强度渐变
核心实现思路
- 给强度数组设置阈值,过滤掉低强度区域,生成掩码标记有效区域
- 基于掩码只保留偏振图中有效区域的内容
- 将强度数组归一化后,作为透明度或亮度因子叠加到偏振图上,让强度变化直观体现
用Matplotlib直接处理数组(适合原始数据)
如果你的强度和偏振都是numpy数组,直接用matplotlib就能实现需求:
import numpy as np import matplotlib.pyplot as plt from matplotlib.colors import Normalize # 替换成你自己的n×n数组 n = 200 intensity = np.random.rand(n, n) * 0.8 + 0.2 # 示例强度数组(范围0.2-1.0) polarization = np.random.rand(n, n) * 180 # 示例偏振角度数组(0-180°) # 1. 设置强度阈值,生成掩码 intensity_threshold = 0.3 mask = intensity > intensity_threshold # 2. 归一化强度,用于控制偏振图的透明度 norm_intensity = Normalize(vmin=intensity_threshold, vmax=intensity.max())(intensity) # 3. 绘制叠加图 fig, ax = plt.subplots(figsize=(8, 8)) # 绘制偏振图,用mask过滤低强度区域,强度归一化值控制透明度 im = ax.imshow(polarization, cmap='hsv', alpha=norm_intensity) im.set_data(np.where(mask, polarization, np.nan)) # 低强度区域设为NaN,不显示 # 添加颜色条 cbar_pol = plt.colorbar(im, ax=ax, fraction=0.046, pad=0.04) cbar_pol.set_label('偏振角度 (°)') # 添加强度参考色条 cbar_int = plt.colorbar(plt.cm.ScalarMappable(norm=Normalize(vmin=intensity_threshold, vmax=intensity.max()), cmap='gray'), ax=ax, fraction=0.046, pad=0.1) cbar_int.set_label('强度 (归一化)') # 隐藏冗余坐标轴 ax.set_xticks([]) ax.set_yticks([]) plt.tight_layout() plt.show()
关键细节说明
- 偏振图用
hsv色图最适配角度数据,色相对应偏振角度,直观易读 - 用
np.where把低于阈值的区域设为NaN,matplotlib会自动隐藏这些区域 - 强度归一化后作为
alpha参数,强度越高偏振图越清晰,反之越透明,完美叠加强度渐变效果
用PIL处理已保存的图像(适合已有图片文件)
如果你的强度和偏振已经是图片文件,用PIL库处理更直接:
from PIL import Image import numpy as np # 加载图片(替换成你的文件路径) intensity_img = Image.open('intensity.png').convert('L') # 转灰度图 polarization_img = Image.open('polarization.png').convert('RGB') # 转numpy数组 intensity_arr = np.array(intensity_img) polarization_arr = np.array(polarization_img) # 设置灰度阈值(0-255,根据你的强度图调整) threshold = 50 mask = intensity_arr > threshold # 归一化强度到0-1,用于控制透明度 norm_intensity = (intensity_arr - threshold) / (255 - threshold) norm_intensity[~mask] = 0 # 低强度区域透明度直接设为0 # 生成透明度通道并添加到偏振图 alpha_channel = (norm_intensity * 255).astype(np.uint8) polarization_with_alpha = Image.fromarray(polarization_arr).convert('RGBA') polarization_with_alpha.putalpha(alpha_channel) # 保存或展示结果 polarization_with_alpha.save('polarization_with_intensity.png') polarization_with_alpha.show()
内容的提问来源于stack exchange,提问作者Robin Dumora Beyriere
相关产品推荐
相关产品推荐

