本文整理匯總了Python中cv2.RANSAC屬性的典型用法代碼示例。如果您正苦於以下問題:Python cv2.RANSAC屬性的具體用法?Python cv2.RANSAC怎麽用?Python cv2.RANSAC使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在類cv2
的用法示例。
在下文中一共展示了cv2.RANSAC屬性的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: findHomography
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import RANSAC [as 別名]
def findHomography(image_1_kp, image_2_kp, matches):
image_1_points = np.zeros((len(matches), 1, 2), dtype=np.float32)
image_2_points = np.zeros((len(matches), 1, 2), dtype=np.float32)
for i in range(0,len(matches)):
image_1_points[i] = image_1_kp[matches[i].queryIdx].pt
image_2_points[i] = image_2_kp[matches[i].trainIdx].pt
homography, mask = cv2.findHomography(image_1_points, image_2_points, cv2.RANSAC, ransacReprojThreshold=2.0)
return homography
#
# Align the images so they overlap properly...
#
#
示例2: removeOutliersByMask
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import RANSAC [as 別名]
def removeOutliersByMask(self, mask):
if mask is not None:
n = self.kpn_cur.shape[0]
mask_index = [ i for i,v in enumerate(mask) if v > 0]
self.kpn_cur = self.kpn_cur[mask_index]
self.kpn_ref = self.kpn_ref[mask_index]
if self.des_cur is not None:
self.des_cur = self.des_cur[mask_index]
if self.des_ref is not None:
self.des_ref = self.des_ref[mask_index]
if kVerbose:
print('removed ', n-self.kpn_cur.shape[0],' outliers')
# fit essential matrix E with RANSAC such that: p2.T * E * p1 = 0 where E = [t21]x * R21
# out: [Rrc, trc] (with respect to 'ref' frame)
# N.B.1: trc is estimated up to scale (i.e. the algorithm always returns ||trc||=1, we need a scale in order to recover a translation which is coherent with previous estimated poses)
# N.B.2: this function has problems in the following cases: [see Hartley/Zisserman Book]
# - 'geometrical degenerate correspondences', e.g. all the observed features lie on a plane (the correct model for the correspondences is an homography) or lie on a ruled quadric
# - degenerate motions such a pure rotation (a sufficient parallax is required) or an infinitesimal viewpoint change (where the translation is almost zero)
# N.B.3: the five-point algorithm (used for estimating the Essential Matrix) seems to work well in the degenerate planar cases [Five-Point Motion Estimation Made Easy, Hartley]
# N.B.4: as reported above, in case of pure rotation, this algorithm will compute a useless fundamental matrix which cannot be decomposed to return the rotation
示例3: frame_homography
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import RANSAC [as 別名]
def frame_homography(totalPts, homTh):
"""
Filter foreground points i.e. the outlier points found by fitting
homography using RANSAC
Input:
totalPts: (numAllPoints, 4): x0, y0, x1, y1
fgPts: (numAllPoints, 4): x0, y0, x1, y1
"""
if totalPts.ndim != 2 or totalPts.shape[0] < 8 or homTh < 0:
return totalPts
import cv2
p1 = totalPts[:, :2].astype('float')
p2 = totalPts[:, 2:4].astype('float')
_, status = cv2.findHomography(
p1, p2, cv2.RANSAC, ransacReprojThreshold=homTh)
fgPts = totalPts[status[:, 0] == 0, :]
return fgPts
示例4: getHomography
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import RANSAC [as 別名]
def getHomography(self, rightKps, rightDescriptor):
rawMatches = self.matcher.knnMatch(self.leftDescriptor, rightDescriptor, 2)
matches = []
for m in rawMatches:
if(len(m)==2 and m[0].distance < m[1].distance*self.ratio):
matches.append((m[0].trainIdx, m[0].queryIdx))
if(len(matches) >=4):
# print(matches)
ptsB = np.float32([self.leftKps[i] for (_, i) in matches])
ptsA = np.float32([rightKps[i] for (i, _) in matches])
# ptsB = H*ptsA
H, status = cv2.findHomography(ptsA, ptsB, cv2.RANSAC, self.reprojThresh)
return H
return None
示例5: compute_homography_error
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import RANSAC [as 別名]
def compute_homography_error(kpts1, kpts2, matches, shape2, H_gt):
if matches.shape[0] == 0:
return False, None
kpts1 = kpts1[matches[:, 0]]
kpts2 = kpts2[matches[:, 1]]
H, _ = cv2.findHomography(kpts2, kpts1, cv2.RANSAC, 3.0)
if H is None:
return None
w, h = shape2
corners2 = to_homogeneous(
np.array([[0, 0], [0, h-1], [w-1, h-1], [w-1, 0]]))
corners1_gt = np.dot(corners2, np.transpose(H_gt))
corners1_gt = corners1_gt[:, :2] / corners1_gt[:, 2:]
corners1 = np.dot(corners2, np.transpose(H))
corners1 = corners1[:, :2] / corners1[:, 2:]
mean_dist = np.mean(np.linalg.norm(corners1 - corners1_gt, axis=1))
return mean_dist
示例6: findHomography
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import RANSAC [as 別名]
def findHomography(self, i1, i2, pairs):
src = []
dst = []
for pair in pairs:
c1 = i1.coord_list[pair[0]]
c2 = i2.coord_list[pair[1]]
src.append( c1 )
dst.append( c2 )
#H, status = cv2.findHomography(np.array([src]).astype(np.float32),
# np.array([dst]).astype(np.float32),
# cv2.RANSAC, 5.0)
H, status = cv2.findHomography(np.array([src]).astype(np.float32),
np.array([dst]).astype(np.float32))
#print str(affine)
return H
# compare against best 'placed' image (averaging transform
# matrices together directly doesn't do what we want)
示例7: match
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import RANSAC [as 別名]
def match(self, key_a, fea_a, key_b, fea_b):
# Compute the raw matches and initialize the list of actual matches
matcher = cv2.DescriptorMatcher_create(self.distance_method)
raw_matches = matcher.knnMatch(fea_b, fea_a, 2)
matches = []
# Loop over the raw matches
for match in raw_matches:
# Ensure the distance is within a certain ratio of each other
if len(match) == 2 and match[0].distance < match[1].distance * self.ratio:
matches.append((match[0].trainIdx, match[0].queryIdx))
# Check to see if there are enough matches to process
if len(matches) > self.min_matches:
# Construct the two sets of points
poi_a = np.float32([key_a[i] for (i, _) in matches])
poi_b = np.float32([key_b[j] for (_, j) in matches])
# Compute the homography between the two sets of points and compute the ratio of matched points
(_, status) = cv2.findHomography(poi_a, poi_b, cv2.RANSAC, 4.0)
# Return the ratio of the number of matched keypoints to the total number of keypoints
return float(status.sum()) / status.size
# No matches were found
return -1.0
示例8: track
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import RANSAC [as 別名]
def track(self, frame):
'''Returns a list of detected TrackedTarget objects'''
self.frame_points, frame_descrs = self.detect_features(frame)
if len(self.frame_points) < MIN_MATCH_COUNT:
return []
matches = self.matcher.knnMatch(frame_descrs, k = 2)
matches = [m[0] for m in matches if len(m) == 2 and m[0].distance < m[1].distance * 0.75]
if len(matches) < MIN_MATCH_COUNT:
return []
matches_by_id = [[] for _ in xrange(len(self.targets))]
for m in matches:
matches_by_id[m.imgIdx].append(m)
tracked = []
for imgIdx, matches in enumerate(matches_by_id):
if len(matches) < MIN_MATCH_COUNT:
continue
target = self.targets[imgIdx]
p0 = [target.keypoints[m.trainIdx].pt for m in matches]
p1 = [self.frame_points[m.queryIdx].pt for m in matches]
p0, p1 = np.float32((p0, p1))
H, status = cv2.findHomography(p0, p1, cv2.RANSAC, 3.0)
status = status.ravel() != 0
if status.sum() < MIN_MATCH_COUNT:
continue
p0, p1 = p0[status], p1[status]
x0, y0, x1, y1 = target.rect
quad = np.float32([[x0, y0], [x1, y0], [x1, y1], [x0, y1]])
quad = cv2.perspectiveTransform(quad.reshape(1, -1, 2), H).reshape(-1, 2)
track = TrackedTarget(target=target, p0=p0, p1=p1, H=H, quad=quad)
tracked.append(track)
tracked.sort(key = lambda t: len(t.p0), reverse=True)
return tracked
示例9: match_and_draw
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import RANSAC [as 別名]
def match_and_draw(win):
with Timer('matching'):
raw_matches = matcher.knnMatch(desc1, trainDescriptors = desc2, k = 2) #2
p1, p2, kp_pairs = filter_matches(kp1, kp2, raw_matches)
if len(p1) >= 4:
H, status = cv2.findHomography(p1, p2, cv2.RANSAC, 5.0)
print('%d / %d inliers/matched' % (np.sum(status), len(status)))
# do not draw outliers (there will be a lot of them)
kp_pairs = [kpp for kpp, flag in zip(kp_pairs, status) if flag]
else:
H, status = None, None
print('%d matches found, not enough for homography estimation' % len(p1))
vis = explore_match(win, img1, img2, kp_pairs, None, H)
示例10: match_and_draw
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import RANSAC [as 別名]
def match_and_draw(win):
print('matching...')
raw_matches = matcher.knnMatch(desc1, trainDescriptors = desc2, k = 2) #2
p1, p2, kp_pairs = filter_matches(kp1, kp2, raw_matches)
if len(p1) >= 4:
H, status = cv2.findHomography(p1, p2, cv2.RANSAC, 5.0)
print('%d / %d inliers/matched' % (np.sum(status), len(status)))
else:
H, status = None, None
print('%d matches found, not enough for homography estimation' % len(p1))
vis = explore_match(win, img1, img2, kp_pairs, status, H)
示例11: _find_homography
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import RANSAC [as 別名]
def _find_homography(self, sch_pts, src_pts):
"""多組特征點對時,求取單向性矩陣."""
try:
M, mask = cv2.findHomography(sch_pts, src_pts, cv2.RANSAC, 5.0)
except Exception:
import traceback
traceback.print_exc()
raise HomographyError("OpenCV error in _find_homography()...")
else:
if mask is None:
raise HomographyError("In _find_homography(), find no transfomation matrix...")
else:
return M, mask
示例12: _find_homography
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import RANSAC [as 別名]
def _find_homography(sch_pts, src_pts):
"""多組特征點對時,求取單向性矩陣."""
try:
M, mask = cv2.findHomography(sch_pts, src_pts, cv2.RANSAC, 5.0)
except Exception:
import traceback
traceback.print_exc()
raise HomographyError("OpenCV error in _find_homography()...")
else:
if mask is None:
raise HomographyError("In _find_homography(), find no mask...")
else:
return M, mask
示例13: ransac_orb_distance
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import RANSAC [as 別名]
def ransac_orb_distance(self, pic_package_from: Dict, pic_package_to: Dict) -> Dict[str, sd.AlgoMatch]:
"""
Distance between two provided pictures (dicts) with RANSAC-ORB methods
:param pic_package_from: first picture dict
:param pic_package_to: second picture dict
:return: A dictionary of algo name to the match detail (distance, decision ..)
"""
answer = {}
self.logger.info("RANSAC-Orb distance computation ... ")
# Verify if what is needed to compute it is present
if pic_package_from.get("ORB_DESCRIPTORS", None) is None \
or pic_package_to.get("ORB_DESCRIPTORS", None) is None:
self.logger.warning(f"RANSAC-ORB descriptors are NOT presents in the results.")
raise AlgoFeatureNotPresentError("None RANSAC-ORB descriptors in orb distance.")
# Verify if what is needed to compute it is present
if pic_package_from.get("ORB_KEYPOINTS", None) is None \
or pic_package_to.get("ORB_KEYPOINTS", None) is None:
self.logger.warning(f"RANSAC-ORB keypoints are NOT presents in the results.")
raise AlgoFeatureNotPresentError("None RANSAC-ORB keypoints in orb distance.")
# Add result for enabled algorithms
try:
if self.fe_conf.RANSAC_ORB.get("is_enabled", False):
answer = self.add_results(self.fe_conf.RANSAC_ORB, pic_package_from, pic_package_to, answer)
except Exception as e:
self.logger.error(traceback.print_tb(e.__traceback__))
self.logger.error("Error during RANSAC-orb distance calculation : " + str(e))
return answer
示例14: filter_matches
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import RANSAC [as 別名]
def filter_matches(matches: List, matches_threshold_to_accelerate: float) -> List:
# Output a list of filtered matches, according to distance
# Do remove the farthest matches to greatly accelerate RANSAC
# From : http://answers.opencv.org/question/984/performance-of-findhomography/
diminished_matches = []
for m in matches:
if m.distance < matches_threshold_to_accelerate:
diminished_matches.append(m)
return diminished_matches
示例15: find_homography
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import RANSAC [as 別名]
def find_homography(keypoints_pic1, keypoints_pic2, matches) -> (List, np.float32, np.float32):
# Find an Homography matrix between two pictures
# From two list of keypoints and a list of matches, extrat
# A list of good matches found by RANSAC and two transformation matrix (an homography and a rigid homography/affine)
# Instanciate outputs
good = []
# Transforming keypoints to list of points
src_pts = np.float32([keypoints_pic1[m.queryIdx].pt for m in matches]).reshape(-1, 1, 2)
dst_pts = np.float32([keypoints_pic2[m.trainIdx].pt for m in matches]).reshape(-1, 1, 2)
# Find the transformation between points
transformation_matrix, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0)
# Compute a rigid transformation (without depth, only scale + rotation + translation)
transformation_rigid_matrix, rigid_mask = cv2.estimateAffinePartial2D(src_pts, dst_pts)
# Get a mask list for matches = A list that says "This match is an in/out-lier"
matchesMask = mask.ravel().tolist()
# Filter the matches list thanks to the mask
for i, element in enumerate(matchesMask):
if element == 1:
good.append(matches[i])
return good, transformation_matrix, transformation_rigid_matrix