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
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
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
# 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 masknod[masknod!=finding] = 0 masknod[masknod>0] = 1 mask_cube = extractCube(masknod,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),:,:]) axs[0,1].imshow(scan_cube[:,int(scan_cube.shape[1]/2),:]) axs[1,1].imshow(mask_cube[:,int(mask_cube.shape[1]/2),:]) axs[0,2].imshow(scan_cube[:,:,int(scan_cube.shape[2]/2)]) axs[1,2].imshow(mask_cube[:,:,int(mask_cube.shape[2]/2)])
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 mask_cube = extractCube(masknod,spacing,ctr)""" slice1 = scan_cube[int(scan_cube.shape[0] / 2), :, :] slice2 = scan_cube[:, int(scan_cube.shape[1] / 2), :] slice3 = scan_cube[:, :, int(scan_cube.shape[2] / 2)] # Display mid slices from resampled scan/mask if dispFlag: fig, axs = plt.subplots(2, 3) axs[0, 0].imshow(slice1) axs[1, 0].imshow(mask_cube[int(mask_cube.shape[0] / 2), :, :]) axs[0, 1].imshow(slice2) axs[1, 1].imshow(mask_cube[:, int(mask_cube.shape[1] / 2), :])
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. masknod[masknod > 0] = 1 mask_cube = extractCube(masknod, spacing, ctr, cube_size, voxel_size) print(mask_cube) # 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), :, :]) axs[0, 1].imshow(scan_cube[:, int(scan_cube.shape[1] / 2), :]) axs[1, 1].imshow(mask_cube[:, int(mask_cube.shape[1] / 2), :])
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) OriginalCTR = ctr 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 mask_cube = extractCube(masknod, spacing, ctr) img1 = mask_cube[int(mask_cube.shape[0] / 2), :, :] img2 = mask_cube[:, int(mask_cube.shape[1] / 2), :] img3 = mask_cube[:, :, int(mask_cube.shape[2] / 2)] XValues = [] YValues = [] ZValues = [] XLayers = [] YLayers = [] ZLayers = [] E = 0 fil, col = img1.shape