python: 'cv2.' -> 'cv.' via 'import cv2 as cv'
This commit is contained in:
+36
-36
@@ -18,7 +18,7 @@ USAGE
|
||||
from __future__ import print_function
|
||||
|
||||
import numpy as np
|
||||
import cv2
|
||||
import cv2 as cv
|
||||
from common import anorm, getsize
|
||||
|
||||
FLANN_INDEX_KDTREE = 1 # bug: flann enums are missing
|
||||
@@ -28,33 +28,33 @@ FLANN_INDEX_LSH = 6
|
||||
def init_feature(name):
|
||||
chunks = name.split('-')
|
||||
if chunks[0] == 'sift':
|
||||
detector = cv2.xfeatures2d.SIFT_create()
|
||||
norm = cv2.NORM_L2
|
||||
detector = cv.xfeatures2d.SIFT_create()
|
||||
norm = cv.NORM_L2
|
||||
elif chunks[0] == 'surf':
|
||||
detector = cv2.xfeatures2d.SURF_create(800)
|
||||
norm = cv2.NORM_L2
|
||||
detector = cv.xfeatures2d.SURF_create(800)
|
||||
norm = cv.NORM_L2
|
||||
elif chunks[0] == 'orb':
|
||||
detector = cv2.ORB_create(400)
|
||||
norm = cv2.NORM_HAMMING
|
||||
detector = cv.ORB_create(400)
|
||||
norm = cv.NORM_HAMMING
|
||||
elif chunks[0] == 'akaze':
|
||||
detector = cv2.AKAZE_create()
|
||||
norm = cv2.NORM_HAMMING
|
||||
detector = cv.AKAZE_create()
|
||||
norm = cv.NORM_HAMMING
|
||||
elif chunks[0] == 'brisk':
|
||||
detector = cv2.BRISK_create()
|
||||
norm = cv2.NORM_HAMMING
|
||||
detector = cv.BRISK_create()
|
||||
norm = cv.NORM_HAMMING
|
||||
else:
|
||||
return None, None
|
||||
if 'flann' in chunks:
|
||||
if norm == cv2.NORM_L2:
|
||||
if norm == cv.NORM_L2:
|
||||
flann_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 5)
|
||||
else:
|
||||
flann_params= dict(algorithm = FLANN_INDEX_LSH,
|
||||
table_number = 6, # 12
|
||||
key_size = 12, # 20
|
||||
multi_probe_level = 1) #2
|
||||
matcher = cv2.FlannBasedMatcher(flann_params, {}) # bug : need to pass empty dict (#1329)
|
||||
matcher = cv.FlannBasedMatcher(flann_params, {}) # bug : need to pass empty dict (#1329)
|
||||
else:
|
||||
matcher = cv2.BFMatcher(norm)
|
||||
matcher = cv.BFMatcher(norm)
|
||||
return detector, matcher
|
||||
|
||||
|
||||
@@ -76,12 +76,12 @@ def explore_match(win, img1, img2, kp_pairs, status = None, H = None):
|
||||
vis = np.zeros((max(h1, h2), w1+w2), np.uint8)
|
||||
vis[:h1, :w1] = img1
|
||||
vis[:h2, w1:w1+w2] = img2
|
||||
vis = cv2.cvtColor(vis, cv2.COLOR_GRAY2BGR)
|
||||
vis = cv.cvtColor(vis, cv.COLOR_GRAY2BGR)
|
||||
|
||||
if H is not None:
|
||||
corners = np.float32([[0, 0], [w1, 0], [w1, h1], [0, h1]])
|
||||
corners = np.int32( cv2.perspectiveTransform(corners.reshape(1, -1, 2), H).reshape(-1, 2) + (w1, 0) )
|
||||
cv2.polylines(vis, [corners], True, (255, 255, 255))
|
||||
corners = np.int32( cv.perspectiveTransform(corners.reshape(1, -1, 2), H).reshape(-1, 2) + (w1, 0) )
|
||||
cv.polylines(vis, [corners], True, (255, 255, 255))
|
||||
|
||||
if status is None:
|
||||
status = np.ones(len(kp_pairs), np.bool_)
|
||||
@@ -96,26 +96,26 @@ def explore_match(win, img1, img2, kp_pairs, status = None, H = None):
|
||||
for (x1, y1), (x2, y2), inlier in zip(p1, p2, status):
|
||||
if inlier:
|
||||
col = green
|
||||
cv2.circle(vis, (x1, y1), 2, col, -1)
|
||||
cv2.circle(vis, (x2, y2), 2, col, -1)
|
||||
cv.circle(vis, (x1, y1), 2, col, -1)
|
||||
cv.circle(vis, (x2, y2), 2, col, -1)
|
||||
else:
|
||||
col = red
|
||||
r = 2
|
||||
thickness = 3
|
||||
cv2.line(vis, (x1-r, y1-r), (x1+r, y1+r), col, thickness)
|
||||
cv2.line(vis, (x1-r, y1+r), (x1+r, y1-r), col, thickness)
|
||||
cv2.line(vis, (x2-r, y2-r), (x2+r, y2+r), col, thickness)
|
||||
cv2.line(vis, (x2-r, y2+r), (x2+r, y2-r), col, thickness)
|
||||
cv.line(vis, (x1-r, y1-r), (x1+r, y1+r), col, thickness)
|
||||
cv.line(vis, (x1-r, y1+r), (x1+r, y1-r), col, thickness)
|
||||
cv.line(vis, (x2-r, y2-r), (x2+r, y2+r), col, thickness)
|
||||
cv.line(vis, (x2-r, y2+r), (x2+r, y2-r), col, thickness)
|
||||
vis0 = vis.copy()
|
||||
for (x1, y1), (x2, y2), inlier in zip(p1, p2, status):
|
||||
if inlier:
|
||||
cv2.line(vis, (x1, y1), (x2, y2), green)
|
||||
cv.line(vis, (x1, y1), (x2, y2), green)
|
||||
|
||||
cv2.imshow(win, vis)
|
||||
cv.imshow(win, vis)
|
||||
|
||||
def onmouse(event, x, y, flags, param):
|
||||
cur_vis = vis
|
||||
if flags & cv2.EVENT_FLAG_LBUTTON:
|
||||
if flags & cv.EVENT_FLAG_LBUTTON:
|
||||
cur_vis = vis0.copy()
|
||||
r = 8
|
||||
m = (anorm(np.array(p1) - (x, y)) < r) | (anorm(np.array(p2) - (x, y)) < r)
|
||||
@@ -124,15 +124,15 @@ def explore_match(win, img1, img2, kp_pairs, status = None, H = None):
|
||||
for i in idxs:
|
||||
(x1, y1), (x2, y2) = p1[i], p2[i]
|
||||
col = (red, green)[status[i]]
|
||||
cv2.line(cur_vis, (x1, y1), (x2, y2), col)
|
||||
cv.line(cur_vis, (x1, y1), (x2, y2), col)
|
||||
kp1, kp2 = kp_pairs[i]
|
||||
kp1s.append(kp1)
|
||||
kp2s.append(kp2)
|
||||
cur_vis = cv2.drawKeypoints(cur_vis, kp1s, None, flags=4, color=kp_color)
|
||||
cur_vis[:,w1:] = cv2.drawKeypoints(cur_vis[:,w1:], kp2s, None, flags=4, color=kp_color)
|
||||
cur_vis = cv.drawKeypoints(cur_vis, kp1s, None, flags=4, color=kp_color)
|
||||
cur_vis[:,w1:] = cv.drawKeypoints(cur_vis[:,w1:], kp2s, None, flags=4, color=kp_color)
|
||||
|
||||
cv2.imshow(win, cur_vis)
|
||||
cv2.setMouseCallback(win, onmouse)
|
||||
cv.imshow(win, cur_vis)
|
||||
cv.setMouseCallback(win, onmouse)
|
||||
return vis
|
||||
|
||||
|
||||
@@ -149,8 +149,8 @@ if __name__ == '__main__':
|
||||
fn1 = '../data/box.png'
|
||||
fn2 = '../data/box_in_scene.png'
|
||||
|
||||
img1 = cv2.imread(fn1, 0)
|
||||
img2 = cv2.imread(fn2, 0)
|
||||
img1 = cv.imread(fn1, 0)
|
||||
img2 = cv.imread(fn2, 0)
|
||||
detector, matcher = init_feature(feature_name)
|
||||
|
||||
if img1 is None:
|
||||
@@ -176,7 +176,7 @@ if __name__ == '__main__':
|
||||
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)
|
||||
H, status = cv.findHomography(p1, p2, cv.RANSAC, 5.0)
|
||||
print('%d / %d inliers/matched' % (np.sum(status), len(status)))
|
||||
else:
|
||||
H, status = None, None
|
||||
@@ -185,5 +185,5 @@ if __name__ == '__main__':
|
||||
_vis = explore_match(win, img1, img2, kp_pairs, status, H)
|
||||
|
||||
match_and_draw('find_obj')
|
||||
cv2.waitKey()
|
||||
cv2.destroyAllWindows()
|
||||
cv.waitKey()
|
||||
cv.destroyAllWindows()
|
||||
|
||||
Reference in New Issue
Block a user