gpt4 book ai didi

python - 当兴趣点位于图像边缘时如何裁剪 3D 图像?

转载 作者:太空宇宙 更新时间:2023-11-03 21:35:03 26 4
gpt4 key购买 nike

我有一个尺寸为 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 处,我收到了超出范围的错误,这是预期的。

如何避免这个问题并确保顺利复制?

谢谢

最佳答案

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

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...]

关于python - 当兴趣点位于图像边缘时如何裁剪 3D 图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53286025/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com