You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

寻求Python自动化提取图像条纹及像素值的实现方法

自动化条纹线轮廓提取方案

依赖库安装

先安装所需的Python工具库:

pip install opencv-python numpy matplotlib

完整实现代码

import cv2
import numpy as np
import matplotlib.pyplot as plt

# 1. 加载图像并预处理
img_path = "your_image.png"  # 替换为你的图像路径
img = cv2.imread(img_path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# 降噪+增强条纹对比度(可根据图像调整参数)
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
enhanced = cv2.equalizeHist(blurred)

# 2. 检测条纹位置(水平投影找峰值)
# 计算每行像素总和,定位亮条纹所在行
horizontal_proj = np.sum(enhanced, axis=1)
# 过滤弱峰值,保留有效条纹位置
peak_threshold = np.max(horizontal_proj) * 0.7
stripes_y = np.where(horizontal_proj > peak_threshold)[0]
# 合并相邻行,避免重复检测同一条纹
stripes_y = np.unique(np.round(stripes_y / 10) * 10)  # 10为合并步长,按需调整

# 3. 提取每条条纹的像素值并保存
profile_data = []
for y in stripes_y:
    # 提取当前条纹水平线上的像素值
    line_profile = enhanced[y, :]
    profile_data.append(line_profile)

# 保存为1D数组集合(npy格式方便后续分析)
np.save("stripe_profiles.npy", np.array(profile_data))

# 4. 生成取线与图像的叠加可视化图
plt.figure(figsize=(10, 8))
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB), origin='lower')
# 绘制黄色取线
for y in stripes_y:
    plt.plot([0, img.shape[1]-1], [y, y], color='yellow', linewidth=1)
plt.title("条纹取线叠加验证图")
plt.savefig("overlay_verification.png")
plt.show()

关键调整说明

  • 条纹方向适配:如果你的条纹是垂直方向,只需将水平投影改为垂直投影(np.sum(enhanced, axis=0)),提取stripes_x后绘制垂直线即可。
  • 参数优化:根据图像实际情况调整高斯核大小、峰值阈值、合并步长,确保精准检测所有条纹。
  • 垂直线提取:若需要提取示例图中那样的垂直取线(固定x坐标,覆盖所有条纹),只需循环不同x值,提取enhanced[:, x]作为单条线的像素值。

内容的提问来源于stack exchange,提问作者dartingeyes

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 23:16:11