我正在尝试将nii文件转换为png文件,但我需要更多的对比png。有办法这样做吗?
我的代码:
import numpy as np
import os # Traverse folders
import nibabel as nib #nii Format 1 This bag will be used in general
import imageio # Convert to an image
def nii_to_image(file, pos, mode):
patient_name = file.split('/')[-1].split('.')[0]
if mode == 'mask':
save_to = 'dataset/Сегментация артерии/МАСКА/png/' + patient_name
if mode == 'ct':
save_to = 'dataset/Сегментация артерии/КТ/png/' + patient_name
if not os.path.exists(save_to):
os.mkdir(save_to) # New Folder
img = nib.load(file) # Read nii
img_fdata = img.get_fdata()
# Contrast
if mode == 'ct':
img_fdata = img_fdata - np.min(img_fdata)
img_fdata = (img_fdata/np.max(img_fdata)) * 255
# Start converting to an image
(x,y,z) = img.shape
for i in range(z): #z Is a sequence of images
if pos == 'ax':
silce = img_fdata[:, :, i] # You can choose which direction of slice
imageio.imwrite(os.path.join(save_to,'{}.png'.format(i)), silce)
# Save an image

发布于 2022-06-06 18:10:31
解决了。
def change_contrast(img, brightness, contrast):
img = np.int16(img)
img = img * (contrast/127+1) - contrast + brightness
img = np.clip(img, 0, 255)
img = np.uint8(img)
return img
contrasted = change_contrast(img=img, brightness=-160, contrast=600)https://stackoverflow.com/questions/72520599
复制相似问题