import cv2 import numpy as np import homography if __name__ == '__main__': img = cv2.imread('test/image_01.jpeg') #img = cv2.imread('/Users/tal/Dropbox/School/Y4S1/CSC420/Assignment3/data/11.jpg') gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) img2 = cv2.imread('DVDcovers/shrek2.jpg') #img2 = cv2.imread('/Users/tal/Dropbox/School/Y4S1/CSC420/Assignment3/data/toy.jpg') gray2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY) sift = cv2.xfeatures2d.SIFT_create() kp2, tdesc = sift.detectAndCompute(gray, None) kp1, rdesc = sift.detectAndCompute(gray2, None) print rdesc.shape print len(kp1) print len(kp2) rfeat = np.array([k.pt for k in kp1]) tfeat = np.array([k.pt for k in kp2]) inliers, hom = homography.estimate_homography(rdesc, rfeat, tdesc, tfeat, num_iters=1000) rmat, tmat = homography.do_matching(rdesc, tdesc) print rmat.shape print inliers print hom homography.visualize_transformation(img, hom, img2.shape[0], img2.shape[1], 'tes_visualization.jpeg')
cProfile.run('vt.get_most_similar(tdesc)') most_similar = vt.get_most_similar(tdesc) best_im_name = "" best_im = None most_in = 0 best_rdesc = None best_rfeat = None best_hom = None print most_similar for train in most_similar: train_image = cv2.imread(train_dir + os.sep + train) gray = cv2.cvtColor(train_image, cv2.COLOR_BGR2GRAY) kp, rdesc = sift.detectAndCompute(gray, None) rfeat = np.array([k.pt for k in kp]) # cProfile.run('estimate_homography(rdesc, rfeat, tdesc, tfeat)') inl, hom = estimate_homography(rdesc, rfeat, tdesc, tfeat) print inl if inl > most_in: most_in = inl best_hom = hom best_im_name = train best_rdesc = rdesc best_rfeat = rfeat best_im = train_image print best_im_name im = visualize_transformation(test_image, best_hom, best_im.shape[0], best_im.shape[1]) im = cv2.putText(im, best_im_name, (5,100), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 0, 0)) cv2.imwrite('full_vis.jpg', im) # first_image = cv2.imread('DVDcovers/' + images[0])