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

如何遍历循环为图像每列生成Sinogram并实现back projection?

解决方案

修改后的完整代码

import os
import numpy as np
import matplotlib.pyplot as plt
from scipy import ndimage

# 读取所有以"image"开头的图像文件
imagestack = [image for image in os.listdir() if image.startswith("image")]
# 按文件名排序(确保角度顺序正确,比如image0.png到image359.png)
imagestack.sort(key=lambda x: int(x.split("image")[1].split(".")[0]))
images = [plt.imread(img) for img in imagestack]

# --------------------------
# 生成Sinogram
# --------------------------
# 初始化Sinogram数组:(角度数, 投影点数) = (360, 256)
sinogram = np.zeros((len(images), images[0].shape[1]))

for idx, img in enumerate(images):
    # 若图像是RGB格式,转换为灰度图
    if len(img.shape) == 3:
        img = np.mean(img, axis=2)
    # 计算当前角度下所有列的像素和(即投影值)
    projection = np.sum(img, axis=0)
    sinogram[idx] = projection

# 显示Sinogram
plt.figure()
plt.imshow(sinogram, cmap='Greys', aspect='auto')
plt.title('Sinogram')
plt.xlabel('Projection Position')
plt.ylabel('Angle (degrees)')
plt.show()

# --------------------------
# 反投影实现(修复旋转功能)
# --------------------------
# 初始化反投影图像,尺寸与原图一致
back_proj = np.zeros_like(images[0] if len(images[0].shape) == 2 else images[0][:, :, 0])
# 每个图像对应的旋转角度(0到359度)
angles = np.arange(0, 360)

for idx, angle in enumerate(angles):
    # 获取当前角度的投影值
    proj = sinogram[idx]
    # 将投影值扩展为256×256的图像:每一行重复投影值,模拟投影的列分布
    proj_img = np.tile(proj, (back_proj.shape[0], 1))
    # 旋转图像:负角度将投影图像转回原图方向,reshape=False保持尺寸,order=1使用双线性插值
    rotated_proj = ndimage.rotate(proj_img, -angle, reshape=False, order=1)
    # 叠加到反投影图像
    back_proj += rotated_proj

# 归一化反投影图像,便于显示
back_proj = back_proj / np.max(back_proj)

# 显示反投影结果
plt.figure()
plt.imshow(back_proj, cmap='Greys')
plt.title('Back Projection Result')
plt.show()

关键修改说明

1. 生成完整Sinogram

  • 原代码仅提取每张图的第一列,改为计算每张图所有列的像素和,得到对应角度的完整投影,最终堆叠成360×256的Sinogram(行=角度,列=投影位置)。
  • 增加RGB转灰度的逻辑,避免多通道图像导致的维度错误。
  • 对图像文件名排序,确保角度顺序从0到359度连续,避免Sinogram角度混乱。

2. 修复旋转与反投影

  • 移除冗余的np.linspace角度生成,直接使用0-359度的连续角度序列。
  • 将单一行的投影值扩展为与原图同尺寸的图像(np.tile实现),确保旋转操作的对象是二维图像而非一维列向量。
  • 修正旋转方向:使用负角度将投影图像旋转回原图的方向,同时设置reshape=False保持图像尺寸一致,避免变形。
  • 实现反投影的叠加逻辑,最后对结果归一化,保证显示效果正常。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 14:40:20