基于阈值的Numpy 4D阵列掩码二值化

5q4ezhmt  于 2023-08-05  发布在  其他
关注(0)|答案(1)|浏览(76)

我有一个图像,这是一个4D numpy数组。我想根据阈值做一个面具。这是基于如下所示的第一个4D阵列(平均值的40%)。所以我想把低于这个值的所有东西都归零来制作遮罩。我下面有一些代码,但当我有了nifti图像的面具看起来是错误的。有人能指出原因吗?我必须首先将numpy数组规范化为[0 1]吗?谢谢你的好意

mean =  np.mean(im_aray[:,:,:,0])
  threshold = 0.4 * mean_echo1
  idx = im_array[:,:,:,0] < threshold
  mask = np.ones(np.shape(im_array))
  mask[idx,0] = threshold

字符串

ghhkc1vu

ghhkc1vu1#

你必须首先对图像进行归一化,然后计算阈值等等。

im_array = im_array / np.max(im_array)
  mean_echo1 = np.mean(im_array[:, :, :, 0])
  threshold = 0.4 * mean_echo1

  # Create an index array where all the voxels with intensity values below the
  # threshold are set to True.
  idx = im_array[:, :, :, 0] < threshold

  # Create a mask where all the voxels with intensity values below the threshold
  # are set to 0, and all the other voxels are set to 1.
  mask = np.ones(np.shape(im_array))
  mask[idx] = 0

字符串

相关问题