def downsample_gt_data(image, metadata, gt_polygons, normed_disp_field_maps, downsampling_factor): # First, correct the downsampling_factor so that: # A downsampling_factor of 1 results in a final pixel_size equal to config.REFERENCE_PIXEL_SIZE # A downsampling_factor of 2 results in a final pixel_size equal to 2 * config.REFERENCE_PIXEL_SIZE corrected_downsampling_factor = downsampling_factor * config.REFERENCE_PIXEL_SIZE / metadata[ "pixelsize"] scale = 1 / corrected_downsampling_factor downsampled_image = skimage.transform.rescale(image, scale, order=3, preserve_range=True, multichannel=True) downsampled_image = downsampled_image.astype(image.dtype) downsampled_gt_polygons = polygon_utils.rescale_polygon(gt_polygons, scale) downsampled_normed_disp_field_maps = np.empty( (normed_disp_field_maps.shape[0], round(normed_disp_field_maps.shape[1] / corrected_downsampling_factor), round(normed_disp_field_maps.shape[2] / corrected_downsampling_factor), normed_disp_field_maps.shape[3])) for i in range(normed_disp_field_maps.shape[0]): downsampled_normed_disp_field_maps[i] = skimage.transform.rescale( normed_disp_field_maps[i], scale, order=3, preserve_range=True, multichannel=True) return downsampled_image, downsampled_gt_polygons, downsampled_normed_disp_field_maps
def rescale_data(image, polygons, scale): downsampled_image = skimage.transform.rescale(image, scale, order=3, preserve_range=True, multichannel=True, anti_aliasing=True) downsampled_image = downsampled_image.astype(image.dtype) downsampled_polygons = polygon_utils.rescale_polygon(polygons, scale) return downsampled_image, downsampled_polygons
def downsample_gt_data(image, gt_polygons, normed_disp_field_maps, downsampling_factor): downsampled_image = skimage.transform.rescale(image, 1 / downsampling_factor, order=3, preserve_range=True, multichannel=True) downsampled_image = downsampled_image.astype(image.dtype) downsampled_gt_polygons = polygon_utils.rescale_polygon(gt_polygons, 1 / downsampling_factor) downsampled_normed_disp_field_maps = np.empty((normed_disp_field_maps.shape[0], round(normed_disp_field_maps.shape[1] / downsampling_factor), round(normed_disp_field_maps.shape[2] / downsampling_factor), normed_disp_field_maps.shape[3])) for i in range(normed_disp_field_maps.shape[0]): downsampled_normed_disp_field_maps[i] = skimage.transform.rescale(normed_disp_field_maps[i], 1 / downsampling_factor, order=3, preserve_range=True, multichannel=True) return downsampled_image, downsampled_gt_polygons, downsampled_normed_disp_field_maps
def sample_patches(params): raw_dirpath, tile_info, ds_fac, size, count, seed = params im_filepath = read.get_image_filepath(raw_dirpath, tile_info["city"], tile_info["number"]) im_size = image_utils.get_image_size(im_filepath) polygon_list = read.load_polygons(raw_dirpath, read.POLYGON_DIRNAME, tile_info["city"], tile_info["number"]) # Rescale data corrected_factor = ds_fac * REFERENCE_PIXEL_SIZE / tile_info["pixelsize"] scale_factor = 1 / corrected_factor im_size = (int(np.round(im_size[0] * scale_factor)), int(np.round(im_size[1] * scale_factor))) ds_polygon_list = polygon_utils.rescale_polygon(polygon_list, 1 / corrected_factor) bbox_list = image_utils.compute_patch_boundingboxes(im_size, size, size) random.seed(seed) random.shuffle(bbox_list) # Sample <count> patches in tile, making sure there is at least a polygon inside sampled_bbox_list = [] for bbox in bbox_list: bbox_polygon_list = polygon_utils.filter_polygons_in_bounding_box( ds_polygon_list, bbox) if 1 <= len(bbox_polygon_list): sampled_bbox_list.append(bbox) if count <= len(sampled_bbox_list): break tile_info["bbox_list"] = sampled_bbox_list tile_info["scale_factor"] = scale_factor return tile_info