首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将nii转换为png时的对比问题

将nii转换为png时的对比问题
EN

Stack Overflow用户
提问于 2022-06-06 16:19:40
回答 1查看 79关注 0票数 0

我正在尝试将nii文件转换为png文件,但我需要更多的对比png。有办法这样做吗?

我的代码:

代码语言:javascript
运行
复制
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 

EN

回答 1

Stack Overflow用户

发布于 2022-06-06 18:10:31

解决了。

代码语言:javascript
运行
复制
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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72520599

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档