def compute_homographies(self, outlier_algorithm=cv2.RANSAC, clean_keys=[]): """ For each edge in the (sub) graph, compute the homography Parameters ---------- outlier_algorithm : object An openCV outlier detections algorithm, e.g. cv2.RANSAC clean_keys : list of string keys to masking arrays (created by calling outlier detection) Returns ------- transformation_matrix : ndarray The 3x3 transformation matrix mask : ndarray Boolean array of the outliers """ for source, destination, attributes in self.edges_iter(data=True): matches = attributes['matches'] if clean_keys: mask = np.prod([attributes[i] for i in clean_keys], axis=0, dtype=np.bool) matches = matches[mask] full_mask = np.where(mask == True) s_coords = np.empty((len(matches), 2)) d_coords = np.empty((len(matches), 2)) for i, (idx, row) in enumerate(matches.iterrows()): s_idx = int(row['source_idx']) d_idx = int(row['destination_idx']) s_coords[i] = self.node[source]['keypoints'][s_idx].pt d_coords[i] = self.node[destination]['keypoints'][d_idx].pt transformation_matrix, ransac_mask = od.compute_homography(s_coords, d_coords) ransac_mask = ransac_mask.ravel() # Convert the truncated RANSAC mask back into a full length mask if clean_keys: mask[full_mask] = ransac_mask else: mask = ransac_mask attributes['homography'] = transformation_matrix attributes['ransac'] = mask
def compute_homography(self, method='ransac', clean_keys=[], pid=None, **kwargs): """ For each edge in the (sub) graph, compute the homography Parameters ---------- outlier_algorithm : object An openCV outlier detections algorithm, e.g. cv2.RANSAC clean_keys : list of string keys to masking arrays (created by calling outlier detection) Returns ------- transformation_matrix : ndarray The 3x3 transformation matrix mask : ndarray Boolean array of the outliers """ if hasattr(self, 'matches'): matches = self.matches else: raise AttributeError('Matches have not been computed for this edge') if clean_keys: matches, mask = self._clean(clean_keys) s_keypoints = self.source.keypoints.iloc[matches['source_idx'].values] d_keypoints = self.destination.keypoints.iloc[matches['destination_idx'].values] transformation_matrix, ransac_mask = od.compute_homography(s_keypoints[['x', 'y']].values, d_keypoints[['x', 'y']].values, **kwargs) ransac_mask = ransac_mask.ravel() # Convert the truncated RANSAC mask back into a full length mask if clean_keys: mask[mask == True] = ransac_mask else: mask = ransac_mask self.masks = ('ransac', mask) self.homography = Homography(transformation_matrix, s_keypoints[ransac_mask][['x', 'y']], d_keypoints[ransac_mask][['x', 'y']], mask=mask[mask == True].index) # Finalize the array to get custom attrs to propagate self.homography.__array_finalize__(self.homography)
def compute_homography(self, outlier_algorithm=cv2.RANSAC, clean_keys=[]): """ For each edge in the (sub) graph, compute the homography Parameters ---------- outlier_algorithm : object An openCV outlier detections algorithm, e.g. cv2.RANSAC clean_keys : list of string keys to masking arrays (created by calling outlier detection) Returns ------- transformation_matrix : ndarray The 3x3 transformation matrix mask : ndarray Boolean array of the outliers """ if hasattr(self, 'matches'): matches = self.matches else: raise AttributeError( 'Matches have not been computed for this edge') if clean_keys: mask = np.prod([self._mask_arrays[i] for i in clean_keys], axis=0, dtype=np.bool) matches = matches[mask] full_mask = np.where(mask == True) s_keypoints = self.source.keypoints.iloc[matches['source_idx'].values] d_keypoints = self.destination.keypoints.iloc[ matches['destination_idx'].values] transformation_matrix, ransac_mask = od.compute_homography( s_keypoints[['x', 'y']].values, d_keypoints[['x', 'y']].values) ransac_mask = ransac_mask.ravel() # Convert the truncated RANSAC mask back into a full length mask if clean_keys: mask[full_mask] = ransac_mask else: mask = ransac_mask self.masks = ('ransac', mask) self.homography = transformation_matrix
def compute_homography(self, outlier_algorithm=cv2.RANSAC, clean_keys=[]): """ For each edge in the (sub) graph, compute the homography Parameters ---------- outlier_algorithm : object An openCV outlier detections algorithm, e.g. cv2.RANSAC clean_keys : list of string keys to masking arrays (created by calling outlier detection) Returns ------- transformation_matrix : ndarray The 3x3 transformation matrix mask : ndarray Boolean array of the outliers """ if hasattr(self, 'matches'): matches = self.matches else: raise AttributeError('Matches have not been computed for this edge') if clean_keys: mask = np.prod([self._mask_arrays[i] for i in clean_keys], axis=0, dtype=np.bool) matches = matches[mask] full_mask = np.where(mask == True) s_keypoints = self.source.keypoints.iloc[matches['source_idx'].values] d_keypoints = self.destination.keypoints.iloc[matches['destination_idx'].values] transformation_matrix, ransac_mask = od.compute_homography(s_keypoints[['x', 'y']].values, d_keypoints[['x', 'y']].values) ransac_mask = ransac_mask.ravel() # Convert the truncated RANSAC mask back into a full length mask if clean_keys: mask[full_mask] = ransac_mask else: mask = ransac_mask self.masks = ('ransac', mask) self.homography = transformation_matrix