當前位置: 首頁>>代碼示例>>Python>>正文


Python cv2.drawMatches方法代碼示例

本文整理匯總了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() 
開發者ID:cangyan,項目名稱:image-detect,代碼行數:24,代碼來源:image_detect_01.py

示例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 
開發者ID:Huangying-Zhan,項目名稱:DF-VO,代碼行數:26,代碼來源:frame_drawer.py

示例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 
開發者ID:gmichaeljaison,項目名稱:specularity-removal,代碼行數:17,代碼來源:homography.py

示例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 
開發者ID:dalmia,項目名稱:WannaPark,代碼行數:13,代碼來源:utils.py

示例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 
開發者ID:lzx551402,項目名稱:geodesc,代碼行數:16,代碼來源:opencvhelper.py

示例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']) 
開發者ID:DeniseCaiLab,項目名稱:minian,代碼行數:33,代碼來源:visualization_ply.py

示例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 
開發者ID:GeekLiB,項目名稱:AR-BXT-AR4Python,代碼行數:7,代碼來源:getPMatrix.py

示例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) 
開發者ID:PacktPublishing,項目名稱:Practical-Computer-Vision,代碼行數:13,代碼來源:04_flann_feature_match.py

示例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() 
開發者ID:ethz-asl,項目名稱:hfnet,代碼行數:30,代碼來源:frame_matching.py

示例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 
開發者ID:Raymondhhh90,項目名稱:idcardocr,代碼行數:62,代碼來源:findidcard.py

示例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" 
開發者ID:rpng,項目名稱:calc,代碼行數:58,代碼來源:writeDatabase.py

示例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) 
開發者ID:GeekLiB,項目名稱:AR-BXT-AR4Python,代碼行數:52,代碼來源:getPMatrix.py

示例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) 
開發者ID:zhaobenx,項目名稱:Image-stitcher,代碼行數:47,代碼來源:stitch.py


注:本文中的cv2.drawMatches方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。