Python previews .mha format pictures through the SimpleITK library

python code

import SimpleITK as sitk
import matplotlib.pyplot as plt
from pathlib import Path

def read_and_display_mha(file_path):
# Use SimpleITK library to read MHA file
image = sitk.ReadImage(str(file_path))
# Convert SimpleITK image to NumPy array
image_array = sitk.GetArrayFromImage(image)
plt.imshow(image_array, cmap=' gray')
plt. axis('off')
plt. show()

if name == " main ":
file_path = Path("/Users/mark/ok.mha") # Change to the path address of your mha image
read_and_display_mha(file_path)

Precautions (Note)

The path may not support Chinese

Guess you like

Origin blog.csdn.net/crazyjinks/article/details/131167555
Recommended