繁体   English   中英

MRI 图像的 3D 图像中心裁剪

[英]Center cropping of 3D images for MRI images

我有一个 3D MRI 图像列表,我想将这些图像裁剪到中心,以便只留下 ROI,关于如何做到这一点的任何建议?

我尝试了以下代码,但它最后返回了原始大小......知道如何将所有不同尺寸的图像裁剪到中心吗?

path1=(r"\*.*")
#ALL after resliced and resized 

#path_2(r"C")
#Reading multiple files ED images for all pateints 1 to 72 and reslice them to 3D. 
dimensions_img =np.zeros((len(glob.glob(path1)),3)) # this help to as counter 
for i, img in enumerate(sorted(glob.glob(path1))): 
    print(img)
    a= nib.load(img) 
    img_data = a.get_data()    
    #print(a.shape)
    if len(a.shape)== 4:# if the shape is 4
      resliced = img_data[:,:,:,0]
      print(resliced.shape)
      dimensions_img[i,:]=resliced.shape
      #print(dimensions_img)  
    #print(resliced.shape)
    #dimensions_img[i,:]=resliced.shape   
    #print(dimensions_img)      
    #new_dim = np.max(dimensions_img,axis=0).astype(int)
    #center = (new_dim/2).astype(int)      
    #new_image_zeros[int(center[0] - resliced.shape[0]/2):int(resliced.shape[0]/2+center[0]),int(center[1] - resliced.shape[1]/2):int(resliced.shape[1]/2+center[1]),int(center[2] - resliced.shape[2]/2):int(resliced.shape[2]/2+center[2])] =  resliced 

您可以尝试此功能并遍历您的 3D MRI 体积以应用它。

def crop3D(path_scan):
    start = (90,90) # You change the values here to fit well your ROI 
    end = (290,290)
    slices = tuple(map(slice, start, end))
    return scan[slices]

到数据集的链接会很好,但发布单个图像也会有帮助:让每个人都更容易复制您的问题。

通常使用 numpy 图像,您应该能够执行以下操作:

cropped_image = your_image[crop_y_start_index:cropy_y_stop_index,crop_x_start_index:crop_x_stop_index]

它可能取决于图像的加载方式/图像 numpy 数组的形状。

您需要根据图像尺寸(和裁剪大小)偏移开始/结束索引。

一旦您可以裁剪一张图像,请将其包装在一个可重复使用的函数中,以便您可以在需要时为每个切片应用不同的裁剪尺寸。

我建议放慢速度,进一步解决问题,然后自信地前进:

  1. 加载单个图像/切片
  2. 裁剪图像的一部分(可以是左上角的 50x50 区域:很容易计算出image[0:50,0:50]
  3. 如果按预期工作:很好,开始计算开始/结束 x 和 y 裁剪坐标以从中心裁剪,否则在基本裁剪测试失败的地方进行调试。 一旦您可以轻松地从中心裁剪具有给定大小的切片,循环遍历所有切片,根据需要调整裁剪区域并且裁剪应该是直截了当的🤞。

(如果以后您需要使用 PyTorch 中的数据,值得一试Kornia (它在许多有用的功能中具有centre_crop功能)(并且可以直接以 Tensor 格式工作))

更新重新阅读您的代码我注意到您正在使用NiBabel库。 查看图像切片参考,我看到您可以直接使用 numpy 获得类似的结果,但是我看到 NiBabel 图像具有确定图像元素的世界坐标的仿射变换 我建议阅读本教程,然后将相同的数据应用于单个数据切片,并在理想情况下仔细检查仿射数据是否在此过程中丢失。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM