本文整理汇总了Python中cv2.drawMatches方法的典型用法代码示例。如果您正苦于以下问题:Python cv2.drawMatches方法的具体用法?Python cv2.drawMatches怎么用?Python cv2.drawMatches使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cv2
的用法示例。
在下文中一共展示了cv2.drawMatches方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: matchAB
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import drawMatches [as 别名]
def matchAB(fileA, fileB):
# 读取图像数据
imgA = cv2.imread(fileA)
imgB = cv2.imread(fileB)
# 转换成灰色
grayA = cv2.cvtColor(imgA, cv2.COLOR_BGR2GRAY)
grayB = cv2.cvtColor(imgB, cv2.COLOR_BGR2GRAY)
# akaze特征量抽出
akaze = cv2.AKAZE_create()
kpA, desA = akaze.detectAndCompute(grayA, None)
kpB, desB = akaze.detectAndCompute(grayB, None)
# BFMatcher定义和图形化
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
matches = bf.match(desB, desB)
matches = sorted(matches, key=lambda x: x.distance)
matched_image = cv2.drawMatches(imgA, kpA, imgB, kpB, matches, None, flags=2)
plt.imshow(cv2.cvtColor(matched_image, cv2.COLOR_BGR2RGB))
plt.show()
示例2: draw_match_2_side
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import drawMatches [as 别名]
def draw_match_2_side(img1, kp1, img2, kp2, N):
"""Draw matches on 2 sides
Args:
img1 (HxW(xC) array): image 1
kp1 (Nx2 array): keypoint for image 1
img2 (HxW(xC) array): image 2
kp2 (Nx2 array): keypoint for image 2
N (int): number of matches to draw
Returns:
out_img (Hx2W(xC) array): output image with drawn matches
"""
kp_list = np.linspace(0, min(kp1.shape[0], kp2.shape[0])-1, N,
dtype=np.int
)
# Convert keypoints to cv2.Keypoint object
cv_kp1 = [cv2.KeyPoint(x=pt[0], y=pt[1], _size=1) for pt in kp1[kp_list]]
cv_kp2 = [cv2.KeyPoint(x=pt[0], y=pt[1], _size=1) for pt in kp2[kp_list]]
out_img = np.array([])
good_matches = [cv2.DMatch(_imgIdx=0, _queryIdx=idx, _trainIdx=idx,_distance=0) for idx in range(N)]
out_img = cv2.drawMatches(img1, cv_kp1, img2, cv_kp2, matches1to2=good_matches, outImg=out_img)
return out_img
示例3: visualize_homo
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import drawMatches [as 别名]
def visualize_homo(img1, img2, kp1, kp2, matches, homo, mask):
h, w, d = img1.shape
pts = [[0, 0], [0, h - 1], [w - 1, h - 1], [w - 1, 0]]
pts = np.array(pts, dtype=np.float32).reshape((-1, 1, 2))
dst = cv.perspectiveTransform(pts, homo)
img2 = cv.polylines(img2, [np.int32(dst)], True, [255, 0, 0], 3, 8)
matches_mask = mask.ravel().tolist()
draw_params = dict(matchesMask=matches_mask,
singlePointColor=None,
matchColor=(0, 255, 0),
flags=2)
res = cv.drawMatches(img1, kp1, img2, kp2, matches, None, **draw_params)
return res
示例4: convertTupleListToRectangleList
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import drawMatches [as 别名]
def convertTupleListToRectangleList(l_xywh):
""" Receives a list of tuples (x,y,w,h) defining rectangles
Returns a list of Rectangle objects
"""
l = []
for (x,y,w,h) in l_xywh:
l.append(Rectangle(x,y,w,h))
return l
#Custom drawMatches (it is not included in prior versions to 3.0 of OpenCV)
#https://stackoverflow.com/questions/20259025/module-object-has-no-attribute-drawmatches-opencv-python
示例5: draw_matches
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import drawMatches [as 别名]
def draw_matches(self, img1, cv_kpts1, img2, cv_kpts2, good_matches, mask,
match_color=(0, 255, 0), pt_color=(0, 0, 255)):
"""Draw matches."""
if type(cv_kpts1) is np.ndarray and type(cv_kpts2) is np.ndarray:
cv_kpts1 = [cv2.KeyPoint(cv_kpts1[i][0], cv_kpts1[i][1], 1)
for i in range(cv_kpts1.shape[0])]
cv_kpts2 = [cv2.KeyPoint(cv_kpts2[i][0], cv_kpts2[i][1], 1)
for i in range(cv_kpts2.shape[0])]
display = cv2.drawMatches(img1, cv_kpts1, img2, cv_kpts2, good_matches,
None,
matchColor=match_color,
singlePointColor=pt_color,
matchesMask=mask.ravel().tolist(), flags=4)
return display
示例6: _rgb
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import drawMatches [as 别名]
def _rgb(self, f):
dat_orig = list(self.varr.data_vars.values())[0]
dat_mocc = list(self.varr.data_vars.values())[1]
w = dat_orig.coords['width']
h = dat_orig.coords['height']
try:
ma = self.match[f]
except KeyError:
ma = None
if ma:
up = ma['upsample']
im_src = dat_orig.sel(frame=ma['src_fid']).reindex(
method='nearest',
width=np.linspace(w[0], w[-1], len(w) * up),
height=np.linspace(h[0], h[-1], len(h) * up))
im_dst = dat_orig.sel(frame=f).reindex(
method='nearest',
width=np.linspace(w[0], w[-1], len(w) * up),
height=np.linspace(h[0], h[-1], len(h) * up))
img = cv2.drawMatches(im_src.values, ma['src'][0], im_dst.values,
ma['dst'][0], ma['match'], None)
else:
try:
im_src = dat_orig.sel(frame=f - 1)
except KeyError:
im_src = dat_orig.sel(frame=f)
im_dst = dat_orig.sel(frame=f)
img = xr.concat([im_src, im_dst], dim='width')
img = xr.concat([img] * 3, dim='color')
img = img.transpose('height', 'width', 'color').values
return hv.RGB(img, kdims=['width', 'height'])
示例7: drawMatches
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import drawMatches [as 别名]
def drawMatches(self, MarkImage, SceneImage):
outImg = cv2.drawMatches(MarkImage,self.KP1,
SceneImage,self.KP2,
self.GoodMatches,None,**self.DrawParams)
return outImg
示例8: draw_matches
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import drawMatches [as 别名]
def draw_matches(img1, img2, kp1, kp2, matches, thres=10):
"""
Utility function to draw lines connecting matches between two images.
"""
draw_params = dict(matchColor = (0,255,0),
singlePointColor = (255,0,0),
flags = 0)
# Draw first thres matches.
img3 = cv2.drawMatches(img1,kp1,img2,kp2,matches[:thres],None, **draw_params)
plot_img(img3)
示例9: debug_matching
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import drawMatches [as 别名]
def debug_matching(frame1, frame2, path_image1, path_image2, matches,
matches_mask, num_points, use_ratio_test):
img1 = cv2.imread(path_image1, 0)
img2 = cv2.imread(path_image2, 0)
kp1 = get_ocv_kpts_from_np(frame1['keypoints'][:num_points, :])
kp2 = get_ocv_kpts_from_np(frame2['keypoints'][:num_points, :])
if use_ratio_test:
img = cv2.drawMatchesKnn(img1, kp1, img2, kp2, matches, None,
matchColor=(0, 255, 0),
matchesMask=matches_mask,
singlePointColor=(255, 0, 0), flags=0)
else:
img = cv2.drawMatches(img1, kp1, img2, kp2, matches, None,
matchColor=(0, 255, 0),
singlePointColor=(255, 0, 0), flags=0)
img_sift = baseline_sift_matching(img1, img2)
fig = plt.figure(figsize=(2, 1))
fig.add_subplot(2, 1, 1)
plt.imshow(img)
plt.title('Custom features')
fig.add_subplot(2, 1, 2)
plt.imshow(img_sift)
plt.title('SIFT')
plt.show()
示例10: find
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import drawMatches [as 别名]
def find(self, img2_name):
print(u'进入身份证模版匹配流程...')
img1_name = 'idcard_mask.jpg'
MIN_MATCH_COUNT = 10
img1 = cv2.UMat(cv2.imread(img1_name, 0)) # queryImage in Gray
img1 = self.img_resize(img1, 640)
# self.showimg(img1)
#img1 = idocr.hist_equal(img1)
img2 = cv2.UMat(cv2.imread(img2_name, 0)) # trainImage in Gray
# print(img2.get().shape)
img2 = self.img_resize(img2, 1920)
#img2 = idocr.hist_equal(img2)
img_org = cv2.UMat(cv2.imread(img2_name))
img_org = self.img_resize(img_org, 1920)
# Initiate SIFT detector
t1 = round(time.time() * 1000)
sift = cv2.xfeatures2d.SIFT_create()
# find the keypoints and descriptors with SIFT
kp1, des1 = sift.detectAndCompute(img1,None)
kp2, des2 = sift.detectAndCompute(img2,None)
FLANN_INDEX_KDTREE = 0
index_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 5)
search_params = dict(checks = 10)
flann = cv2.FlannBasedMatcher(index_params, search_params)
matches = flann.knnMatch(des1,des2,k=2)
# store all the good matches as per Lowe's ratio test.
#两个最佳匹配之间距离需要大于ratio 0.7,距离过于相似可能是噪声点
good = []
for m,n in matches:
if m.distance < 0.7*n.distance:
good.append(m)
#reshape为(x,y)数组
if len(good)>MIN_MATCH_COUNT:
src_pts = np.float32([ kp1[m.queryIdx].pt for m in good ]).reshape(-1,1,2)
dst_pts = np.float32([ kp2[m.trainIdx].pt for m in good ]).reshape(-1,1,2)
#用HomoGraphy计算图像与图像之间映射关系, M为转换矩阵
M, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC,5.0)
matchesMask = mask.ravel().tolist()
#使用转换矩阵M计算出img1在img2的对应形状
h,w = cv2.UMat.get(img1).shape
M_r=np.linalg.inv(M)
im_r = cv2.warpPerspective(img_org, M_r, (w,h))
# self.showimg(im_r)
else:
print("Not enough matches are found - %d/%d" % (len(good),MIN_MATCH_COUNT))
matchesMask = None
#draw_params = dict(matchColor = (0,255,0), # draw matches in green color
# singlePointColor = None,
# matchesMask = matchesMask, # draw only inliers
# flags = 2)
#img3 = cv2.drawMatches(img1,kp1,img2,kp2,good,None,**draw_params)
#plt.imshow(img3, 'gray'),plt.show()
t2 = round(time.time() * 1000)
print(u'查找身份证耗时:%s' % (t2 - t1))
return im_r
示例11: showImWarpEx
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import drawMatches [as 别名]
def showImWarpEx(im_fl, save):
"""
Show an example of warped images and their corresponding four corner points.
"""
im = cv2.resize(cv2.cvtColor(cv2.imread(im_fl), cv2.COLOR_BGR2GRAY), (256,int(120./160*256)))
r = Random(0)
r.seed(time.time())
h, w = im.shape
im_warp, pts_warp = randPerspectiveWarp(im, w, h, r, ret_pts=True) # get the perspective warped picture
pts_orig = np.zeros((4, 2), dtype=np.float32) # four original points
ofst = 3
pts_orig[0, 0] = ofst
pts_orig[0, 1] = ofst
pts_orig[1, 0] = ofst
pts_orig[1, 1] = h-ofst
pts_orig[2, 0] = w-ofst
pts_orig[2, 1] = ofst
pts_orig[3, 0] = w-ofst
pts_orig[3, 1] = h-ofst
kpts_warp = []
kpts_orig = []
matches = []
pts_rect = np.zeros((4, 2), dtype=np.float32) # for creating rectangles
pts_rect[0, 0] = w/4
pts_rect[0, 1] = h/4
pts_rect[1, 0] = w/4
pts_rect[1, 1] = 3*h/4
pts_rect[2, 0] = 3*w/4
pts_rect[2, 1] = h/4
pts_rect[3, 0] = 3*w/4
pts_rect[3, 1] = 3*h/4
if save: # save orig before placing rectangles on it
cv2.imwrite("Original.jpg", im)
for i in range(4):
kpts_warp.append(cv2.KeyPoint(pts_warp[i,0], pts_warp[i,1], 0))
kpts_orig.append(cv2.KeyPoint(pts_orig[i,0], pts_orig[i,1], 0))
matches.append(cv2.DMatch(i,i,0))
im = cv2.rectangle(im, (pts_orig[i,0], pts_orig[i,1]), (pts_rect[i,0], pts_rect[i,1]), (255,255,255), thickness=2)
draw_params = dict(matchColor=(0,0,250),flags = 4)
out_im = cv2.drawMatches(im, kpts_warp, im_warp, kpts_orig, matches, None, **draw_params)
plots = os.path.join(os.getcwd(), "plots")
from matplotlib import rcParams
rcParams['savefig.directory'] = plots
if not os.path.isdir(plots):
os.makedirs(plots)
plt.imshow(out_im)
plt.axis('off')
plt.show()
if save:
cv2.imwrite("Warped.jpg", im_warp)
print "Images saved in current directory"
示例12: debugMatches
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import drawMatches [as 别名]
def debugMatches():
# Debug module.
# from matplotlib import pyplot as plt
markImage = cv2.imread('./clock.png')
sceneImage = cv2.imread('./clock_in_scene.png')
# Init PM.
pm = GetPMatrix(markImage)
# Get kp1, kp2, dst, goodMatches, [draw_params].
dst = pm.getMatches(sceneImage)
if dst is None:
exit()
# Draw circles and lines.
img3 = pm.drawMatches(markImage, sceneImage)
# # Get ret, mtx, dist, rvecs, tvecs
tmp = None
for i in range(30):
tmp = pm.getP(dst)
if tmp is None:
exit()
print i
mtx, dist, rvec, tvec = tmp
# Draw Box
h,w = markImage.shape[:2]
img3[:,w:] = pm.drawBox(img3[:,w:])
h2,w2 = sceneImage.shape[:2]
glP = pm.getGLP(w2, h2)
glM = pm.getGLM()
print 'mtx -------------'
print mtx
print 'dist ------------'
print dist
print 'rvec -----------'
print rvec
print 'tvec -----------'
print tvec
print 'glP ------------'
print glP
print 'glM ------------'
print glM
img3 = cv2.cvtColor(img3, cv2.COLOR_BGR2RGB)
plt.figure('Matches test.'), plt.imshow(img3)
示例13: match
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import drawMatches [as 别名]
def match(self, max_match_lenth=20, threshold=0.04, show_match=False):
"""对两幅图片计算得出的特征值进行匹配,对ORB来说使用OpenCV的BFMatcher算法,而对于其他特征检测方法则使用FlannBasedMatcher算法。
max_match_lenth (int, optional): Defaults to 20. 最大匹配点数量
threshold (float, optional): Defaults to 0.04. 默认最大匹配距离差
show_match (bool, optional): Defaults to False. 是否展示匹配结果
"""
self.compute_keypoint()
'''计算两张图片中的配对点,并至多取其中最优的`max_match_lenth`个'''
self.match_points = sorted(self.matcher.match(
self._descriptors1, self._descriptors2), key=lambda x: x.distance)
match_len = min(len(self.match_points), max_match_lenth)
# in case distance is 0
max_distance = max(2 * self.match_points[0].distance, 20)
for i in range(match_len):
if self.match_points[i].distance > max_distance:
match_len = i
break
print('max distance: ', self.match_points[match_len].distance)
print("Min distance: ", self.match_points[0].distance)
print('match_len: ', match_len)
assert(match_len >= 4)
self.match_points = self.match_points[:match_len]
if show_match:
img3 = cv2.drawMatches(self.image1, self._keypoints1, self.image2, self._keypoints2,
self.match_points, None, flags=0)
show_image(img3)
# cv2.imwrite('../resource/3-sift-match.jpg', img3)
'''由最佳匹配取得匹配点对,并进行形变拼接'''
image_points1, image_points2 = [], []
for i in self.match_points:
image_points1.append(self._keypoints1[i.queryIdx].pt)
image_points2.append(self._keypoints2[i.trainIdx].pt)
self.image_points1 = np.float32(image_points1)
self.image_points2 = np.float32(image_points2)
# print(image_points1)