簡體   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