Exemplo n.º 1
0
def display(s_idx, pw_idx):
    plt.figure()
    (im1, kp1, des1) = (s_im[s_idx], s_kp[s_idx], s_des[s_idx])
    (im2, kp2, des2) = (pw_im[pw_idx], pw_kp[pw_idx], pw_des[pw_idx])
    matches = match(kp1, des1, kp2, des2)
    # Convert to OpenCV objects for viewing
    matches = match_to_cv(matches)
    kp1 = array_to_kp(kp1)
    kp2 = array_to_kp(kp2)
    s_plate = s_label[s_idx]
    pw_plate = pw_label[pw_idx]
    im_matches = cv2.drawMatches(
        im1,
        kp1,
        im2,
        kp2,
        matches,
        None,
        flags=cv2.DRAW_MATCHES_FLAGS_NOT_DRAW_SINGLE_POINTS)
    s = '[CV] Matches between S%s & PW%s = %s' % (s_plate, pw_plate,
                                                  str(len(matches)))
    print(s)
    plt.title(s)
    plt.imshow(im_matches)
#     plt.figure(1)
#     plt.imshow(im_match)
#     # dst = cv2.warpPerspective(P1,H,(P1.shape[1] + P2.shape[1], P2.shape[0]))

#%% SIFT for warping
# for m in matches[:,0]:
#     src_pts.append(kp1[m.queryIdx].pt)
#     dst_pts.append(kp2[m.trainIdx].pt)
#
# src_pts = np.array(src_pts, dtype=np.float32)
# dst_pts = np.array(dst_pts, dtype=np.float32)

#%% My Method
print("Performing my matching algorithm")
import util_matching, util_cv, util_sift
matches2 = util_matching.match(util_sift.kp_to_array(kp1), des1,
                               util_sift.kp_to_array(kp2), des2)
matches2 = util_cv.match_to_cv(matches2)
im_match2 = cv2.drawMatches(
    P1,
    kp1,
    P1,
    kp2,
    matches2,
    None,
    flags=cv2.DRAW_MATCHES_FLAGS_NOT_DRAW_SINGLE_POINTS)
# plt.imshow(im_match2)

#%% Mine for warping
print("Adding matching points for warping")
for m in matches2:
    pt1 = kp1[m.queryIdx].pt
from util_sift import array_to_kp, precompute_sift, load_sift
from util_matching import match
from util_cv import match_to_cv
from util_im import imshow

precompute_sift('S_BB_V4', 'PW_BB_V4')
s_im, s_label, s_kp, s_des = load_sift('S_BB_V4_SIFT.npz')
pw_im, pw_label, pw_kp, pw_des = load_sift('PW_BB_V4_SIFT.npz')

#%%
s_idx = 32  # np.where(s_label == 33)[0][0]
pw_idx = 36  # np.where(pw_label == 50)[0][0]
(im1, kp1, des1) = (s_im[s_idx], s_kp[s_idx], s_des[s_idx])
(im2, kp2, des2) = (pw_im[pw_idx], pw_kp[pw_idx], pw_des[pw_idx])
matches = match(kp1, des1, kp2, des2)
# Convert to OpenCV objects for viewing
matches = match_to_cv(matches)
kp1 = array_to_kp(kp1)
kp2 = array_to_kp(kp2)
im_matches = cv2.drawMatches(
    im1,
    kp1,
    im2,
    kp2,
    matches,
    None,
    flags=cv2.DRAW_MATCHES_FLAGS_NOT_DRAW_SINGLE_POINTS)
# plt.gray()
str = '%s Matches Between S%s and PW%s' % (len(matches), s_label[s_idx],
                                           pw_label[pw_idx])