将 NRRD 文件转换为 JPG

Converting NRRD file to JPG

我正在尝试将 nrrd 文件转换为 jpg,方法是使用 pynrrd 读取该 nrrd 图像,然后使用我试图形成图像的像素,但我得到的结果是糟糕的对比度图像。以下是我试过的内容

import numpy as np
import nrrd
from PIL import Image
import numpy as np
filename = "/content/drive/MyDrive/CT.nrrd"
readdata, header = nrrd.read(filename)
print(readdata.shape) # (512, 512, 504)
for i in range(504):
  if i == 200:
    img = np.array(readdata[:,:,i])
    print(img.shape)
    print(np.amax(img))
    img = (np.maximum(img, 0) / img.max()) * 255.0
    img = Image.fromarray(np.uint8(img), mode = "L")
    img.save(f'/content/testrgb{i}.png')
    break

我得到的输出是这个

谁能帮我这个

从 PIL 导入图片 将 numpy 导入为 np 导入 nrrd

def manipulating_nrrd_contrast(img, level):
    img_c = img.astype(int).copy()
    factor = (8 * (level+255)) / (255 * (259-level)) #This 8 here is the value that i changes manually by try and test and found out that this works best 
    img_c = factor * (img_c - 128) + 128
    img_c = np.clip(img_c, 0, 255)
    return img_c.astype(np.uint8)

filename = "/content/drive/MyDrive/CT.nrrd"
readdata, header = nrrd.read(filename)
tag_info = get_meta_data(header)
print(tag_info)
print(readdata.shape)
for i in range(readdata.shape[2]):
  b = np.asarray(readdata[:,:,i]).astype(int)
  final = Image.fromarray(manipulating_nrrd_contrast(b, 128))
  final.save(f'/content/png/testrgb{i}.png')

而且,我得到的结果如下