def readdata():
    file = h5py.File('File_Patientdata.h5', 'w')
    patient = []
    path = "E:/zhangjinjing/brain2D/Image_Data"
    for i in os.listdir(path):
        patient.append(i)
    patient = np.array(patient)
    for j in range(patient.shape[0]):
        CH1 = util.read_mha_image_as_nuarray(path + "/" + patient[j] +
                                             "/001/001.mha")
        CH2 = util.read_mha_image_as_nuarray(path + "/" + patient[j] +
                                             "/002/002.mha")
        CH3 = util.read_mha_image_as_nuarray(path + "/" + patient[j] +
                                             "/003/003.mha")
        CH4 = util.read_mha_image_as_nuarray(path + "/" + patient[j] +
                                             "/004/004.mha")
        LABEL = util.read_mha_image_as_nuarray(path + "/" + patient[j] +
                                               "/005/005.mha")
        file.create_dataset(
            'data' + str(j),
            data=np.concatenate([
                CH1[:, :, :, np.newaxis], CH2[:, :, :, np.newaxis],
                CH3[:, :, :, np.newaxis], CH4[:, :, :, np.newaxis]
            ], 3))
        file.create_dataset('label' + str(j), data=LABEL)
    print('set data ok!')
def readDataLocal():
    All_data = []
    All_label = []

    for zip_num in range(274):
        print(zip_num)
        CH1 = util.read_mha_image_as_nuarray('BrainTrain/' + str(zip_num) +
                                             '/0001/0001.mha')
        CH2 = util.read_mha_image_as_nuarray('BrainTrain/' + str(zip_num) +
                                             '/0002/0002.mha')
        CH3 = util.read_mha_image_as_nuarray('BrainTrain/' + str(zip_num) +
                                             '/0003/0003.mha')
        CH4 = util.read_mha_image_as_nuarray('BrainTrain/' + str(zip_num) +
                                             '/0004/0004.mha')
        CHLABEL = util.read_mha_image_as_nuarray('BrainTrain/' + str(zip_num) +
                                                 '/0005/0005.mha')

        All_data.append(
            np.concatenate([
                CH1[:, :, :, np.newaxis], CH2[:, :, :, np.newaxis],
                CH3[:, :, :, np.newaxis], CH4[:, :, :, np.newaxis]
            ], 3))
        All_label.append(CHLABEL)

    return All_data, All_label
Exemplo n.º 3
0
def compare(path1,path2):
    data1=util.read_mha_image_as_nuarray(path1)
    data2=util.read_mha_image_as_nuarray(path2)
    for i in range(data1.shape[0]):
        for j in range(data1.shape[1]):
            for k in range (data1.shape[2]):
                if data1[i,j,k]==data2[i,j,k]:
                    data1[i,j,k]=0
    return data1
Exemplo n.º 4
0
def DataProcess(zip_num):
    TestPath = 'E://zhangjinjing/Brain2D/BRATS_Leaderboard/' + str(zip_num + 1)
    CH1 = util.read_mha_image_as_nuarray(TestPath + '/1/1.mha')
    CH2 = util.read_mha_image_as_nuarray(TestPath + '/2/2.mha')
    CH3 = util.read_mha_image_as_nuarray(TestPath + '/3/3.mha')
    CH4 = util.read_mha_image_as_nuarray(TestPath + '/4/4.mha')
    ALLtest = np.concatenate([
        CH1[:, :, :, np.newaxis], CH2[:, :, :, np.newaxis],
        CH3[:, :, :, np.newaxis], CH4[:, :, :, np.newaxis]
    ], 3)

    del CH1, CH2, CH3, CH4
    gc.collect()
    return ALLtest
def image_getindex():
    index1 = []
    index2 = []
    index3 = []
    index4 = []
    patient = []
    patch = 65
    path = "E:/zhangjinjing/brain2D/Image_Data"
    for i in os.listdir(path):
        patient.append(i)
    patient = np.array(patient)
    for j in range(patient.shape[0]):
        label = util.read_mha_image_as_nuarray(path + "/" + patient[j] +
                                               '/005/005.mha')
        for x in range(label.shape[0]):
            for y in range(label.shape[1] - patch):
                y = y + int(patch / 2)
                for z in range(label.shape[2] - patch):
                    z = z + int(patch / 2)
                    pixel = label[x, y, z]
                    if pixel == 1:
                        index1.append([j, x, y, z])
                        print('j,x,y,z:', j, x, y, z)
                    if pixel == 2:
                        index2.append([j, x, y, z])
                        print('j,x,y,z:', j, x, y, z)
                    if pixel == 3:
                        index3.append([j, x, y, z])
                        print('j,x,y,z:', j, x, y, z)
                    if pixel == 4:
                        index4.append([j, x, y, z])
                        print('j,x,y,z:', j, x, y, z)
    return np.array(index1), np.array(index2), np.array(index3), np.array(
        index4)
Exemplo n.º 6
0
    def load_patient_files(self):

        self.file_T1 = self._find_file("T1.*\.mha")
        self.file_T1c = self._find_file("T1c.*\.mha")
        self.file_T2 = self._find_file("T2.*\.mha")
        self.file_FLAIR = self._find_file("Flair.*\.mha")

        self.T1 = utility.read_mha_image_as_nuarray(
            self._find_file("T1.*\.mha"))
        self.T1c = utility.read_mha_image_as_nuarray(
            self._find_file("T1c.*\.mha"))
        self.T2 = utility.read_mha_image_as_nuarray(
            self._find_file("T2.*\.mha"))
        self.FLAIR = utility.read_mha_image_as_nuarray(
            self._find_file("Flair.*\.mha"))
        self.label = utility.read_mha_image_as_nuarray(
            self._find_file("OT.*\.mha"))
Exemplo n.º 7
0
def readdata():
    path = 'E:/zhangjinjing/brain2D/Image_Data'
    patient = []
    for i in os.listdir(path):
        print('i', i)
        patient.append(i)
    patient = np.array(patient)
    for i in range(patient.shape[0]):
        data = util.read_mha_image_as_nuarray(path + '/' + str(patient[i]) +
                                              '/001/001.mha')
        print(data.shape)
Exemplo n.º 8
0
    def preprocess(self):
        self.set_parent_file(self.file_parent)

        if (self._find_file("back_ground") == None):
            back_ground = self.BFS()
            utility.save_nuarray_as_mha(
                os.path.join(self.file_parent, "back_ground.mha"), back_ground)
            self.set_parent_file(self.file_parent)

        self.back_ground = utility.read_mha_image_as_nuarray(
            self._find_file("back_ground\.mha"))

        balanced_data_file = os.path.join(self.file_parent,
                                          "balanced_data.pickle")
        if (not os.path.isfile(balanced_data_file)):
            self.balance_data()

        ff = open(balanced_data_file, "rb")
        self.balanced_data_indices = pickle.load(ff)
        ff.close()
Exemplo n.º 9
0
def colorize():
    file = util.read_mha_image_as_nuarray(test)
    newfile = np.zeros([file.shape[0], file.shape[1], file.shape[2], 3])
    for i in range(file.shape[0]):
        for j in range(file.shape[1]):
            for k in range(file.shape[2]):
                if file[i, j, k] == 1:
                    newfile[i, j, k] = [192, 57, 43]
                elif file[i, j, k] == 2:
                    newfile[i, j, k] = [39, 174, 96]
                elif file[i, j, k] == 3:
                    newfile[i, j, k] = [155, 89, 182]
                elif file[i, j, k] == 4:
                    newfile[i, j, k] = [243, 156, 18]
                else:
                    newfile[i, j, k] = [0, 0, 0]
    for i in range(file.shape[2]):
        img = Image.fromarray(
            np.uint8(newfile[0:newfile.shape[0], 0:newfile.shape[1], i]))
        img.save("picture/test9/green/test9-g" + str(i) + ".jpg", 'jpeg')
Exemplo n.º 10
0
def patient_accuracy(patient_file):

    p = Patient()
    p.set_parent_file(patient_file)
    p.set_window_size(0)
    p.start_iteration()

    print("patient accuracy")
    print(p.file_parent)
    for z in range(0, p.limit_z):
        for y in range(0, p.limit_y):
            for x in range(0, p.limit_x):
                if (p.label[z][y][x] != 0):
                    p.label[z][y][x] = 1

    a = p.label
    b = utility.read_mha_image_as_nuarray(p._find_file("segmentation.mha"))

    print("files load , start accuracy calculation")

    P = TP(a, b, 0, a.ndim) + FP(a, b, 0, a.ndim)  #total positive
    T = TP(a, b, 0, a.ndim) + FN(a, b, 0, a.ndim)  #total actual positive
    N = TN(a, b, 0, a.ndim) + FP(a, b, 0, a.ndim)  #total actual negative
    dice = TP(a, b, 0, a.ndim) / float(((P + T) / 2))
    sens = TP(a, b, 0, a.ndim) / float(T)
    spec = TN(a, b, 0, a.ndim) / float(N)
    acc = (TP(a, b, 0, a.ndim) + TN(a, b, 0, a.ndim)) / float(T + N)
    error = 1.0 - acc

    p.stop_iteration()
    print("P %s" % P)
    print("T %s" % T)
    print("N %s" % N)
    print("dice %s" % dice)
    print("sens %s" % sens)
    print("spec %s" % spec)
    print("acc %s" % acc)
    print("error %s" % error)
Exemplo n.º 11
0
    def preprocess(
        self
    ):  # creates a background.mha file ,which displays black brain part
        self.set_parent_file(self.file_parent)

        if (self._find_file("back_ground") == None):
            back_ground = self.BFS()
            utility.save_nuarray_as_mha(
                os.path.join(self.file_parent, "back_ground.mha"),
                back_ground)  #getting image from array
            self.set_parent_file(self.file_parent)

        self.back_ground = utility.read_mha_image_as_nuarray(
            self._find_file("back_ground\.mha"))  # getting array from img

        balanced_data_file = os.path.join(
            self.file_parent, "balanced_data.pickle")  #file pickling
        if (not os.path.isfile(balanced_data_file)):
            self.balance_data()

        ff = open(balanced_data_file, "rb")
        self.balanced_data_indices = pickle.load(ff)
        ff.close()
Exemplo n.º 12
0
import numpy as np
import utility as util

modi210=util.read_mha_image_as_nuarray('test210.mha')
temp=np.zeros([155,240,240])
temp[:,32:207,32:207]=modi210
util.save_nuarray_as_mha('test210_2.mha', temp)


import numpy as np
import utility as util
import h5py
import gc
import tensorflow as tf
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '1'

patch_size = 65
patch_size2 = 33
halfpz = int(patch_size / 2)
halfpz2 = int(patch_size2 / 2)

path = 'Testing/'
patient = '78'
CH1 = util.read_mha_image_as_nuarray(path + patient + '/0001/0001.mha')
CH2 = util.read_mha_image_as_nuarray(path + patient + '/0002/0002.mha')
CH3 = util.read_mha_image_as_nuarray(path + patient + '/0003/0003.mha')
CH4 = util.read_mha_image_as_nuarray(path + patient + '/0004/0004.mha')
#CHLABEL = util.read_mha_image_as_nuarray(path+patient+'/0005/0005.mha')
ALLtest = np.concatenate([
    CH1[:, :, :, np.newaxis], CH2[:, :, :, np.newaxis],
    CH3[:, :, :, np.newaxis], CH4[:, :, :, np.newaxis]
], 3)
del CH1, CH2, CH3, CH4
gc.collect()


def getBatch2D(pos_x, pos_y):

    #print(index_shuffle.shape)
Exemplo n.º 14
0
import gc
import tensorflow as tf
import os
os.environ['CUDA_VISIBLE_DEVICES']='1'



patch_size=65
patch_size2=33
halfpz=int(patch_size/2)
halfpz2=int(patch_size2/2)

zip_num=114
#TestPath='Testing/'+str(zip_num)
TrainPath='BrainTrain/'+str(zip_num)
CH1=util.read_mha_image_as_nuarray(TrainPath+'/0001/0001.mha')
CH2=util.read_mha_image_as_nuarray(TrainPath+'/0002/0002.mha')
CH3=util.read_mha_image_as_nuarray(TrainPath+'/0003/0003.mha')
CH4=util.read_mha_image_as_nuarray(TrainPath+'/0004/0004.mha')
CHLABEL=util.read_mha_image_as_nuarray(TrainPath+'/0005/0005.mha')
ALLtest=np.concatenate([CH1[:,:,:,np.newaxis],
                        CH2[:,:,:,np.newaxis],
                        CH3[:,:,:,np.newaxis],
                        CH4[:,:,:,np.newaxis]],3)

del CH1,CH2,CH3,CH4
gc.collect()


def getBatch2D(pos_x,pos_y):
Exemplo n.º 15
0
Combine_conv = tf.nn.conv2d(
    Combine, WCombine_conv, strides=[1, 1, 1, 1
                                     ], padding='VALID') + BCombine_conv
ReCombine_conv = tf.reshape(Combine_conv, [175, 5])

OUT = tf.nn.softmax(ReCombine_conv)

#loss=tf.reduce_mean(-tf.reduce_sum(Yp*tf.log(OUT),reduction_indices=[1]))
#correct_prediction = tf.equal(tf.argmax(OUT,1), tf.argmax(Yp,1))
#accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))

index = readindex()
index = np.array(index)
for zip_num in range(index.shape[0]):
    TestPath = 'Testing/' + str(zip_num + 1)
    CH1 = util.read_mha_image_as_nuarray(TestPath + '/0001/0001.mha')
    CH2 = util.read_mha_image_as_nuarray(TestPath + '/0002/0002.mha')
    CH3 = util.read_mha_image_as_nuarray(TestPath + '/0003/0003.mha')
    CH4 = util.read_mha_image_as_nuarray(TestPath + '/0004/0004.mha')
    # CHLABEL=util.read_mha_image_as_nuarray(TestPath+'/0005/0005.mha')
    ALLtest = np.concatenate([
        CH1[:, :, :, np.newaxis], CH2[:, :, :, np.newaxis],
        CH3[:, :, :, np.newaxis], CH4[:, :, :, np.newaxis]
    ], 3)

    del CH1, CH2, CH3, CH4
    gc.collect()
    print('ALLTEST:', ALLtest.shape)
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    sess = tf.Session(config=config)
Exemplo n.º 16
0
def adjust():
    file = util.read_mha_image_as_nuarray(trainpatient)
    for i in range(file.shape[1]):
        img = Image.fromarray(
            np.uint8(file[0:file.shape[0], 0:file.shape[2], i]))
        img.save("picture/flair/green/flair" + str(i) + ".jpg", 'jpeg')