#extract_patches.py 主要参考的原代码
importnumpy as npimportrandomimportconfigparser as ConfigParserfrom help_functions importload_hdf5from help_functions importvisualizefrom help_functions importgroup_imagesfrom pre_processing importmy_PreProc#To select the same images#random.seed(10)
#Load the original data and return the extracted patches for training/testing
defget_data_training(DRIVE_train_imgs_original,
DRIVE_train_groudTruth,
patch_height,
patch_width,
N_subimgs,
inside_FOV):
train_imgs_original=load_hdf5(DRIVE_train_imgs_original)
train_masks= load_hdf5(DRIVE_train_groudTruth) #masks always the same
#visualize(group_images(train_imgs_original[0:20,:,:,:],5),'imgs_train')#.show() #check original imgs train
train_imgs=my_PreProc(train_imgs_original)
train_masks= train_masks/255.
train_imgs= train_imgs[:,:,9:574,:] #cut bottom and top so now it is 565*565
train_masks = train_masks[:,:,9:574,:] #cut bottom and top so now it is 565*565
data_consistency_check(train_imgs,train_masks)#check masks are within 0-1
assert(np.min(train_masks)==0 and np.max(train_masks)==1)print ("\ntrain images/masks shape:")print(train_imgs.shape)print ("train images range (min-max):" +str(np.min(train_imgs)) +'-'+str(np.max(train_imgs)))print ("train masks are within 0-1\n")#extract the TRAINING patches from the full images
patches_imgs_train, patches_masks_train =extract_random(train_imgs,train_masks,patch_height,patch_width,N_subimgs,inside_FOV)
data_consistency_check(patches_imgs_train, patches_masks_train)print ("\ntrain PATCHES images/masks shape:")print(patches_imgs_train.shape)print ("train PATCHES images range (min-max):" +str(np.min(patches_imgs_train)) +'-'+str(np.max(patches_imgs_train)))return patches_imgs_train, patches_masks_train#, patches_imgs_test, patches_masks_test
#Load the original data and return the extracted patches for training/testing
defget_data_testing(DRIVE_test_imgs_original, DRIVE_test_groudTruth, Imgs_to_test, patch_height, patch_width):### test
test_imgs_original =load_hdf5(DRIVE_test_imgs_original)
test_masks=load_hdf5(DRIVE_test_groudTruth)
test_imgs=my_PreProc(test_imgs_original)
test_masks= test_masks/255.#extend both images and masks so they can be divided exactly by the patches dimensions
test_imgs =test_imgs[0:Imgs_to_test,:,:,:]
test_masks=test_masks[0:Imgs_to_test,:,:,:]
test_imgs=paint_border(test_imgs,patch_height,patch_width)
test_masks=paint_border(test_masks,patch_height,patch_width)
data_consistency_check(test_imgs, test_masks)#check masks are within 0-1
assert(np.max(test_masks)==1 and np.min(test_masks)==0)print ("\ntest images/masks shape:")print(test_imgs.shape)print ("test images range (min-max):" +str(np.min(test_imgs)) +'-'+str(np.max(test_imgs)))print ("test masks are within 0-1\n")#extract the TEST patches from the full images
patches_imgs_test =extract_ordered(test_imgs,patch_height,patch_width)
patches_masks_test=extract_ordered(test_masks,patch_height,patch_width)
data_consistency_check(patches_imgs_test, patches_masks_test)print ("\ntest PATCHES images/masks shape:")print(patches_imgs_test.shape)print ("test PATCHES images range (min-max):" +str(np.min(patches_imgs_test)) +'-'+str(np.max(patches_imgs_test)))returnpatches_imgs_test, patches_masks_test#Load the original data and return the extracted patches for testing#return the ground truth in its original shape