def lung_center_size(patient="", center_size="center"): path_to_LungAreaIso = "../../PET-CT_iso3mm/%s/LungAreaIso.mhd" # % patient path_to_WbMaskIso = "../../PET-CT_iso3mm/%s/WbMaskIso.mhd" # % patient if os.path.exists(path_to_LungAreaIso % patient): lungarea = readmhd.read(path_to_LungAreaIso % patient).vol elif os.path.exists(path_to_WbMaskIso % patient): lungarea = readmhd.read(path_to_WbMaskIso % patient).vol lungarea[lungarea<3]=0 BB_lungarea = np.argwhere(lungarea) (zmin, ymin, xmin), (zmax, ymax, xmax) = BB_lungarea.min(0), BB_lungarea.max(0)+1 x_center, y_center, z_center = int((xmax+xmin)/2.0), int((ymax+ymin)/2.0), int((zmax+zmin)/2.0) if center_size=="center": return x_center, y_center, z_center if center_size=="size": return xmax-xmin, ymax-ymin, zmax-zmin
def get_lung_size(): path_to_petct = "../../PET-CT_iso3mm/" path_to_petiso = "../../PET-CT_iso3mm/%s/PETiso.mhd" # % patient path_to_LungAreaIso = "../../PET-CT_iso3mm/%s/LungAreaIso.mhd" # % patient path_to_WbMaskIso = "../../PET-CT_iso3mm/%s/WbMaskIso.mhd" # % patient path_to_lung_size = "./lung_size.csv" if os.path.exists(path_to_lung_size): os.remove(path_to_lung_size) lung_size_csv = open(path_to_lung_size, 'w') writer = csv.writer(lung_size_csv, lineterminator='\n') writer.writerow( ["patient_id", "xmin", "xmax", "ymin", "ymax", "zmin", "zmax"] ) # headder lung_size_csv.close() # for patient in get_patients(matrix_size=matrix_size): BB_z_max, BB_z_min = 0, 1000 num_patient, num_lai, num_wmi = 0, 0, 0 for patient in os.listdir(path_to_petct): # pet = readmhd.read(path_to_petiso % patient).vol if os.path.exists(path_to_LungAreaIso % patient): num_lai += 1 lungarea = readmhd.read(path_to_LungAreaIso % patient).vol elif os.path.exists(path_to_WbMaskIso % patient): num_wmi += 1 lungarea = readmhd.read(path_to_WbMaskIso % patient).vol lungarea[lungarea<3]=0 BB_lungarea = np.argwhere(lungarea) (zmin, ymin, xmin), (zmax, ymax, xmax) = BB_lungarea.min(0), BB_lungarea.max(0)+1 print(patient) lung_size_csv = open(path_to_lung_size, 'a') writer = csv.writer(lung_size_csv, lineterminator='\n') writer.writerow( [patient, xmin, xmax, ymin, ymax, zmin, zmax] ) lung_size_csv.close() num_patient += 1 if zmax-zmin > BB_z_max: BB_z_max = zmax-zmin if zmax-zmin < BB_z_min: BB_z_min = zmax-zmin # if zmax-zmin > 100: # print(zmax-zmin, patient) print(BB_z_min, BB_z_max) print(num_patient, num_lai, num_wmi)
def get_patients(matrix_size=[166,166,257], ): path_to_petct = "../../PET-CT_iso3mm/" pth_to_petiso = "../../PET-CT_iso3mm/%s/PETiso.mhd" # % patient patients=[] for patient in os.listdir(path_to_petct): volume = readmhd.read(pth_to_petiso % patient) # print(volume.voxelsize) if volume.matrixsize==matrix_size: patients.append(patient) return patients
def resize_all(ref_size=np.array([3.6, 0.625, 0.625])): path_to_dir = "../TrainingData_Part%d/" path_to_original_scale_npy = "../IntermediateData/original_scale/%s.npy" path_to_rescaled_npy = "../IntermediateData/rescaled/%s.npy" for part in range(1, 4): for file in os.listdir(path_to_dir % part): if re.match("Case[0-9][0-9].mhd", file): volume = readmhd.read(path_to_dir % part + file) np.save(path_to_original_scale_npy % file[:-4], np.float32(volume.vol)) matrixsize_rescaled = ( volume.matrixsize[::-1] * (volume.voxelsize[::-1] / ref_size)).astype(np.int) matrixsize_rescaled[1] = matrixsize_rescaled[2] volume_rescaled = resize(volume.vol, matrixsize_rescaled) # np.float32(volume_rescaled).tofile("../IntermediateData/rescaled/"+file[:-4]+".raw") np.save(path_to_rescaled_npy % file[:-4], np.float32(volume_rescaled)) print(file, volume_rescaled.shape) if re.match("Case[0-9][0-9]_segmentation.mhd", file): volume = readmhd.read(path_to_dir % part + file) np.save(path_to_original_scale_npy % (file[:-4] + "32"), np.float32(volume.vol)) matrixsize_rescaled = ( volume.matrixsize[::-1] * (volume.voxelsize[::-1] / ref_size)).astype(np.int) matrixsize_rescaled[1] = matrixsize_rescaled[2] volume_rescaled = resize(np.float32(volume.vol), matrixsize_rescaled) # volume_rescaled.tofile("../IntermediateData/rescaled/"+file[:-4]+".raw") volume_rescaled[volume_rescaled > 0.5] = 1 np.save(path_to_rescaled_npy % file[:-4], np.int8(volume_rescaled)) np.save(path_to_rescaled_npy % (file[:-4] + "32"), np.float32(volume_rescaled)) print(file, volume_rescaled.shape)
import readmhd path_to_petct = "../../PET-CT_iso3mm/" pth_to_petiso = "../../PET-CT_iso3mm/%s/PETiso.mhd" # % patient path_to_text = "./pet.txt" path_to_csv = "./image_info.csv" #volume = readmhd.read(pth_to_pet) image_info_csv = open(path_to_csv, 'w') writer = csv.writer(image_info_csv, lineterminator='\n') writer.writerow( ["patient_id", "voxel_size1", "voxel_size2", "voxel_size3", "matrix_size1", "matrix_size2", "matrix_size3"] ) # headder image_info_csv.close() for patient_id in os.path.listdir(path_to_petct): volume = readmhd.read(pth_to_petiso % patient_id) image_info_csv = open(path_to_csv, 'a') writer = csv.writer(image_info_csv, lineterminator='\n') writer.writerow( [patient_id, volume.voxelsize[0], volume.voxelsize[1], volume.voxelsize[2], volume.matrixsize[0], volume.matrixsize[1], volume.matrixsize[2]]) image_info_csv.close() # hp_log = open(path_to_hp_log, 'a') # writer = csv.writer(hp_log, lineterminator='\n') # writer.writerow([counter,] + list(hp_value.values()) + [np.amax(val_acc),]) # hp_log.close() # #f = open(path_to_text, "w") #f.write(str(volume.voxelsize)) #f.close()