如何在opencv中使用AKAZE进行Homographic密钥匹配

时间:2016-08-03 07:32:48

标签: python opencv homography

from __future__ import print_function
import cv2
import numpy as np
import os
import matplotlib as plt
path=os.getcwd()+"/Images/"



# load the image and convert it to grayscale
src= cv2.imread(path+'A.jpg')
target= cv2.imread(path+'B.jpg')

#cv2.imshow("Original", image)

def drawMatches(img1, kp1, img2, kp2, matches,color,matchFlag):
rows1 = img1.shape[0]
cols1 = img1.shape[1]
rows2 = img2.shape[0]
cols2 = img2.shape[1]

out = np.zeros((max([rows1,rows2]),cols1+cols2,3), dtype='uint8')

# Place the first image to the left
out[:rows1,:cols1] = np.dstack([img1, img1, img1])

# Place the next image to the right of it
out[:rows2,cols1:] = np.dstack([img2, img2, img2])

# For each pair of points we have between both images
# draw circles, then connect a line between them
for mat in matches:

    # Get the matching keypoints for each of the images
    img1_idx = mat.queryIdx
    img2_idx = mat.trainIdx

    # x - columns
    # y - rows
    (x1,y1) = kp1[img1_idx].pt
    (x2,y2) = kp2[img2_idx].pt

    # Draw a small circle at both co-ordinates
    # radius 4
    # colour blue
    # thickness = 1

    cv2.circle(out, (int(x1),int(y1)), 4, (255, 0, 0), 1)
    cv2.circle(out, (int(x2)+cols1,int(y2)), 4, (255, 0, 0), 1)

    # Draw a line in between the two points
    # thickness = 1
    # colour blue

    cv2.line(out, (int(x1),int(y1)), (int(x2)+cols1,int(y2)), color, 1)


# Show the image
cv2.imshow('Matched Features', out)
cv2.waitKey(0)
cv2.destroyWindow('Matched Features')

# Also return the image if you'd like a copy
return out

# initialize the AKAZE descriptor, then detect keypoints and extract
# local invariant descriptors from the image
detector = cv2.AKAZE_create()
(kps1, descs1) = detector.detectAndCompute(src, None)

print("keypoints: {}, descriptors: {}".format(len(kps1), descs1.shape)) 

(kps2, descs2) = detector.detectAndCompute(target, None)

print("keypoints: {}, descriptors: {}".format(len(kps2), descs2.shape))

bf = cv2.BFMatcher(cv2.NORM_HAMMING)  #Brute Force matching
matches = bf.knnMatch(descs1, descs2, k=2)

# Apply ratio test
good = []
for m, n in matches:
    if m.distance < 0.9 * n.distance:
        good.append([m])

# cv2.drawMatchesKnn expects list of lists as matches.
print(len(good))

if len(good)>5:

    src_pts = np.float32([ kps1[m[0].queryIdx].pt for m in good     ]).reshape(-1,1,2)
    dst_pts = np.float32([ kps2[m[0].trainIdx].pt for m in good ]).reshape(-1,1,2)

    M, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC,5.0)
    matchesMask = mask.ravel().tolist()

    h,w,_ = src.shape
    pts = np.float32([ [0,0],[0,h-1],[w-1,h-1],[w-1,0] ]).reshape(-1,1,2)
    dst = cv2.perspectiveTransform(pts,M)

    img2 = cv2.polylines(target,[np.int32(dst)],True,255,3, cv2.LINE_AA)

else:

        print("Not enough matches are found - %d/%d" % (len(good),10))

       matchesMask = None


      cv2.imshow("Output", img3)
      cv2.waitKey(0)


 img3 = cv2.drawMatchesKnn(src, kps1, target,kps2,good,None,matchesMask=matchesMask,flags=2) # get best matches

我正在尝试在python opencv3.1中进行单应匹配,但我无法将掩码应用于训练图像 SystemError:没有异常设置的错误返回 如何应用掩码匹配关键点并使用RANSAC忽略异常值?

0 个答案:

没有答案