[英]Change entire images slices of NIFTI in Python 3.7
我實際上正在使用 Python 處理 MRI 圖像。 圖像格式是 NIFTI 格式,我知道如何在 x、y 或 z 軸上可視化切片,但是現在,我希望對每個切片使用 Sobel 過濾,並使用這些切片創建新的 NIFTI 圖像。
為了那個原因:
使用子圖,我看到每個切片上的 sobel 過濾工作,但似乎“img_sobel_data[:, :, sl] == np.hypot(sx, sy)”行不起作用,為什么?
這是 lopp 部分:
# Name the interested data
img_data = img.get_fdata()
img_sobel_data = img_sobel.get_fdata()
header = img.header
nb_img = header.get_data_shape()
nb_img_h = nb_img[2] #Hauteur
for sl in range(0,nb_img_h):
slice_h = img_data[:, :, sl]
#Sobel
sx = ndimage.sobel(slice_h, axis=0, mode='constant')
sy = ndimage.sobel(slice_h, axis=1, mode='constant')
sobel_h = np.hypot(sx, sy)
img_sobel_data[:, :, sl] = sobel_h #Change the image slice to the sobel one
# Save Sobel:
nib.save(img_sobel,imSobel_path)
怎么了 ? 我們不能用 Python 替換另一個圖像切片嗎? 有沒有解決這個問題的技巧?
謝謝 !
編輯:好的,我明白了為什么我不能這么輕松地做到這一點:我提取了 NIFTI 圖像的切片,對其進行了過濾,但我沒有更改 NIFTI 圖像本身!!! 所以我現在的問題是:如何更改從 img_sobel.get_fdata() 獲取的 NIFTI 圖像?
僅僅因為你沒有正確保存你的 img_sobel_data,帶有仿射和標題,如果你想保存 Nifti 圖像,你必須在保存它之前提供標題和仿射img_sobel = nib.Nifti1Image(img_sobel_data, affine=img_sobel_affine, header=header)
否則您可以使用帶有cv2.imwrite
cv2 庫將圖像保存為其他格式,以將圖像保存為 JPG 或 PNG 擴展名。
#======================================
# Importing Necessary Libs
#======================================
import nibabel as nib
import numpy as np
from scipy import ndimage, misc
import matplotlib.pyplot as plt
#==============================
img = nib.load(Nifti_img_path)
img_sobel = nib.load(Nifti_img_sobel_path)
#==============================
if True:
# Name the interested data
img_data = img.get_fdata()
img_sobel_data = img_sobel.get_fdata()
img_sobel_affine = img_sobel.affine
header = img.header
nb_img = header.get_data_shape()
nb_img_h = nb_img[2] #Hauteur
for sl in range(0,nb_img_h):
slice_h = img_data[:, :, sl]
#Sobel
sx = ndimage.sobel(slice_h, axis=0, mode='constant')
sy = ndimage.sobel(slice_h, axis=1, mode='constant')
sobel_h = np.hypot(sx, sy)
img_sobel_data[:, :, sl] = sobel_h #Change the image slice to the sobel one
# Save Sobel:
img_sobel = nib.Nifti1Image(img_sobel_data, affine=img_sobel_affine, header=header)
nib.save(img_sobel,imSobel_path)
#==============================
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.