【问题标题】:How to crop a 3D image when the point of interest lies on the edge of the image?当兴趣点位于图像边缘时,如何裁剪 3D 图像?
【发布时间】:2018-11-14 01:15:31
【问题描述】:

我有一个大小为512*512*30 的 3D 图像。我还有一个 csv 文件,其中存储了兴趣点。我想在兴趣点周围裁剪大小为32*32*16 的3D 体积,该点位于其中心。我写了以下内容来实现这一点:

block = [32, 32, 16]
img = imageio.volread('path\\to\\tiff\\file')
x, y, z = 191, 303, 17
img_block = img_block[x - int(block[0] / 2):x + int(block[0] / 2),
                              y - int(block[1] / 2):y + int(block[1] / 2),
                              z - int(block[2] / 2):z + int(block[2] / 2)]

这在上述情况下有效,但当我在边缘有一个 x、y、z 点时失败,例如在 z = 28 我得到一个超出范围的错误,这是预期的。

如何避免这个问题并确保顺利完成?

谢谢

【问题讨论】:

  • 答案在一定程度上取决于您想对异常值做什么。处理该问题的典型方法称为“边界检查”。你可以用零填充你的数组,在边界镜像或者只是丢弃值。什么最适合您?
  • 另外:如果一个点在中心,大小应该是奇数而不是偶数。

标签: python numpy image-processing


【解决方案1】:

填充的答案可能如下所示:

import numpy as np

point = [0,1,2]
img = imageio.volread('path\\to\\tiff\\file')
block = [32,32,16]
img_x, img_y, img_z = img.shape
img_padded = np.pad(img, block, 'constant', constant_values=0) #lookup np.pad for other padding options
img_block = img_padded[point[0]-block[0]/2:point[0]+block[0]/2...]

【讨论】:

  • 谢谢,我也尝试过填充。但是在裁剪前填充不会改变 x,y,z 的位置吗?
  • 这个答案对我有用,但我想补充一点,我会将填充量添加到我的坐标中,例如,如果我只使用 npad = ((0, 0), (0, 0), (10, 10))img_padded = np.pad(img_block, pad_width=npad, mode='constant', constant_values=0) 填充 z 维度.这将在 z 维度之前添加 10 行,之后添加 10 行。所以我会在 z 坐标上加 10。
  • 我再次补充说np.pad 有更多选项来处理填充区域。例如。您可以镜像或钳制边缘上的值,如果您处理例如图像重建(以避免强度的突然变化),但对可视化没有多大意义。
猜你喜欢
  • 1970-01-01
  • 2012-03-08
  • 1970-01-01
  • 1970-01-01
  • 2021-06-03
  • 1970-01-01
  • 2020-07-09
  • 2013-03-19
相关资源
最近更新 更多