def computeForPatchImages(image_file_path: str, config: Dict, model: Any) -> np.array: """Computes descriptors for images containing patches to be described.""" # Load patch image img = cv2.imread(image_file_path, 0) # Assuming the patches are ordered vertically, and all patches are squares # of size MxM, find number of patches in image and compute the descriptor # for each patch. patch_size = img.shape[1] num_patches = np.int(img.shape[0] / patch_size) patches = [] for i in range(num_patches): patch = img[i * patch_size:(i + 1) * patch_size, :] # 65x65 Patch patch = io_utils.smart_scale(patch, 32, prevent_upscaling=True) # 32x32 patches.append(patch) # TODO: Muss hier das Patch noch rektifiziert werden? desc = compute_descriptors(model, patches, use_gpu=False) return desc
def compute(image_file_path: str, config: Dict, model: Any) -> np.array: """Computes descriptors from keypoints saved in a file.""" img = cv2.imread(image_file_path, 0) img = io_utils.smart_scale( img, config['max_size'], prevent_upscaling=True) if config['max_size'] is not None else img # Infer the path to the corresponding csv file for the keypoints. collection_name, set_name, image_name, _ = io_utils.get_path_components( image_file_path) # find path to keypoints file keypoints_file_path = io_utils.build_output_path( config['output_dir'], collection_name, set_name, 'keypoints', config['detector_name'], image_name, max_size=config['max_size'], max_num_keypoints=config['max_num_keypoints']) if not os.path.isfile(keypoints_file_path): print('Could not find keypoints in path: {}\n.Skip'.format( keypoints_file_path)) return None # Load keypoints from csv file as numpy array. kpts_numpy = io_utils.get_keypoints_from_csv(keypoints_file_path) # Convert numpy array to List of cv2.KeyPoint list kpts_cv2 = io_utils.numpy_to_cv2_kp(kpts_numpy) # Create iamge patches for each keypoint patches = create_patches(img, kpts_cv2, 42) # Save patches in tmp dir path_to_desc = os.path.join(config['tmp_dir_doap'], 'descriptors.csv') path_to_patches = os.path.join(config['tmp_dir_doap'], 'patches.csv') io_utils.save_patches_list(patches, path_to_patches) # Compute descritpors in matlab. Save result in tmp_dir # TODO: file paths for vlfeat, matconvnet and the model must be parameters subprocess.check_call([ 'matlab', '-nosplash', '-r', "use_doap_with_file('vlfeat-0.9.21', 'matconvnet-1.0-beta25', 'HPatches_ST_LM_128d.mat', '.', '{}', '{}');quit" .format(path_to_patches, path_to_desc) ]) # Load matlab results and return. desc = np.loadtxt(path_to_desc, delimiter=',') return desc
def detect(image_path: str, config: Dict, detector: Any) -> Tuple[List, np.array, None]: """Detects keypoints for a given input image. Draws keypoints into the image. Returns keypoints, heatmap and image with keypoints. """ img = cv2.imread(image_path, 0) img = io_utils.smart_scale( img, config['max_size'], prevent_upscaling=config['prevent_upscaling'] ) if config['max_size'] is not None else img # Get keypoints kpts = detector.detect(img, None) # Sort by response, take best n <= max_num_keypoints kpts.sort(key=lambda x: x.response, reverse=True) img_kp = io_utils.draw_keypoints(img, kpts, config) return (kpts, img_kp, None)
def compute(image_file_path: str, config: Dict, model: Any) -> np.array: """Computes descriptors from keypoints saved in a file.""" # Load image and scale appropiately. Image is later used to create patch, # which in turn is used to create the descriptor. img = cv2.imread(image_file_path, 0) img = io_utils.smart_scale( img, config['max_size'], prevent_upscaling=True) if config['max_size'] is not None else img # Infer the path to the corresponding csv file for the keypoints. collection_name, set_name, image_name, _ = io_utils.get_path_components( image_file_path) # find path to keypoints file keypoints_file_path = io_utils.build_output_path( config['output_dir'], collection_name, set_name, 'keypoints', config['detector_name'], image_name, max_size=config['max_size'], max_num_keypoints=config['max_num_keypoints']) if not os.path.isfile(keypoints_file_path): print('Could not find keypoints in path: {}\n.Skip'.format( keypoints_file_path)) return None # Load keypoints from csv file as numpy array. kpts_numpy = io_utils.get_keypoints_from_csv(keypoints_file_path) # Convert numpy array to List of cv2.KeyPoint list kpts_cv2 = io_utils.numpy_to_cv2_kp(kpts_numpy) # Create image patches for each keypoint patches = rectify_patches(img, kpts_cv2, 32, 3) #Compute descriptors desc = compute_descriptors(model, patches, use_gpu=False) return desc
def detect(image_path: str, config: Dict, model: SuperPointFrontend, size: int = None ) -> Tuple[List[cv2.KeyPoint], np.array, np.array, np.array]: """Computes the keypoints and descriptors for a given input image. Draws keypoints into the image. Returns keypoints, descriptors and image with keypoints. Arguments: image_path {np.array} -- Path to the image. model {superpoint_frontend.SuperPointFrontend} -- The SuperPoint keypoint detector and descriptor. config {Dict} -- Configuration object. See config_run_detector.py Returns: Tuple[List[cv2.KeyPoint], np.array, np.array, None] -- Returns tuple (keypoints, descriptors, image with keypoints, image of heatmap). """ img = cv2.imread(image_path, 0) img = io_utils.smart_scale( img, config['max_size'], prevent_upscaling=config['prevent_upscaling'] ) if config['max_size'] is not None else img _kp, desc, heatmap = detectAndCompute(img, config, model) # Sort by confidences, descending. _kp, desc = _sort_keypoints_and_descriptors_by_confidence(_kp, desc) # Take n-th best max_num_kp = config['max_num_keypoints'] if max_num_kp: _kp = _kp[:max_num_kp] desc = desc[:max_num_kp] # Convert to list of openCV's KeyPoint kp = kps2KeyPoints(_kp) img_kp = io_utils.draw_keypoints(img, kp, config) return (kp, desc, img_kp, heatmap)
def detect(image_path: str, config: Dict, model: CustomModelLift) -> None: """Detects keypoints for a given input image. Draws keypoints into the image. Returns keypoints, heatmap and image with keypoints. 1) Load and smart scale the image. 2) Save the resulting image in `tmp` folder. 3) Subprocess call to tf-lift for keypoints. Save output text file in `tmp`. 4) Load text file as as np.array with dimension [num_kp x 13] 5) Convert keypoints to list of cv2.Keypoint. 6) Draw list of cv2.KeyPoints into image. 7) Return KeyPoint list, descriptors and image with KeyPoints. """ lift_path = os.path.join(config['root_dir_lift'], 'tf-lift') # 1) img = cv2.imread(image_path, 0) img = io_utils.smart_scale( img, config['max_size'], prevent_upscaling=config['prevent_upscaling'] ) if config['max_size'] is not None else img kpts_numpy = chunkify_image(img, config, model) # Sort by response kpts_numpy = _sort_keypoints_by_response(kpts_numpy) # Take n-th best if config['max_num_keypoints']: kpts_numpy = kpts_numpy[:config['max_num_keypoints']] # 5) Convert to cv2.KeyPoint list kpts_cv2 = kp_list_2_opencv_kp_list(kpts_numpy) # 6) Draw keypoints in image img_kp = io_utils.draw_keypoints(img, kpts_cv2, config) return (kpts_cv2, img_kp, None)
def computeForPatchImages(image_file_path: str, config: Dict, model: Any) -> np.array: """Computes descriptors for images containing patches to be described.""" # Load patch image img = cv2.imread(image_file_path, 0) # Assuming the patches are ordered vertically, and all patches are squares # of size MxM, find number of patches in image and compute the descriptor # for each patch. patch_size = img.shape[1] num_patches = np.int(img.shape[0] / patch_size) patches = [] for i in range(num_patches): patch = img[i * patch_size:(i + 1) * patch_size, :] patch = io_utils.smart_scale(patch, 42) patches.append(patch) # Save patches in tmp dir path_to_desc = os.path.join(config['tmp_dir_doap'], 'descriptors.csv') path_to_patches = os.path.join(config['tmp_dir_doap'], 'patches.csv') io_utils.save_patches_list(patches, path_to_patches) # Compute descritpors in matlab. Save result in tmp_dir # TODO: file paths for vlfeat, matconvnet and the model must be parameters subprocess.check_call([ 'matlab', '-nosplash', '-r', "use_doap_with_file('vlfeat-0.9.21', 'matconvnet-1.0-beta25', 'HPatches_ST_LM_128d.mat', '.', '{}', '{}');quit" .format(path_to_patches, path_to_desc) ]) # Load matlab results and return. desc = np.loadtxt(path_to_desc, delimiter=',') return desc
def detect( image_path: str, config: dict) -> None: """Detects keypoints for a given input image. Draws keypoints into the image. Returns keypoints, heatmap and image with keypoints. Arguments: image_path {str} -- Path to the image. config {dict} -- General configuations. See config_run_detectors.py. Returns: Tuple[List[cv2.KeyPoint], np.array, (np.array | None) ] -- Returns list of cv2.KeyPoint, an image with the corresponding keypoints, and if available, an heatmap. 1) Create temporary folder `tmp` to save intermediate output. 2a) Load and smart scale the image 2b) Save the resulting image in `tmp`. 3a) Subprocess call to TILDE for keypoints. Save output in `tmp` 4a) Load keypoints from 'tmp' and convert keypoints to cv2.Keypoints. 4b) Draw list of cv2.KeyPoints into image. 5) Return KeyPoint list and image with keypoints. """ # 1) io_utils.create_dir(config['tmp_dir_tilde']) # 2) img = cv2.imread(image_path, 0) img = io_utils.smart_scale(img, config['max_size'], prevent_upscaling=config['prevent_upscaling']) if config['max_size'] is not None else img # 2b) tmp_filename = 'tmp_img.png' tmp_keypoints = 'keypoints.csv' tmp_heatmap = 'heatmap.csv' path_tmp_img = os.path.join(config['tmp_dir_tilde'], tmp_filename) path_tmp_kpts = os.path.join(config['tmp_dir_tilde'], tmp_keypoints) path_tmp_heatmap = os.path.join(config['tmp_dir_tilde'], tmp_heatmap) cv2.imwrite(path_tmp_img, img) # 3a) imageDir = config['tmp_dir_tilde'] outputDir = config['tmp_dir_tilde'] fileName = tmp_filename filterPath = '/home/tilde/TILDE/c++/Lib/filters' filterName = 'Mexico.txt' # Call use_tilde.cpp # The output will be saved into # - config['tmp_dir_tilde']/keypoints.csv and # - config['tmp_dir_tilde']/heatmap.csv subprocess.check_call([ './use_tilde', '--imageDir', imageDir, '--outputDir', outputDir, '--fileName', fileName, '--filterPath', filterPath, '--filterName', filterName]) # 4) kpts_file = np.loadtxt(path_tmp_kpts, dtype=int, comments='#', delimiter=', ') max_num_keypoints = config['max_num_keypoints'] if max_num_keypoints: kpts_file = kpts_file[:max_num_keypoints] kpts = [cv2.KeyPoint(x[0], x[1], _size=1) for x in kpts_file] heatmap = np.loadtxt(path_tmp_heatmap, dtype=float, comments='# ', delimiter=', ') img_kp = io_utils.draw_keypoints(img, kpts, config) return (kpts, img_kp, heatmap)
def compute(image_file_path: str, config: Dict, model: Any) -> np.array: """ Computes the descriptors for all keypoints in a keypoint .csv file. 1) Check if keypoint file exisits. 2) Load and scale image. 2b) Save image in tmp dir. 3) Load .csv file mit keypoints as cv2.KeyPoint list. 4) ...and convert it with correct format for LIFT. 5) Save keypoint list to text in `tmp` dir as .txt 6) Compute orientation 7) compute descriptors 8) Load descriptors as numpy """ # Build paths lift_path = os.path.join(config['root_dir_lift'], 'tf-lift') path_tmp_img = os.path.join(config['tmp_dir_lift'], 'tmp_img.png') path_tmp_kpts = os.path.join(config['tmp_dir_lift'], 'tmp_kpts.txt') path_tmp_ori = os.path.join(config['tmp_dir_lift'], 'tmp_ori.txt') path_tmp_desc = os.path.join(config['tmp_dir_lift'], 'tmp_desc.h5') # Infer the path to the corresponding csv file for the keypoints. collection_name, set_name, image_name, _ = io_utils.get_path_components( image_file_path) # find path to keypoints file keypoints_file_path = io_utils.build_output_path( config['output_dir'], collection_name, set_name, 'keypoints', config['detector_name'], image_name, max_size=config['max_size'], max_num_keypoints=config['max_num_keypoints']) # 1) Check if keypoint file exists. if not os.path.isfile(keypoints_file_path): print('Could not find keypoints in path: {}\n.Skip'.format( keypoints_file_path)) return None # 2) Load and scale image img = cv2.imread(image_file_path, 0) img = io_utils.smart_scale( img, config['max_size'], prevent_upscaling=config['prevent_upscaling'] ) if config['max_size'] is not None else img # 2b) Write image in tmp dir cv2.imwrite(path_tmp_img, img) # 3) Load .csv file mit keypoints as cv2.KeyPoint list. kpts_numpy = io_utils.get_keypoints_from_csv(keypoints_file_path) # Convert numpy array to List of cv2.KeyPoint list kpts_cv2 = io_utils.numpy_to_cv2_kp(kpts_numpy) # 4) Convert to LIFT format kpts_lift = opencv_kp_list_2_kp_list(kpts_cv2) # 5) Save keypoint list as .txt file for LIFT saveKpListToTxt(kpts_lift, None, path_tmp_kpts) try: # 6) Orientation subprocess.check_call([ 'python', 'main.py', '--subtask=ori', '--test_img_file={}'.format(path_tmp_img), '--test_out_file={}'.format(path_tmp_ori), '--test_kp_file={}'.format(path_tmp_kpts) ], cwd=lift_path) # 7) Descriptors subprocess.check_call([ 'python', 'main.py', '--subtask=desc', '--test_img_file={}'.format(path_tmp_img), '--test_out_file={}'.format(path_tmp_desc), '--test_kp_file={}'.format(path_tmp_ori) ], cwd=lift_path) except Exception: print( 'Could not compute descriptros for image {} at max_size {}. Skip.'. format(image_file_path, config['max_size'])) return None # 8) f = h5py.File(path_tmp_desc, 'r') descriptors = np.array(list(f['descriptors'])) return descriptors