def create_masks(bldg_footprints_shp=BUILDING_SHAPEFILE): '''Create masks for images in a directory given the building footprints shapefile Args: bldg_footprints_shp: building footprint shapefile ''' files = files_absolute_path(INPUT_PATH) with fiona.open(bldg_footprints_shp, "r") as shapefile: geoms = [feature["geometry"] for feature in shapefile] for file in files: out_file = get_outfile(file, MASK_BASE_PATH) mask, mask_meta = create_one_mask(file, geoms) save_mask(out_file, mask, mask_meta)
def evaluate_query_set(self, query_set_dict, has_masks, distance_eq): gt = query_set_dict["gt"] predictions = [] for file, im in query_set_dict["images"].items(): mask = None if has_masks: mask_filename = os.path.join( self.output_folder, file.split("/")[-1].split(".")[0] + ".png", ) im, mask = mask_background(im) gt_mask = query_set_dict["masks"][file.replace("jpg", "png")] self.calc_mask_metrics(gt_mask[..., 0] / 255, mask / 255) if self.opt.save: save_mask(mask_filename, mask) fv = self.calc_FV_query(im, mask) distances = calculate_distances(self.feature_vector_protoypes, fv, distance_eq) predictions.append(list(distances.argsort()[:10])) if self.opt.save: save_predictions( os.path.join( self.output_folder, "result_{}.pkl".format(int(has_masks) + 1), ), predictions, ) map_k = mapk(gt, predictions) return map_k
def eval_set(loader, gt_correspondences, bbdd_fvs, opt): masks_metrics = {"precision": [], "recall": [], "f1": []} ious = [] predictions = [] set_bboxes = [] for name, query_image, gt_mask in loader: if opt.apply_denoise: query_image, Noise_level_before, Noise_level_after, blur_type_last = detect_denoise( query_image, opt.blur_type) # transform to another color space multiple_painting, split_point, bg_mask = detect_paintings(query_image) bboxes, bbox_mask = detect_bboxes(query_image) res_mask = bg_mask.astype(bool) ^ bbox_mask.astype( bool) if loader.detect_bboxes else bg_mask if loader.compute_masks: if loader.evaluate: calc_mask_metrics(masks_metrics, gt_mask / 255, bg_mask) if opt.save: mask_name = name.split("/")[-1].replace(".jpg", ".png") save_mask( os.path.join(opt.output, loader.root.split("/")[-1], mask_name), res_mask * 255) # cropped sets, no need to mask image for retrieval if gt_mask is None: res_mask = None if loader.detect_bboxes: set_bboxes.append(bboxes) # change colorspace before computing feature vector query_image = transform_color( query_image, opt.color) if opt.color is not None else query_image if multiple_painting and gt_mask is not None: im_preds = [] left_paint = np.zeros_like(res_mask) right_paint = np.zeros_like(res_mask) left_paint[:, split_point:] = res_mask[:, split_point:] right_paint[:, :split_point] = res_mask[:, :split_point] res_masks = [left_paint, right_paint] for submasks in res_masks: query_fv = calc_FV(query_image, opt, submasks).ravel() distances = calculate_distances(bbdd_fvs, query_fv, mode=opt.dist) im_preds.append((distances.argsort()[:10]).tolist()) predictions.append(im_preds) else: query_fv = calc_FV(query_image, opt, res_mask).ravel() distances = calculate_distances(bbdd_fvs, query_fv, mode=opt.dist) predictions.append((distances.argsort()[:10]).tolist()) if opt.save: save_predictions( "{}/{}/result.pkl".format(opt.output, loader.root.split("/")[-1]), predictions) save_predictions( "{}/{}/text_boxes.pkl".format(opt.output, loader.root.split("/")[-1]), set_bboxes) map_k = { i: mapk(gt_correspondences, predictions, k=i) for i in [10, 3, 1] } if loader.evaluate else None avg_mask_metrics = averge_masks_metrics( masks_metrics) if loader.evaluate else None return map_k, avg_mask_metrics
result = cv2.morphologyEx(result, cv2.MORPH_CLOSE, kernel) result = cv2.medianBlur(result, 17) result = cv2.morphologyEx(result, cv2.MORPH_CLOSE, kernel) return result if __name__ == "__main__": parser = argparse.ArgumentParser(description="Script for segmentation of Gall Bladder Images") parser.add_argument("--img_path", type=str, default="img/", help="Path for the image folder") parser.add_argument("--det_path", type=str, default="det/", help="Path for the output masks folder") args = parser.parse_args() if(not os.path.exists(args.det_path)): os.mkdir(args.det_path) path = os.walk(args.img_path) for _, _, files in path: for file in files: output_name = file.split(".")[0] + ".png" image = load_image(args.img_path + file) raw_image = image.copy() kernel = np.array([[-1, -1, -1], [-1, 9, -1], [-1, -1, -1]]) image = cv2.filter2D(image, -1, kernel) image = cv2.GaussianBlur(image, (5, 5), 0) mask = segment_image(image, raw_image) # plot_image(mask) save_mask(mask, args.det_path, output_name) print("++++++++++++++++++++++++++++++++++++++++++++++++++++++")
for file in file_list: with rasterio.open(file, 'r') as ds: image = ds.read() # read all raster values image_meta = ds.meta.copy() H = image.shape[1] W = image.shape[2] image_meta.update({ "driver": "GTiff", "height": H, "width": W, "transform": ds.transform, "count": 1 }) prediction = make_prediction_cropped(unet_model, image, initial_size=(224, 224), final_size=(224 - 20, 224 - 20), num_masks=1, num_channels=3) predicted_mask = pred_mask(prediction, 0.5) # Post-processing image_denoise = grow(predicted_mask, 10) image_grow = denoise(image_denoise, 30) # Save predicted mask out_file = get_outfile(file, OUTPUT_PREDICTED_MASK) save_mask(out_file, image_grow, image_meta)
beta - smoothness term \n\ tau - step for gradient descent \n\ w_line - intensity weight for external energy \n\ w_edge - edge weight for external energy \n\ kappa - balloon force weight') sys.exit(0) input_image, initial_snake, \ output_image, alpha, beta, \ tau, w_line, w_edge, kappa = tuple(sys.argv[1:10]) image = skimage.img_as_float(skio.imread(input_image)) X = np.array([ np.array(x.replace('\n', '').split(' ')) for x in open(initial_snake) ]).astype(np.float64) C = active_contours(image, X[:-1], alpha, beta, tau, w_line, w_edge, kappa) C = np.append(C, [C[0]], axis=0) #utils.save_mask_withimg('result.png', C, np.uint8(image * 255)) utils.save_mask(output_image, C, np.uint8(image * 255)) iou = calc_iou(output_image, input_image.replace('.png', '_mask.png')) print(iou) #X = active_contour(image, X, float(alpha), float(beta), float(w_line), float(w_edge), float(tau)) #utils.save_mask_withimg(output_image, X, np.uint8(image * 255)) #utils.save_mask('genmask.png', X, np.uint8(image * 255)) #calc_iou('genmask.png', 'images/astranaut_mask.png')