【问题标题】:get and display one image in a stack of 3 channel images, in a numpy array在 numpy 数组中获取并显示 3 通道图像堆栈中的一个图像
【发布时间】:2018-07-05 08:59:09
【问题描述】:

我有一堆图像。堆栈上的第一个看起来像这样:

import dicom as dc
dcm = dc.read_file('full_stack.dcm')
dcm = dcm.pixel_array
print type(dcm)
print dcm.shape

这给了我

<type 'numpy.ndarray'>

(3, 180, 480, 640)

所以看起来有:

  • 3 个频道
  • 180 张图片
  • 宽度为480
  • 身高640

太棒了。

我的目标是在堆栈中提取图像。然后,我想显示该图像。听起来很简单。

这是我的策略。我希望对此有任何想法/反馈:

1) 获取一张图片。使用基本切片获取堆栈中的第 10 个图像

dcm1 = dcm[0:, 10:11]
dcm1.shape
(3, 1, 480, 640)

2) 要使用plt.imshow 在 pyplot 中实际绘制它,我们需要这个形状:(r, c, channels)。所以我猜想用蛮力把图像打掉。

dcm2 = np.squeeze(dcm1, axis=1)  # throw away the '1'...this makes me nervous
print 'threw away the "1":              ', dcm2.shape
dcm3 = np.swapaxes(dcm2, 0,2)
print 'swapped the first and last dim:  ', dcm3.shape
dcm4 = np.swapaxes(dcm3, 0,1)
print 'swapped the first and second dim:', dcm4.shape

现在,我已经把这张糟糕的图片改成了这样:

threw away the "1":               (3, 480, 640)
swapped the first and last dim:   (640, 480, 3)
swapped the first and second dim: (480, 640, 3)

是时候策划了!什么可能会出错?

imgplot = plt.imshow(dcm4)

这是我得到的:

不知何故,我的图像现在有各种颜色,看起来很糟糕。

我的问题从这里开始——有人知道发生了什么吗?显然,我的方法不成熟且不令人满意。但我不确定该去哪里。

额外的东西,可能并不相关

此时,我尝试将频道缩减为一个,然后复制到三个,以便imshow 可以阅读它,我会为您保存详细信息,但它给了我这个:

【问题讨论】:

  • Stackoverflow 上的问题应该是可重现的,请参阅minimal reproducible example。因此,在提供一些示例数据或至少链接到它时,您可能会找到更多帮助,以便人们可以对此进行测试
  • 如果我没记错的话,dicom 图像通常以 hounsfield 为单位。您确定只在数组中加载 uint8 值吗?
  • 仅用于 CT 还是也用于超声?我得调查一下!谢谢!

标签: python image numpy matplotlib imshow


【解决方案1】:

我对 dicom 图像一无所知,也没有文件可以对此进行测试,但我想一个简单的转置操作应该可以为您提供所需的输出

import numpy as np

a = np.random.rand(3,1,480,640)
b = np.transpose(a[:,0,:,:], axes=[1,2,0])
print (b.shape) # (480L, 640L, 3L)

【讨论】:

  • 使用您的推荐,我仍然可以得到我的 unicorn-puke 图像。我开始认为这不是一个可伸缩的问题。
  • 说到“可伸缩”,Stackoverflow 上的问题应该是可重现的,请参阅minimal reproducible example。因此,在提供一些示例数据或至少链接到它时,您可能会找到更多帮助,以便人们可以对此进行测试。
【解决方案2】:

这个解决方案对我来说很好。请验证输入数组中的值范围是否在 0-255 范围内,并且为 uint8 数据类型。仅尺寸不足以再现结果。例如,以下设置对我来说很好:

In [50]: img = np.random.randint(0, 255, (3, 180, 480, 640), dtype=np.uint8)
In [51]: img1 = np.squeeze(img[:, 9:10])

# proper_img is a *view*; caution while modifying it
In [52]: proper_img = np.moveaxis(img1, source=0, destination=-1)
In [53]: plt.imshow(proper_img)

我得到的情节如下:

【讨论】:

  • 我通过了范围和数据类型测试。我认为我的形象可能已损坏。 :( 非常感谢您的帮助。
  • @MonicaHeddneck 是的,这也很有可能。尝试加载一些其他图像:)
猜你喜欢
  • 1970-01-01
  • 2018-03-21
  • 1970-01-01
  • 2020-04-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-20
  • 1970-01-01
相关资源
最近更新 更多