@ -10,7 +10,6 @@ import imutils
import math
def bluring ( img_in , kind ) :
if kind == ' guass ' :
img_blur = cv2 . GaussianBlur ( img_in , ( 5 , 5 ) , 0 )
@ -20,8 +19,8 @@ def bluring(img_in,kind):
img_blur = cv2 . blur ( img_in , ( 5 , 5 ) )
return img_blur
def elastic_transform ( image , alpha , sigma , seedj , random_state = None ) :
def elastic_transform ( image , alpha , sigma , seedj , random_state = None ) :
""" Elastic deformation of images as described in [Simard2003]_.
. . [ Simard2003 ] Simard , Steinkraus and Platt , " Best Practices for
Convolutional Neural Networks applied to Visual Document Analysis " , in
@ -42,6 +41,7 @@ def elastic_transform(image, alpha, sigma,seedj, random_state=None):
distored_image = map_coordinates ( image , indices , order = 1 , mode = ' reflect ' )
return distored_image . reshape ( image . shape )
def rotation_90 ( img ) :
img_rot = np . zeros ( ( img . shape [ 1 ] , img . shape [ 0 ] , img . shape [ 2 ] ) )
img_rot [ : , : , 0 ] = img [ : , : , 0 ] . T
@ -49,6 +49,7 @@ def rotation_90(img):
img_rot [ : , : , 2 ] = img [ : , : , 2 ] . T
return img_rot
def rotatedRectWithMaxArea ( w , h , angle ) :
"""
Given a rectangle of size wxh that has been rotated by ' angle ' ( in
@ -76,6 +77,7 @@ def rotatedRectWithMaxArea(w, h, angle):
return wr , hr
def rotate_max_area ( image , rotated , rotated_label , angle ) :
""" image: cv2 image matrix object
angle : in degree
@ -88,11 +90,14 @@ def rotate_max_area(image,rotated, rotated_label,angle):
x1 = w / / 2 - int ( wr / 2 )
x2 = x1 + int ( wr )
return rotated [ y1 : y2 , x1 : x2 ] , rotated_label [ y1 : y2 , x1 : x2 ]
def rotation_not_90_func ( img , label , thetha ) :
rotated = imutils . rotate ( img , thetha )
rotated_label = imutils . rotate ( label , thetha )
return rotate_max_area ( img , rotated , rotated_label , thetha )
def color_images ( seg , n_classes ) :
ann_u = range ( n_classes )
if len ( np . shape ( seg ) ) == 3 :
@ -112,6 +117,8 @@ def color_images(seg, n_classes):
def resize_image ( seg_in , input_height , input_width ) :
return cv2 . resize ( seg_in , ( input_width , input_height ) , interpolation = cv2 . INTER_NEAREST )
def get_one_hot ( seg , input_height , input_width , n_classes ) :
seg = seg [ : , : , 0 ]
seg_f = np . zeros ( ( input_height , input_width , n_classes ) )
@ -121,8 +128,8 @@ def get_one_hot(seg,input_height,input_width,n_classes):
def IoU ( Yi , y_predi ) :
# # mean Intersection over Union
# # Mean IoU = TP/(FN + TP + FP)
# mean Intersection over Union
# Mean IoU = TP/(FN + TP + FP)
IoUs = [ ]
classes_true = np . unique ( Yi )
@ -137,6 +144,8 @@ def IoU(Yi,y_predi):
print ( " _________________ " )
print ( " Mean IoU: {:4.3f} " . format ( mIoU ) )
return mIoU
def data_gen ( img_folder , mask_folder , batch_size , input_height , input_width , n_classes ) :
c = 0
n = [ f for f in os . listdir ( img_folder ) if not f . startswith ( ' . ' ) ] # os.listdir(img_folder) #List of training images
@ -152,13 +161,15 @@ def data_gen(img_folder, mask_folder, batch_size,input_height, input_width,n_cla
filename = n [ i ] . split ( ' . ' ) [ 0 ]
train_img = cv2 . imread ( img_folder + ' / ' + n [ i ] ) / 255.
train_img = cv2 . resize ( train_img , ( input_width , input_height ) , interpolation = cv2 . INTER_NEAREST ) # Read an image from folder and resize
train_img = cv2 . resize ( train_img , ( input_width , input_height ) ,
interpolation = cv2 . INTER_NEAREST ) # Read an image from folder and resize
img [ i - c ] = train_img # add to array - img[0], img[1], and so on.
train_mask = cv2 . imread ( mask_folder + ' / ' + filename + ' .png ' )
# print(mask_folder+'/'+filename+'.png')
# print(train_mask.shape)
train_mask = get_one_hot ( resize_image ( train_mask , input_height , input_width ) , input_height , input_width , n_classes )
train_mask = get_one_hot ( resize_image ( train_mask , input_height , input_width ) , input_height , input_width ,
n_classes )
# train_mask = train_mask.reshape(224, 224, 1) # Add extra dimension for parity with train_img size [512 * 512 * 3]
mask [ i - c ] = train_mask
@ -166,14 +177,13 @@ def data_gen(img_folder, mask_folder, batch_size,input_height, input_width,n_cla
img [ i - c ] = np . ones ( ( input_height , input_width , 3 ) ) . astype ( ' float ' )
mask [ i - c ] = np . zeros ( ( input_height , input_width , n_classes ) ) . astype ( ' float ' )
c + = batch_size
if ( c + batch_size > = len ( os . listdir ( img_folder ) ) ) :
if c + batch_size > = len ( os . listdir ( img_folder ) ) :
c = 0
random . shuffle ( n )
yield img , mask
def otsu_copy ( img ) :
img_r = np . zeros ( img . shape )
img1 = img [ : , : , 0 ]
@ -186,8 +196,9 @@ def otsu_copy(img):
img_r [ : , : , 1 ] = threshold1
img_r [ : , : , 2 ] = threshold1
return img_r
def get_patches ( dir_img_f , dir_seg_f , img , label , height , width , indexer ) :
def get_patches ( dir_img_f , dir_seg_f , img , label , height , width , indexer ) :
if img . shape [ 0 ] < height or img . shape [ 1 ] < width :
img , label = do_padding ( img , label , height , width )
@ -220,7 +231,6 @@ def get_patches(dir_img_f,dir_seg_f,img,label,height,width,indexer):
index_y_u = img_h
index_y_d = img_h - height
img_patch = img [ index_y_d : index_y_u , index_x_d : index_x_u , : ]
label_patch = label [ index_y_d : index_y_u , index_x_d : index_x_u , : ]
@ -230,8 +240,8 @@ def get_patches(dir_img_f,dir_seg_f,img,label,height,width,indexer):
return indexer
def do_padding ( img , label , height , width ) :
def do_padding ( img , label , height , width ) :
height_new = img . shape [ 0 ]
width_new = img . shape [ 1 ]
@ -256,8 +266,6 @@ def do_padding(img,label,height,width):
def get_patches_num_scale ( dir_img_f , dir_seg_f , img , label , height , width , indexer , n_patches , scaler ) :
if img . shape [ 0 ] < height or img . shape [ 1 ] < width :
img , label = do_padding ( img , label , height , width )
@ -267,7 +275,6 @@ def get_patches_num_scale(dir_img_f,dir_seg_f,img,label,height,width,indexer,n_p
height_scale = int ( height * scaler )
width_scale = int ( width * scaler )
nxf = img_w / float ( width_scale )
nyf = img_h / float ( height_scale )
@ -294,7 +301,6 @@ def get_patches_num_scale(dir_img_f,dir_seg_f,img,label,height,width,indexer,n_p
index_y_u = img_h
index_y_d = img_h - height_scale
img_patch = img [ index_y_d : index_y_u , index_x_d : index_x_u , : ]
label_patch = label [ index_y_d : index_y_u , index_x_d : index_x_u , : ]
@ -307,6 +313,7 @@ def get_patches_num_scale(dir_img_f,dir_seg_f,img,label,height,width,indexer,n_p
return indexer
def get_patches_num_scale_new ( dir_img_f , dir_seg_f , img , label , height , width , indexer , scaler ) :
img = resize_image ( img , int ( img . shape [ 0 ] * scaler ) , int ( img . shape [ 1 ] * scaler ) )
label = resize_image ( label , int ( label . shape [ 0 ] * scaler ) , int ( label . shape [ 1 ] * scaler ) )
@ -320,7 +327,6 @@ def get_patches_num_scale_new(dir_img_f,dir_seg_f,img,label,height,width,indexer
height_scale = int ( height * 1 )
width_scale = int ( width * 1 )
nxf = img_w / float ( width_scale )
nyf = img_h / float ( height_scale )
@ -347,7 +353,6 @@ def get_patches_num_scale_new(dir_img_f,dir_seg_f,img,label,height,width,indexer
index_y_u = img_h
index_y_d = img_h - height_scale
img_patch = img [ index_y_d : index_y_u , index_x_d : index_x_u , : ]
label_patch = label [ index_y_d : index_y_u , index_x_d : index_x_u , : ]
@ -368,7 +373,6 @@ def provide_patches(dir_img,dir_seg,dir_flow_train_imgs,
scaling_bluring , scaling_binarization , rotation ,
rotation_not_90 , thetha , scaling_flip ,
augmentation = False , patches = False ) :
imgs_cv_train = np . array ( os . listdir ( dir_img ) )
segs_cv_train = np . array ( os . listdir ( dir_seg ) )
@ -376,30 +380,35 @@ def provide_patches(dir_img,dir_seg,dir_flow_train_imgs,
for im , seg_i in tqdm ( zip ( imgs_cv_train , segs_cv_train ) ) :
img_name = im . split ( ' . ' ) [ 0 ]
if not patches :
cv2 . imwrite ( dir_flow_train_imgs + ' /img_ ' + str ( indexer ) + ' .png ' , resize_image ( cv2 . imread ( dir_img + ' / ' + im ) , input_height , input_width ) )
cv2 . imwrite ( dir_flow_train_labels + ' /img_ ' + str ( indexer ) + ' .png ' , resize_image ( cv2 . imread ( dir_seg + ' / ' + img_name + ' .png ' ) , input_height , input_width ) )
cv2 . imwrite ( dir_flow_train_imgs + ' /img_ ' + str ( indexer ) + ' .png ' ,
resize_image ( cv2 . imread ( dir_img + ' / ' + im ) , input_height , input_width ) )
cv2 . imwrite ( dir_flow_train_labels + ' /img_ ' + str ( indexer ) + ' .png ' ,
resize_image ( cv2 . imread ( dir_seg + ' / ' + img_name + ' .png ' ) , input_height , input_width ) )
indexer + = 1
if augmentation :
if flip_aug :
for f_i in flip_index :
cv2 . imwrite ( dir_flow_train_imgs + ' /img_ ' + str ( indexer ) + ' .png ' ,
resize_image ( cv2 . flip ( cv2 . imread ( dir_img + ' / ' + im ) , f_i ) , input_height , input_width ) )
resize_image ( cv2 . flip ( cv2 . imread ( dir_img + ' / ' + im ) , f_i ) , input_height ,
input_width ) )
cv2 . imwrite ( dir_flow_train_labels + ' /img_ ' + str ( indexer ) + ' .png ' ,
resize_image ( cv2 . flip ( cv2 . imread ( dir_seg + ' / ' + img_name + ' .png ' ) , f_i ) , input_height , input_width ) )
resize_image ( cv2 . flip ( cv2 . imread ( dir_seg + ' / ' + img_name + ' .png ' ) , f_i ) ,
input_height , input_width ) )
indexer + = 1
if blur_aug :
for blur_i in blur_k :
cv2 . imwrite ( dir_flow_train_imgs + ' /img_ ' + str ( indexer ) + ' .png ' ,
( resize_image ( bluring ( cv2 . imread ( dir_img + ' / ' + im ) , blur_i ) , input_height , input_width ) ) )
( resize_image ( bluring ( cv2 . imread ( dir_img + ' / ' + im ) , blur_i ) , input_height ,
input_width ) ) )
cv2 . imwrite ( dir_flow_train_labels + ' /img_ ' + str ( indexer ) + ' .png ' ,
resize_image ( cv2 . imread ( dir_seg + ' / ' + img_name + ' .png ' ) , input_height , input_width ) )
resize_image ( cv2 . imread ( dir_seg + ' / ' + img_name + ' .png ' ) , input_height ,
input_width ) )
indexer + = 1
if binarization :
cv2 . imwrite ( dir_flow_train_imgs + ' /img_ ' + str ( indexer ) + ' .png ' ,
resize_image ( otsu_copy ( cv2 . imread ( dir_img + ' / ' + im ) ) , input_height , input_width ) )
@ -408,11 +417,6 @@ def provide_patches(dir_img,dir_seg,dir_flow_train_imgs,
resize_image ( cv2 . imread ( dir_seg + ' / ' + img_name + ' .png ' ) , input_height , input_width ) )
indexer + = 1
if patches :
indexer = get_patches ( dir_flow_train_imgs , dir_flow_train_labels ,
@ -422,8 +426,6 @@ def provide_patches(dir_img,dir_seg,dir_flow_train_imgs,
if augmentation :
if rotation :
indexer = get_patches ( dir_flow_train_imgs , dir_flow_train_labels ,
rotation_90 ( cv2 . imread ( dir_img + ' / ' + im ) ) ,
rotation_90 ( cv2 . imread ( dir_seg + ' / ' + img_name + ' .png ' ) ) ,
@ -432,7 +434,10 @@ def provide_patches(dir_img,dir_seg,dir_flow_train_imgs,
if rotation_not_90 :
for thetha_i in thetha :
img_max_rotated , label_max_rotated = rotation_not_90_func ( cv2 . imread ( dir_img + ' / ' + im ) , cv2 . imread ( dir_seg + ' / ' + img_name + ' .png ' ) , thetha_i )
img_max_rotated , label_max_rotated = rotation_not_90_func ( cv2 . imread ( dir_img + ' / ' + im ) ,
cv2 . imread (
dir_seg + ' / ' + img_name + ' .png ' ) ,
thetha_i )
indexer = get_patches ( dir_flow_train_imgs , dir_flow_train_labels ,
img_max_rotated ,
label_max_rotated ,
@ -445,13 +450,11 @@ def provide_patches(dir_img,dir_seg,dir_flow_train_imgs,
input_height , input_width , indexer = indexer )
if blur_aug :
for blur_i in blur_k :
indexer = get_patches ( dir_flow_train_imgs , dir_flow_train_labels ,
bluring ( cv2 . imread ( dir_img + ' / ' + im ) , blur_i ) ,
cv2 . imread ( dir_seg + ' / ' + img_name + ' .png ' ) ,
input_height , input_width , indexer = indexer )
if scaling :
for sc_ind in scales :
indexer = get_patches_num_scale_new ( dir_flow_train_imgs , dir_flow_train_labels ,
@ -464,15 +467,14 @@ def provide_patches(dir_img,dir_seg,dir_flow_train_imgs,
cv2 . imread ( dir_seg + ' / ' + img_name + ' .png ' ) ,
input_height , input_width , indexer = indexer )
if scaling_bluring :
for sc_ind in scales :
for blur_i in blur_k :
indexer = get_patches_num_scale_new ( dir_flow_train_imgs , dir_flow_train_labels ,
bluring ( cv2 . imread ( dir_img + ' / ' + im ) , blur_i ) ,
cv2 . imread ( dir_seg + ' / ' + img_name + ' .png ' ) ,
input_height , input_width , indexer = indexer , scaler = sc_ind )
input_height , input_width , indexer = indexer ,
scaler = sc_ind )
if scaling_binarization :
for sc_ind in scales :
@ -486,12 +488,7 @@ def provide_patches(dir_img,dir_seg,dir_flow_train_imgs,
for f_i in flip_index :
indexer = get_patches_num_scale_new ( dir_flow_train_imgs , dir_flow_train_labels ,
cv2 . flip ( cv2 . imread ( dir_img + ' / ' + im ) , f_i ) ,
cv2 . flip ( cv2 . imread ( dir_seg + ' / ' + img_name + ' .png ' ) , f_i ) ,
input_height , input_width , indexer = indexer , scaler = sc_ind )
cv2 . flip ( cv2 . imread ( dir_seg + ' / ' + img_name + ' .png ' ) ,
f_i ) ,
input_height , input_width , indexer = indexer ,
scaler = sc_ind )