本文整理匯總了Python中cv2.DMatch方法的典型用法代碼示例。如果您正苦於以下問題:Python cv2.DMatch方法的具體用法?Python cv2.DMatch怎麽用?Python cv2.DMatch使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類cv2
的用法示例。
在下文中一共展示了cv2.DMatch方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: thin_plate_transform
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import DMatch [as 別名]
def thin_plate_transform(x,y,offw,offh,imshape,shift_l=-0.05,shift_r=0.05,num_points=5,offsetMatrix=False):
rand_p=np.random.choice(x.size,num_points,replace=False)
movingPoints=np.zeros((1,num_points,2),dtype='float32')
fixedPoints=np.zeros((1,num_points,2),dtype='float32')
movingPoints[:,:,0]=x[rand_p]
movingPoints[:,:,1]=y[rand_p]
fixedPoints[:,:,0]=movingPoints[:,:,0]+offw*(np.random.rand(num_points)*(shift_r-shift_l)+shift_l)
fixedPoints[:,:,1]=movingPoints[:,:,1]+offh*(np.random.rand(num_points)*(shift_r-shift_l)+shift_l)
tps=cv2.createThinPlateSplineShapeTransformer()
good_matches=[cv2.DMatch(i,i,0) for i in range(num_points)]
tps.estimateTransformation(movingPoints,fixedPoints,good_matches)
imh,imw=imshape
x,y=np.meshgrid(np.arange(imw),np.arange(imh))
x,y=x.astype('float32'),y.astype('float32')
newxy=tps.applyTransformation(np.dstack((x.ravel(),y.ravel())))[1]
newxy=newxy.reshape([imh,imw,2])
if offsetMatrix:
return newxy,newxy-np.dstack((x,y))
else:
return newxy
示例2: draw_match_2_side
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import DMatch [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: feature_matcher
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import DMatch [as 別名]
def feature_matcher(self, sess, ref_feat, test_feat):
matches = self.bf_matcher(sess, ref_feat, test_feat)
matches = [cv2.DMatch(matches[i][0], matches[i][1], 0) for i in range(matches.shape[0])]
return matches
示例4: to_cv2_dmatch
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import DMatch [as 別名]
def to_cv2_dmatch(m):
return cv2.DMatch(m, m, m, m)
示例5: showImWarpEx
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import DMatch [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"