def random_transform_group_entry(self, image, annotations): # randomly transform both image and annotations if self.transform_generator: _, _, transform_mat = next(self.transform_generator) transform = adjust_transform_for_image(transform_mat, image, self.transform_parameters.relative_translation) image = apply_transform(transform, image, self.transform_parameters) # Transform the bounding boxes in the annotations. annotations = annotations.copy() for index in range(annotations.shape[0]): annotations[index, :4] = transform_aabb(transform, annotations[index, :4]) return image, annotations
def random_transform_group_entry(self, image, annotations): """ Randomly transforms image and annotation. """ # randomly transform both image and annotations if transform is not None or self.transform_generator: if transform is None: transform = adjust_transform_for_image(next(self.transform_generator), image, self.transform_parameters.relative_translation) # apply transformation to image image = apply_transform(transform, image, self.transform_parameters) # Transform the bounding boxes in the annotations. annotations['bboxes'] = annotations['bboxes'].copy() for index in range(annotations['bboxes'].shape[0]): annotations['bboxes'][index, :] = transform_aabb(transform, annotations['bboxes'][index, :]) return image, annotations
min_rotation=-0.1, max_rotation=0.1, min_translation=(-0.1, -0.1), max_translation=(0.1, 0.1), min_shear=-0.1, max_shear=0.1, min_scaling=(0.9, 0.9), max_scaling=(1.1, 1.1), flip_x_chance=0.5, flip_y_chance=0.5, ) image = img2 transform_parameters = TransformParameters() transform = adjust_transform_for_image( next(transform_generator), image, transform_parameters.relative_translation) img_ = apply_transform(transform, img, transform_parameters) image_ = apply_transform(transform, image, transform_parameters) cv2.imshow('img original', img[:, :, :3]) cv2.waitKey(0) print(image_.shape) print(image_[:, :, 0]) print(img_[:, :, 0]) print(image_.dtype, img.dtype) cv2.imshow('img2', image_[:, :, 0:3]) cv2.waitKey(0) cv2.imshow('img3', img_) cv2.waitKey(0) cv2.destroyAllWindows()
def random_transform_group_entry(self, image, annotations, transform=None): """!@brief Randomly transforms image and annotation. """ #------------------------------------------- # # Draw grid lines for print/debug purposes # grid_size = 20 # for i in range(0, image.shape[1], grid_size): # cv2.line(image, (i, 0), (i, image.shape[0]), color=(50,50,50)) # for j in range(0, image.shape[0], grid_size): # cv2.line(image, (0, j), (image.shape[1], j), color=(50,50,50)) # # # Draw the annotations # from utils.visualization import draw_annotations # draw_annotations(image, # annotations, # color=(0, 0, 255)) # # # Show input image # cv2.imshow("Input", image) #------------------------------------------- # Randomly apply photometric distortions if self.photometric: # Apply photometric distortion to the image image, annotations = photometric_distortions(image, annotations) # Randomly apply point-spread-function (PSF) # blurring to simulate animal motion if self.motion: # Create a random PSF kernel kernel = createPSFKernel(image.shape) # Kernel size can be small enough (< 3) # to not apply the motion blur. if kernel is not None: # Apply PSF blur to the image image = apply_motion_blur(image, kernel) # Adjust bounding boxes accordingly annotations['bboxes'] = annotations['bboxes'].copy() for index in range(annotations['bboxes'].shape[0]): annotations['bboxes'][index, :] = motion_distortion_aabb( image.shape, kernel, annotations['bboxes'][index, :]) #------------------------------------------- # # Draw the annotations # draw_annotations(image, # annotations, # color=(255, 0, 0)) # cv2.imshow("PSF_Blur", image) #------------------------------------------- # Randomly apply smooth elastic deformation to the image if self.deformable: # Get deformable array deformable_transform = adjust_deformable_for_image( image.shape, self.alpha, self.sigma) # Apply transformation to the image image = apply_deformable_transform(image, deformable_transform) # Transform the bounding boxes in the annotations annotations['bboxes'] = annotations['bboxes'].copy() n_bboxes = annotations['bboxes'].shape[0] # Non parallelized option for index in range(n_bboxes): annotations['bboxes'][index, :] = deformable_transform_aabb( image.shape, deformable_transform, annotations['bboxes'][index, :]) ## Parallelized option ## See: https://www.machinelearningplus.com/python/parallel-processing-python/ ## Init multiprocessing.Pool() #pool = mp.Pool(mp.cpu_count()) #results = [pool.apply_async(deformable_transform_aabb, #args=(image.shape, #deformable_transform, #annotations['bboxes'][i, :]) #) for i in range(n_bboxes)] ## Close Pool and let all the processes complete #pool.close() #pool.join() # postpones the execution of next line of code until all processes in the queue are done. ## <results> is a list of pool.ApplyResult objects ## NOTE: This attribution is not ORDERED since async ## is being used to parallelize the function #for i in range(n_bboxes): #annotations['bboxes'][i, :] = results[i].get() #------------------------------------------- # # Draw the annotations # draw_annotations(image, # annotations, # color=(255, 0, 0)) # cv2.imshow("Middle", image) #------------------------------------------- # Randomly transform both image and annotations if transform is not None or self.transform_generator: # Affine + flip transformation if transform is None: transform = adjust_transform_for_image( next(self.transform_generator), image, self.transform_parameters.relative_translation) # Apply transformation to the image image = apply_transform(transform, image, self.transform_parameters) # Transform the bounding boxes in the annotations annotations['bboxes'] = annotations['bboxes'].copy() for index in range(annotations['bboxes'].shape[0]): annotations['bboxes'][index, :] = transform_aabb( transform, annotations['bboxes'][index, :]) #------------------------------------------- # # Draw the annotations # from utils.visualization import draw_annotations # draw_annotations(image, # annotations, # color=(0, 255, 0)) # # Show output image after data augmentation # cv2.imshow("Output", image) # cv2.waitKey(0) #------------------------------------------- return image, annotations