Exemple #1
0
def reconstruct_image(stacked_img, ori_shape, epoch, save_folder_name):
    stacked_img = stacked_img.cpu().data.numpy()
    stacked_img_shape = np.shape(stacked_img)
    in_size = np.shape(stacked_img)[1:]
    pdsz = 20

    crop_n1, crop_n2, crop_n3 = cal_crop_num(ori_shape, in_size)
    div_arr = division_array(stacked_img_shape[1:], crop_n1, crop_n2, crop_n3,
                             ori_shape) + 0.000001

    img_cont_np = image_concatenate(stacked_img, crop_n1, crop_n2, crop_n3,
                                    ori_shape[0], ori_shape[1], ori_shape[2])
    img_cont = img_cont_np / div_arr
    img_cont_np = img_cont.astype('float')
    # pdsz = 30
    img_cont_np = img_cont_np[pdsz:-pdsz, pdsz:-pdsz, pdsz:-pdsz]
    # organize images in every epoch
    desired_path = save_folder_name + '/epoch_' + str(epoch) + '/'
    # Create the path if it does not exist
    if not os.path.exists(desired_path):
        os.makedirs(desired_path)
    # Save Image!
    export_name = str(epoch) + '_dis.nii.gz'
    save_array_as_nii_volume(img_cont_np, desired_path + export_name)

    return img_cont
Exemple #2
0
def save_com_img2nii(pred, lab, save_path):

    lab[lab == 255] = 5
    pred[pred == 255] = 1
    re = lab - pred
    re[re == 255] = 3
    re[re == 4] = 2
    re[re == 5] = 1

    print(np.unique(re[:]))
    save_array_as_nii_volume(re, save_path)
Exemple #3
0
def save_prediction_image(stacked_img,
                          ori_shape,
                          im_name,
                          epoch,
                          indate_it=0,
                          save_folder_name="result_images",
                          save_im=True):
    """save images to save_path
    Args:
        stacked_img (numpy): stacked cropped images
        save_folder_name (str): saving folder name
        division_array(388, 2, 3, 768, 1024):
    """
    stacked_img = stacked_img.cpu().data.numpy()
    stacked_img_shape = np.shape(stacked_img)
    in_size = np.shape(stacked_img)[1:]
    stacked_size = stacked_img_shape[0]
    pdsz = 20

    crop_n1, crop_n2, crop_n3 = cal_crop_num(ori_shape, in_size)

    div_arr = division_array(stacked_img_shape[1:], crop_n1, crop_n2, crop_n3,
                             ori_shape) + 0.000001
    img_cont_np = image_concatenate(stacked_img, crop_n1, crop_n2, crop_n3,
                                    ori_shape[0], ori_shape[1], ori_shape[2])

    probability = img_cont_np / div_arr
    # print(np.unique(probability))
    probability = probability[pdsz:-pdsz, pdsz:-pdsz, pdsz:-pdsz]
    desired_path = save_folder_name + '/epoch_' + str(epoch) + '/'
    # Create the path if it does not exist
    if not os.path.exists(desired_path):
        os.makedirs(desired_path)
    # Save Image!
    export_name = str(epoch) + '_pro.nii.gz'
    save_array_as_nii_volume(probability, desired_path + export_name)

    img_cont_np = img_cont_np.astype('uint8')
    img_cont_np = polarize((img_cont_np) / div_arr) * 255
    img_cont_np = img_cont_np[..., pdsz:-pdsz, pdsz:-pdsz, pdsz:-pdsz]
    # organize images in every epoch
    if indate_it == 0:
        desired_path = save_folder_name + '/epoch_' + str(epoch) + '/'
    else:
        desired_path = save_folder_name + '/iter_' + str(epoch) + '/'
    # Create the path if it does not exist
    if not os.path.exists(desired_path):
        os.makedirs(desired_path)
    # Save Image!
    export_name = str(epoch) + '.nii.gz'
    save_array_as_nii_volume(img_cont_np, desired_path + export_name)

    return img_cont_np
Exemple #4
0
def save_img2nii(img_path, save_path):

    img_arr = glob.glob(str(img_path) + str("/*"))
    img_arr.sort()
    single_image_name = img_arr[0]
    img_as_img = Image.open(single_image_name)
    img_as_np = np.asarray(img_as_img).astype('uint8')
    all_lab = np.zeros((img_as_np.shape[0], img_as_np.shape[1], len(img_arr)))

    for i in range(len(img_arr)):
        single_image_name = img_arr[i]
        img_as_img = Image.open(single_image_name)
        img_as_np = np.asarray(img_as_img).astype('uint8')
        all_lab[:, :, i] = img_as_np

    # img_as_img.show()

    save_array_as_nii_volume(np.transpose(all_lab, [2, 0, 1]), save_path)
Exemple #5
0
import nibabel, glob
import numpy as np
from util.tools_self import save_array_as_nii_volume, load_nifty_volume_as_array
from PIL import Image
from metrics import *
from matplotlib import pyplot as plt
import scipy.misc

pdsz = 30
save_filename = 'train_yz_proximity.nii.gz'
img_path = 'oriCvLab/train_lab_c2_yz'

img_arr = glob.glob(str(img_path) + "/*")
img_arr.sort()

img = np.zeros((165, 768 // 2, 1024 // 2)).astype('uint8')
#  lab = np.zeros((165,1024//2,768//2)).astype('uint8')

for i in range(len(img_arr)):
    img_as_img = Image.open(img_arr[i])
    # img_as_img.show()
    img_as_np = np.asarray(img_as_img).astype('uint8')
    # img_as_np = scipy.misc.imresize(img_as_np, 0.5)
    img[:, :, i] = img_as_np

img = np.pad(img, pdsz, mode='symmetric')

save_array_as_nii_volume(np.transpose(img, [2, 0, 1]), save_filename)
Exemple #6
0
print("pro1, dice is:", dice, "jac is:", jac)
pro2 = load_nifty_volume_as_array(flip_2_path)
# 165  *  512
pro2 = np.transpose(pro2, [1, 0, 2])
print(pro2.shape)
dice, jac = dice_coeff((pro2 >= 0.5).astype('uint8') * 255, target)
print("pro2, dice is:", dice, "jac is:", jac)
pro3 = load_nifty_volume_as_array(flip_3_path)
pro3 = np.transpose(pro3, [1, 2, 0])
print(pro3.shape)
dice, jac = dice_coeff((pro3 >= 0.5).astype('uint8') * 255, target)
print("pro3, dice is:", dice, "jac is:", jac)

desired_path = './history/'
pro = pro1 + pro2 + pro3
pro = pro / 3
export_name = 'aug_test_pro' + '.nii.gz'
save_array_as_nii_volume(pro, desired_path + export_name)
export_name = 'aug_test' + '.nii.gz'
pred = pro >= 0.5
pred = pred.astype('uint8')
save_array_as_nii_volume(pred, desired_path + export_name)

pred[pred == 1] = 255
print(np.unique(pred))

dice, jac = dice_coeff(pred, target)
print("dice is:", dice, "jac is:", jac)
save_path = './com.nii.gz'
save_com_img2nii(pred, target, save_path)