Exemple #1
0
def getMaskVolumes():

    csvLines = utils.readCsv("../trainset_csv/trainNodules_gt.csv")
    last_ID = 0
    scan = 0
    spacing = 0
    origin = 0

    maskVolumesList = []

    # ignore header
    for line in csvLines[1:]:

        # get image of this patient only one time (there are repeated patient ids)
        current_ID = line[0]
        if last_ID != current_ID:
            print(getFileID(current_ID))
            scan, spacing, origin, _ = utils.readMhd(
                '../LNDb dataset/dataset/LNDb-' + getFileID(current_ID) +
                '.mhd')
            spacing = [float(spacing[i]) for i in range(3)]

        # find the coordinates of the current nodule (it is done for every line of the csv)
        finding_coords = line[4:7]

        nodule_x = (float(finding_coords[0]) - float(origin[0])) / float(
            spacing[0])
        nodule_y = (float(finding_coords[1]) - float(origin[1])) / float(
            spacing[1])
        nodule_z = (float(finding_coords[2]) - float(origin[2])) / float(
            spacing[2])
        real_coords = [nodule_x, nodule_y, nodule_z]

        # get a mask for the image of this patient (from one of the radiologists that found the current nodule)
        radiologists = line[
            1]  # list of radiologists that found the current nodule
        radId = str(
            radiologists[0]
        )  # always choose the mask from the first radiologist in the list
        mask, _, _, _ = utils.readMhd('../LNDb dataset/masks/LNDb-' +
                                      getFileID(current_ID) + '_rad' + radId +
                                      '.mhd')

        # extract mini cube of the current nodule on the masked scan
        mask_volume = utils.extractCube(mask,
                                        spacing,
                                        real_coords,
                                        cube_size=80)

        # add mask volumes to the list
        maskVolumesList.append(mask_volume)

        last_ID = current_ID

    return maskVolumesList
Exemple #2
0
def load_image(img_name, centers):
    ''' loads a single image and returns a list of subimages (cubes around nodule-like
            findings)  

        img_name: valid path to an image from current working directory
        centers: a list of the centroids of nodule-like occurrences in img
                in world coordinates
    '''
    scan, spacing, origin, transfmat = readMhd(img_name)
    transfmat_toimg, _ = getImgWorldTransfMats(spacing,
                                               transfmat)  # just need image
    centers = [convertToImgCoord(c, origin, transfmat_toimg) for c in centers]

    subimages = [extractCube(scan, spacing, c) for c in centers]
    return subimages
Exemple #3
0
def createCube(cubeSize=80):
    csvLines = readCsv("csv/trainSet.csv")
    header = csvLines[0]
    nodules = csvLines[1:]

    for i, n in tqdm(enumerate(nodules[:1])):
        x = int(n[header.index("x")])
        y = int(n[header.index("y")])
        z = int(n[header.index("z")])

        lnd = int(n[header.index("LNDbID")])

        ctr = np.array([x, y, z])
        [scan, spacing, _, _] = readMhd('data/LNDb-{:04}.mhd'.format(lnd))
        cube = extractCube(scan, spacing, ctr, cubeSize)

        np.save("cube/{:0}.npy".format(i), cube)
def getCubes(cubeSize):
    csvLines = utils.readCsv("../trainset_csv/trainNodules_gt.csv")
    last_ID = 0
    scan = 0
    spacing = 0
    origin = 0
    
    cubeList = []    
    textures = [row[-1] for row in csvLines]

    # delete 1st element (header)
    del textures[0]

    # ignore header
    for line in csvLines[1:]:
                
        current_ID = line[0]
        if last_ID != current_ID:
            print(getFileID(current_ID))
            scan,spacing,origin,_ = utils.readMhd('../LNDb dataset/dataset/LNDb-' + getFileID(current_ID) + '.mhd')
            spacing = [float(spacing[i]) for i in range(3)]

        finding_coords = line[4:7]
        
        nodule_x = (float(finding_coords[0]) - float(origin[0])) / float(spacing[0])
        nodule_y = (float(finding_coords[1]) - float(origin[1])) / float(spacing[1])
        nodule_z = (float(finding_coords[2]) - float(origin[2])) / float(spacing[2])
        real_coords = [nodule_x, nodule_y, nodule_z]

        scan_cube = utils.extractCube(scan, spacing, real_coords, cube_size=cubeSize)
        
        cubeList.append(scan_cube)

        # nodule_coords
        last_ID = current_ID
    
    return cubeList, textures
Exemple #5
0
for n in nodules:
        vol = float(n[header.index('Volume')])
        if nodEqDiam(vol)>3: #only get nodule cubes for nodules>3mm
                ctr = np.array([float(n[header.index('x')]), float(n[header.index('y')]), float(n[header.index('z')])])
                lnd = int(n[header.index('LNDbID')])
                
                rad = int(n[header.index('RadID')])
                # rads = list(map(int,list(n[header.index('RadID')].split(','))))
                # radfindings = list(map(int,list(n[header.index('RadFindingID')].split(','))))
                finding = int(n[header.index('FindingID')])
                
                # print(lnd,finding,rads,radfindings)
                print(lnd,finding,rad,finding)
                # Read scan
                if lnd!=lndloaded:
                        [scan,spacing,origin,transfmat] =  readMhd('/media/tungthanhlee/SSD/grand_challenge/dataset/LNDB_segmentation/data/LNDb-{:04}.mhd'.format(lnd))                
                        transfmat_toimg,transfmat_toworld = getImgWorldTransfMats(spacing,transfmat)
                        lndloaded = lnd
                
                # Convert coordinates to image
                ctr = convertToImgCoord(ctr,origin,transfmat_toimg)                
                
                
                # for rad,radfinding in zip(rads,radfindings):
                # Read segmentation mask
                [mask,_,_,_] =  readMhd('/media/tungthanhlee/SSD/grand_challenge/dataset/LNDB_segmentation/masks/LNDb-{:04}_rad{}.mhd'.format(lnd,rad))
                
                # Extract cube around nodule
                scan_cube = extractCube(scan,spacing,ctr)
                masknod = copy.copy(mask)
                # masknod[masknod!=radfinding] = 0
# * Z-Axis position of Slice/Label

# ## 3 adjacent grayscale axial slides

if not os.path.isdir('sliceTs'):
    os.mkdir('sliceTs')

tsFiles = []
t_wlds = []
origins = []
id_c = 1
id_n = 0
input_shapes = []

for index in tqdm(range(len(filepaths_imgs))):
    sc, sp, o, t = utils.readMhd(filepaths_imgs[index])

    t_img, t_wld = utils.getImgWorldTransfMats(sp, t)

    z_max = sc.shape[0]
    z_max_idx = z_max - (sc.shape[0] % 3)
    for id_n in range(int(z_max_idx * .6), int(z_max_idx * .9), 3):

        origins.append(o)
        input_shapes.append(sc.shape)
        slide = sc[int(id_n) - 1:int(id_n) + 2, :, :]
        slide = equalize_hist(slide)
        slide = anisotropic_diffusion(slide,
                                      voxelspacing=sp,
                                      kappa=20,
                                      gamma=0.01,
Exemple #7
0
if __name__ == "__main__":
    os.environ['CUDA_VISIBLE_DEVICES'] = '0'
    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

    torch.manual_seed(10)

    reader = readCsv('./train_valid_CTs.csv')
    data_list = readCsv('./data_list_train_valid.csv')
    reader = reader[1:]
    data_list = np.array(data_list[1:])

    tr_data = list()
    tr_label = list()
    for number in range(len(reader)):  #讀取檔案製作訓練資料集[病人,幾張,3,Y,X]
        [scan, spacing, origin, transfmat
         ] = readMhd('traindata/LNDb-{:04}.mhd'.format(int(reader[number][0])))
        [mask, spacing, origin, transfmat] = readMhd(
            'trainlabel_1c1t/LNDb-{:04}_w_Text_merged-mask.mhd'.format(
                int(reader[number][0])))
        for z in range(scan.shape[0]):
            tr_data.append(scan[z])
            tr_label.append(mask[z])
    print("loading finished.")

    model = NestedUNet().to(device)
    #pre_weight = ""
    #model.load_state_dict(torch.load(pre_weight, map_location=device))
    print("amount of parameters: {}".format(count_params(model)))

    # hyperparameters
    learning_rate = 1e-4
Exemple #8
0
            float(n[header.index('x')]),
            float(n[header.index('y')]),
            float(n[header.index('z')])
        ])
        lnd = int(n[header.index('LNDbID')])
        rads = list(map(int, list(n[header.index('RadID')].split(','))))
        radfindings = list(
            map(int, list(n[header.index('RadFindingID')].split(','))))
        finding = int(n[header.index('FindingID')])

        print(lnd, finding, rads, radfindings)

        # Read scan
        if lnd != lndloaded:
            [scan, spacing, origin,
             transfmat] = readMhd('data/LNDb-{:04}.mhd'.format(lnd))
            transfmat_toimg, transfmat_toworld = getImgWorldTransfMats(
                spacing, transfmat)
            lndloaded = lnd

        # Convert coordinates to image
        ctr = convertToImgCoord(ctr, origin, transfmat_toimg)

        for rad, radfinding in zip(rads, radfindings):
            # Read segmentation mask
            [mask, _, _,
             _] = readMhd('masks/LNDb-{:04}_rad{}.mhd'.format(lnd, rad))

            # Extract cube around nodule
            scan_cube = extractCube(scan, spacing, ctr)
            masknod = copy.copy(mask)
        float(n[header.index('z')])
    ])
    lnd = int(n[header.index('LNDbID')])
    rads = list(map(int, list(n[header.index('RadID')].split(','))))
    radfindings = list(
        map(int, list(n[header.index('RadFindingID')].split(','))))
    finding = int(n[header.index('FindingID')])
    noduleTexture = (n[header.index('Text')])
    noduleTexture = float(n[header.index('Text')])
    #label = label_img(noduleTexture)
    print(lnd, finding, rads, radfindings, noduleTexture)
    #print("nodule Texture", label)
    # Read scan
    if lnd != lndloaded:
        [scan, spacing, origin,
         transfmat] = readMhd('data/LNDb-{:04}.mhd'.format(lnd))
        transfmat_toimg, transfmat_toworld = getImgWorldTransfMats(
            spacing, transfmat)
        lndloaded = lnd

    # Convert coordinates to image
    ctr = convertToImgCoord(ctr, origin, transfmat_toimg)

    #for rad,radfinding in zip(rads,radfindings):
    # Read segmentation mask
    #[mask,_,_,_] =  readMhd('masks/LNDb-{:04}_rad{}.mhd'.format(lnd,rad))

    # Extract cube around nodule
    scan_cube = extractCube(scan, spacing, ctr)
    """masknod = copy.copy(mask)
        masknod[masknod!=radfinding] = 0
            float(n[header.index('y')]),
            float(n[header.index('z')])
        ])
        lnd = int(n[header.index('LNDbID')])
        rads = list(map(int, list(n[header.index('RadID')].split(','))))
        radfindings = list(
            map(int, list(n[header.index('RadFindingID')].split(','))))
        finding = int(n[header.index('FindingID')])
        texts = list(map(float, list(n[header.index('Text')].split(','))))

        print(lnd, finding, rads, radfindings)

        # Read scan
        if lnd != lndloaded:
            [scan, spacing, origin,
             transfmat] = readMhd('../Dataset/LNDb-{:04}.mhd'.format(lnd))
            transfmat_toimg, transfmat_toworld = getImgWorldTransfMats(
                spacing, transfmat)
            lndloaded = lnd

        # Convert coordinates to image
        ctr = convertToImgCoord(ctr, origin, transfmat_toimg)

        for rad, radfinding, text in zip(rads, radfindings, texts):
            scan_cube = extractCube(scan, spacing, ctr)

            # Display mid slices from resampled scan/mask
            if dispFlag:
                fig, axs = plt.subplots(2, 3)
                axs[0, 0].imshow(scan_cube[int(scan_cube.shape[0] / 2), :, :])
                # axs[1,0].imshow(mask_cube[int(mask_cube.shape[0]/2),:,:])
Exemple #11
0
    if nodEqDiam(vol) > 3:  #only get nodule cubes for nodules>3mm
        ctr = np.array([
            float(n[header.index('x')]),
            float(n[header.index('y')]),
            float(n[header.index('z')])
        ])
        lnd = int(n[header.index('LNDbID')])
        rad = int(n[header.index('RadID')])
        finding = int(n[header.index('FindingID')])

        print(ctr, lnd, finding, rad)

        # Read scan
        if lnd != lndloaded:
            [scan, spacing, origin,
             transfmat] = readMhd('../Dataset/LNDb-{:04}.mhd'.format(lnd))
            transfmat_toimg, transfmat_toworld = getImgWorldTransfMats(
                spacing, transfmat)
            lndloaded = lnd

        # Convert coordinates to image
        ctr = convertToImgCoord(ctr, origin, transfmat_toimg)

        # Read segmentation mask
        [mask, _, _,
         _] = readMhd('../Dataset/masks/LNDb-{:04}_rad{}.mhd'.format(lnd, rad))

        # Extract cube around nodule
        scan_cube = extractCube(scan, spacing, ctr)
        masknod = copy.copy(mask)
        masknod[masknod != finding] = 0  #is radfinding on other file.
Exemple #12
0
lndloaded = -1
for n in nodules:
        vol = float(n[header.index('Volume')])
        if nodEqDiam(vol)>3: #only get nodule cubes for nodules>3mm
                ctr = np.array([float(n[header.index('x')]), float(n[header.index('y')]), float(n[header.index('z')])])
                lnd = int(n[header.index('LNDbID')])
                rads = list(map(int,list(n[header.index('RadID')].split(','))))
                radfindings = list(map(int,list(n[header.index('RadFindingID')].split(','))))
                finding = int(n[header.index('FindingID')])
                
                print(lnd,finding,rads,radfindings)
                        
                # Read scan
                if lnd!=lndloaded:
                        [scan,spacing,origin,transfmat] =  readMhd('data/LNDb-{:04}.mhd'.format(lnd))                
                        transfmat_toimg,transfmat_toworld = getImgWorldTransfMats(spacing,transfmat)
                        lndloaded = lnd
                
                # Convert coordinates to image
                ctr = convertToImgCoord(ctr,origin,transfmat_toimg)                
                
                for rad,radfinding in zip(rads,radfindings):
                        # Read segmentation mask
                        [mask,_,_,_] =  readMhd('masks/LNDb-{:04}_rad{}.mhd'.format(lnd,rad))
                        
                        # Extract cube around nodule
                        scan_cube = extractCube(scan,spacing,ctr)
                        masknod = copy.copy(mask)
                        masknod[masknod!=radfinding] = 0
                        masknod[masknod>0] = 1