为什么无法从此代码获得结果?

时间:2019-05-09 12:42:38

标签: python

我正在尝试预处理列表中包含的图像列表 文件夹,我尝试使用此代码,但没有结果,而且我不知道如何按输入顺序保存结果。 在这段代码中,我尝试读取图像列表并使用已定义的函数对其进行预处理。 但是我不能救他们。我想在输入等文件夹中得到结果。

有人可以帮我解决这个问题吗?我是python的初学者,但我对它了解不多。

# -*- coding: utf-8 -*-

from __future__ import print_function

import os
import numpy as np
import scipy
from keras.layers.merge import concatenate
from keras import backend as K
K.set_image_data_format('channels_last')

#from multiprocessing import Pool, cpu_count

rows_standard = 200
cols_standard = 200
thresh_FLAIR = 70      #to mask the brain
thresh_T1 = 30
smooth=1.



path = "./dataset"
data_list = ["0","2","4","6","8"]



class Subjects(object):

  def __init__(self, name, path):
    self.name = name
    self.path = path
    self.get_arrays()

  def __str__(self):
    return "Subject: {}\nData path: {}\n".format(self.name,
      self.path)

  def open_files(self):
    self.FLAIR_image = sitk.ReadImage(os.path.join(self.path,
      self.name, 
      "pre/FLAIR.nii.gz"))
    self.T1_image = sitk.ReadImage(os.path.join(self.path, 
      self.name, 
      "pre/T1.nii.gz"))
    self.label_img = sitk.ReadImage(os.path.join(self.path,
      self.name, 
      "wmh.nii.gz"))


def get_arrays(self):
    self.open_files()
    self.FLAIR_array = sitk.GetArrayFromImage(self.FLAIR_image)
    self.T1_array = sitk.GetArrayFromImage(self.T1_image)
    self.label_array = sitk.GetArrayFromImage(self.label_img)
    self.shape = self.FLAIR_array.shape
    self.spacing = self.FLAIR_image.GetSpacing()
    self.origin = self.FLAIR_image.GetOrigin()
    self.direction = self.FLAIR_image.GetDirection()

def Utrecht_preprocessing(FLAIR_image, T1_image):

    channel_num = 2
    print(np.shape(FLAIR_image))
    num_selected_slice = np.shape(FLAIR_image)[0]
    image_rows_Dataset = np.shape(FLAIR_image)[1]
    image_cols_Dataset = np.shape(FLAIR_image)[2]
    T1_image = np.float32(T1_image)

    brain_mask_FLAIR = np.ndarray((np.shape(FLAIR_image)[0],image_rows_Dataset, image_cols_Dataset), dtype=np.float32)
    brain_mask_T1 = np.ndarray((np.shape(FLAIR_image)[0],image_rows_Dataset, image_cols_Dataset), dtype=np.float32)
    imgs_two_channels = np.ndarray((num_selected_slice, rows_standard, cols_standard, channel_num), dtype=np.float32)
    imgs_mask_two_channels = np.ndarray((num_selected_slice, rows_standard, cols_standard,1), dtype=np.float32)

    # FLAIR --------------------------------------------
    brain_mask_FLAIR[FLAIR_image >=thresh_FLAIR] = 1
    brain_mask_FLAIR[FLAIR_image < thresh_FLAIR] = 0
    for iii in range(np.shape(FLAIR_image)[0]):
        brain_mask_FLAIR[iii,:,:] = scipy.ndimage.morphology.binary_fill_holes(brain_mask_FLAIR[iii,:,:])  #fill the holes inside brain
    FLAIR_image = FLAIR_image[:, (image_rows_Dataset/2-rows_standard/2):(image_rows_Dataset/2+rows_standard/2), (image_cols_Dataset/2-cols_standard/2):(image_cols_Dataset/2+cols_standard/2)]
    brain_mask_FLAIR = brain_mask_FLAIR[:, (image_rows_Dataset/2-rows_standard/2):(image_rows_Dataset/2+rows_standard/2), (image_cols_Dataset/2-cols_standard/2):(image_cols_Dataset/2+cols_standard/2)]
    ###------Gaussion Normalization here
    FLAIR_image -=np.mean(FLAIR_image[brain_mask_FLAIR == 1])      #Gaussion Normalization
    FLAIR_image /=np.std(FLAIR_image[brain_mask_FLAIR == 1])
    # T1 -----------------------------------------------
    brain_mask_T1[T1_image >=thresh_T1] = 1
    brain_mask_T1[T1_image < thresh_T1] = 0
    for iii in range(np.shape(T1_image)[0]):
        brain_mask_T1[iii,:,:] = scipy.ndimage.morphology.binary_fill_holes(brain_mask_T1[iii,:,:])  #fill the holes inside brain
    T1_image = T1_image[:, (image_rows_Dataset/2-rows_standard/2):(image_rows_Dataset/2+rows_standard/2), (image_cols_Dataset/2-cols_standard/2):(image_cols_Dataset/2+cols_standard/2)]
    brain_mask_T1 = brain_mask_T1[:, (image_rows_Dataset/2-rows_standard/2):(image_rows_Dataset/2+rows_standard/2), (image_cols_Dataset/2-cols_standard/2):(image_cols_Dataset/2+cols_standard/2)]
    #------Gaussion Normalization
    T1_image -=np.mean(T1_image[brain_mask_T1 == 1])      
    T1_image /=np.std(T1_image[brain_mask_T1 == 1])
    #---------------------------------------------------
    FLAIR_image  = FLAIR_image[..., np.newaxis]
    T1_image  = T1_image[..., np.newaxis]
    imgs_two_channels = np.concatenate((FLAIR_image, T1_image), axis = 3)
    print(np.shape(imgs_two_channels))
    return imgs_two_channels

result_path = 'Utrecht_preprocess'
def save_preprocess(self, patch_size, result_path):
    self.pred_array = np.squeeze(self.pred_array)
    self.pred_array = np.argmax(self.pred_array, axis=3).astype(np.float32)
    self.result_img = sitk.GetImageFromArray(self.pred_array)
    self.result_img.CopyInformation(self.flair_img)
    img_path = os.path.join(result_path, self.name + "_pre.nii.gz")
    sitk.WriteImage(self.result_img, img_path)

0 个答案:

没有答案