Пример #1
0
def FLANN_matching(img1, img2, threshold=0.75):

    # Initiate SIFT detector
    sift = cv.SIFT_create()

    # find the keypoints and descriptors with SIFT
    kp1, des1 = sift.detectAndCompute(img1, None)
    kp2, des2 = sift.detectAndCompute(img2, None)

    # FLANN parameters
    FLANN_INDEX_KDTREE = 1
    index_params = dict(algorithm=FLANN_INDEX_KDTREE, trees=5)
    search_params = dict(checks=50)  # or pass empty dictionary
    flann = cv.FlannBasedMatcher(index_params, search_params)
    matches = flann.knnMatch(des1, des2, k=2)

    good = []
    # ratio test as per Lowe's paper
    for i, (m, n) in enumerate(matches):
        if m.distance < threshold * n.distance:
            good.append(m)

    print(f"Found {len(good)} matches with distance threshold = {threshold}")

    p1 = np.array([kp1[m.queryIdx].pt for m in good])
    p2 = np.array([kp2[m.trainIdx].pt for m in good])

    des = des1[[m.queryIdx for m in good], :]

    uv1 = np.vstack((p1.T, np.ones(p1.shape[0])))
    uv2 = np.vstack((p2.T, np.ones(p2.shape[0])))

    return p1, p2, des
Пример #2
0
def computeHomography(im1, im2):
    ''' '''
    sift = cv2.SIFT_create()
    # find the keypoints with SIFT
    kp1, des1 = sift.detectAndCompute(im1, None)
    kp2, des2 = sift.detectAndCompute(im2, None)

    des1 = np.float32(des1)
    des2 = np.float32(des2)
    # create flann basedMatcher
    FLANN_INDEX_KDTREE = 1
    index_params = dict(algorithm=FLANN_INDEX_KDTREE, trees=5)
    search_params = dict(checks=50)
    flann = cv2.FlannBasedMatcher(index_params, search_params)

    # Match descriptors.
    matches = flann.knnMatch(des1, des2, k=2)

    #good matches as per Lowe's ratio test.
    good = []
    for m, n in matches:
        if m.distance < 0.7 * n.distance:
            good.append(m)

    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)
    H, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC)

    return H
Пример #3
0
def match_image_to_model(X, model_des, img, using_rootsift, threshold=0.75):

    sift = cv.SIFT_create()
    kp_query, query_des = sift.detectAndCompute(img, None)
    if using_rootsift:
        query_des /= query_des.sum(axis=1, keepdims=True)
        query_des = np.sqrt(query_des)

    # FLANN parameters
    FLANN_INDEX_KDTREE = 1
    index_params = dict(algorithm=FLANN_INDEX_KDTREE, trees=5)
    search_params = dict(checks=50)  # or pass empty dictionary
    flann = cv.FlannBasedMatcher(index_params, search_params)
    matches = flann.knnMatch(model_des, query_des, k=2)

    # Need to draw only good matches
    good = []

    # ratio test as per Lowe's paper
    matched_idx = [False] * X.shape[1]
    for i, (m, n) in enumerate(matches):
        if m.distance < threshold * n.distance:
            good.append(m)
            matched_idx[i] = True

    print(f"Found {len(good)} matches with distance threshold = {threshold}")

    matched_2D_points = np.array([kp_query[m.trainIdx].pt for m in good])
    matched_3D_points = X[:, matched_idx]

    return matched_2D_points, matched_3D_points
Пример #4
0
def detect_image_repost(urls: List[str], url: str) -> int:
    ''' 
    Returns image that most closely resembles a repost
    returns image in the form of a list that gives the url to the image,
    the confidence that the image is a repost
    '''
    image1 = io.imread(url)
    sift = cv2.SIFT_create()
    k1, d1 = sift.detectAndCompute(image1, None)

    index = {'algorithm': 0, 'trees': 5}
    search = {}
    flann = cv2.FlannBasedMatcher(index, search)

    max_confidence = -1
    final_url = urls[0]

    for u in urls:
        if u == url:
            continue
        image2 = io.imread(u)
        k2, d2 = sift.detectAndCompute(image2, None)
        matches = flann.knnMatch(d1, d2, k=2)
        points = []
        for m, n in matches:
            if m.distance < .6 * n.distance:
                points.append(m)
        number_keypoints = 0
        if len(k1) < len(k2):
            number_keypoints = len(k1)
        else:
            number_keypoints = len(k2)
        confidence = (len(points) / number_keypoints) * 100
        if confidence > max_confidence:
            max_confidence = confidence
            final_url = u

    return [url, final_url, max_confidence]
Пример #5
0
def FLANN_matching(img1, img2, using_rootsift, threshold=0.75):
    # Initiate SIFT detector
    sift = cv.SIFT_create()

    # find the keypoints and descriptors with SIFT
    kp1, des1 = sift.detectAndCompute(img1, None)
    kp2, des2 = sift.detectAndCompute(img2, None)

    if using_rootsift:
        des1 /= des1.sum(axis=1, keepdims=True)
        des1 = np.sqrt(des1)
        des2 /= des2.sum(axis=1, keepdims=True)
        des2 = np.sqrt(des2)

    # FLANN parameters
    FLANN_INDEX_KDTREE = 1
    index_params = dict(algorithm=FLANN_INDEX_KDTREE, trees=5)
    search_params = dict(checks=50)  # or pass empty dictionary
    flann = cv.FlannBasedMatcher(index_params, search_params)
    matches = flann.knnMatch(des1, des2, k=2)

    # Need to draw only good matches
    good = []

    # ratio test as per Lowe's paper
    for i, (m, n) in enumerate(matches):
        if m.distance < threshold * n.distance:
            good.append(m)

    print(f"Found {len(good)} matches with distance threshold = {threshold}")

    p1 = np.array([kp1[m.queryIdx].pt for m in good])
    p2 = np.array([kp2[m.trainIdx].pt for m in good])

    des = des1[[m.queryIdx for m in good], :]

    return p1, p2, des
Пример #6
0
def init_feature(name):
    chunks = name.split('-')

    if chunks[0] == 'sift':
        detector = cv2.SIFT_create()
        norm = cv2.NORM_L2
    elif chunks[0] == 'surf':
        detector = cv2.xfeatures2d.SURF_create(800)
        norm = cv2.NORM_L2
    elif chunks[0] == 'orb':
        detector = cv2.ORB_create(400)
        norm = cv2.NORM_HAMMING
    elif chunks[0] == 'akaze':
        detector = cv2.AKAZE_create()
        norm = cv2.NORM_HAMMING
    elif chunks[0] == 'brisk':
        detector = cv2.BRISK_create()
        norm = cv2.NORM_HAMMING
    else:
        return None, None  # Return None if unknown detector name

    if 'flann' in chunks:
        if norm == cv2.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)
    else:
        matcher = cv2.BFMatcher(norm)

    return detector, matcher
Пример #7
0
 def detectSIFT(self, ptsNum):
     sift = cv2.SIFT_create(ptsNum)
     self.ref_kps, self.ref_des = sift.detectAndCompute(self.__ref_img, None)
     self.tar_kps, self.tar_des = sift.detectAndCompute(self.__tar_img, None)
Пример #8
0
# rotMat = cv2.getRotationMatrix2D( (img2_ref.shape[1] // 2, img2_ref.shape[0] // 2), deg, 0.8 )
# img2 = cv2.warpAffine(img2_ref, rotMat, (img2_ref.shape[1], img2_ref.shape[0]))
# img2 = img2_ref
# plt.title("Transformed")
# plt.imshow(img2)
# plt.show()

# img2 = img1[20:80, 55:250]
# plt.title("Transformed")
# plt.imshow(img2), plt.show()

# SIFT  ################################################################
print("[INFO] Starting SIFT!")

# Making a instance of class SIFT
sift = cv2.SIFT_create()

print("[INFO] stage 1: extracting features!")
kp1 = sift.detect(img1, None)
kp2 = sift.detect(img2, None)

# showing keypoints in images
img11 = cv2.drawKeypoints(img1,
                          kp1,
                          None,
                          flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
img22 = cv2.drawKeypoints(img2,
                          kp2,
                          None,
                          flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)