In [68]:
import cv2 
import matplotlib.pyplot as plt
import numpy as np
import os
from os import listdir
from os.path import isfile, join


pos_images = '/Users/ishanhanda/Documents/NYU_Fall16/Comp_Vision/Project/ProjectWorkspace/Sift/positive/'

image1_name = 'n02363005_329.JPEG'
image2_name = 'n02363005_1059.JPEG'

sift = cv2.xfeatures2d.SIFT_create()

print("image 1:", image1_name)
img1 = cv2.imread(pos_images + image1_name, 0)
kp1, des1 = sift.detectAndCompute(img1, None)

print("image 2:", image2_name)
img2 = cv2.imread(pos_images + image2_name, 0)
kp2, des2 = sift.detectAndCompute(img2, None)
        
bf = cv2.BFMatcher()
matches = bf.knnMatch(des1, des2, k = 2)
        
good = []
for m, n in matches:
    if m.distance < 0.9 * n.distance:
        good.append([m])


img3 = cv2.drawMatchesKnn(img1, kp1, img2, kp2, good, None, flags = 2)
img4 = cv2.drawMatchesKnn(img1, kp1, img2, kp2, matches, None, flags = 2)
img5 = cv2.drawMatchesKnn(img1, kp1, img2, kp2, [], None, flags = 2)

plt.title('Original Images')
plt.imshow(img5)
# pylab.savefig('Sift_Original.png')
plt.show()

plt.title('All detected Sift matches.')
print(len(matches))
plt.imshow(img4)
# pylab.savefig('Sift_All_Matches.png')
plt.show()

plt.title('Matches with < 0.9*distance.')
print(len(good))
plt.imshow(img3)
# pylab.savefig('Sift_Distance_ratio_matches.png')
plt.show()


image 1: n02363005_329.JPEG
image 2: n02363005_1059.JPEG
1042
157

In [56]:
edges1 = cv2.Canny(img1,100,200)

plt.subplot(121),plt.imshow(img1,cmap = 'gray')
plt.title('Original Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(edges,cmap = 'gray')
plt.title('Edge Image'), plt.xticks([]), plt.yticks([])
plt.show()

edges2 = cv2.Canny(img2,100,200)

plt.subplot(121),plt.imshow(img2,cmap = 'gray')
plt.title('Original Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(edges2,cmap = 'gray')
plt.title('Edge Image'), plt.xticks([]), plt.yticks([])
plt.show()



In [57]:
sift = cv2.xfeatures2d.SIFT_create()

kp1, des1 = sift.detectAndCompute(edges1, None)
kp2, des2 = sift.detectAndCompute(edges2, None)
        
bf = cv2.BFMatcher()
matches = bf.knnMatch(des1, des2, k = 2)
        
good = []
for m, n in matches:
    if m.distance < 0.9 * n.distance:
        good.append([m])


img3 = cv2.drawMatchesKnn(edges1, kp1, edges2, kp2, good, None, flags = 2)
img4 = cv2.drawMatchesKnn(edges1, kp1, edges2, kp2, matches, None, flags = 2)
img5 = cv2.drawMatchesKnn(edges1, kp1, edges2, kp2, [], None, flags = 2)

plt.title('Original Images')
plt.imshow(img5)
# pylab.savefig('Sift_Original.png')
plt.show()

plt.title('All detected Sift matches.')
print(len(matches))
plt.imshow(img4)
# pylab.savefig('Sift_All_Matches.png')
plt.show()

plt.title('Matches with < 0.9*distance.')
print(len(good))
plt.imshow(img3)
# pylab.savefig('Sift_Distance_ratio_matches.png')
plt.show()


1491
111

In [58]:
# remove noise
img_e1 = cv2.GaussianBlur(img1,(3,3),0)

plt.subplot(2,2,1),plt.imshow(img_e1,cmap = 'gray')
plt.title('Original'), plt.xticks([]), plt.yticks([])

plt.show()



In [59]:
# remove noise
img_e2 = cv2.GaussianBlur(img2,(3,3),0)

plt.subplot(2,2,1),plt.imshow(img_e2,cmap = 'gray')
plt.title('Original'), plt.xticks([]), plt.yticks([])

plt.show()



In [60]:
sift = cv2.xfeatures2d.SIFT_create()

kp1, des1 = sift.detectAndCompute(img_e1, None)
kp2, des2 = sift.detectAndCompute(img_e2, None)
        
bf = cv2.BFMatcher()
matches = bf.knnMatch(des1, des2, k = 2)
        
good = []
for m, n in matches:
    if m.distance < 0.9 * n.distance:
        good.append([m])


img3 = cv2.drawMatchesKnn(img_e1, kp1, img_e2, kp2, good, None, flags = 2)
img4 = cv2.drawMatchesKnn(img_e1, kp1, img_e2, kp2, matches, None, flags = 2)
img5 = cv2.drawMatchesKnn(img_e1, kp1, img_e2, kp2, [], None, flags = 2)

plt.title('Original Images')
plt.imshow(img5)
# pylab.savefig('Sift_Original.png')
plt.show()

plt.title('All detected Sift matches.')
print(len(matches))
plt.imshow(img4)
# pylab.savefig('Sift_All_Matches.png')
plt.show()

plt.title('Matches with < 0.9*distance.')
print(len(good))
plt.imshow(img3)
# pylab.savefig('Sift_Distance_ratio_matches.png')
plt.show()


1382
207

In [61]:
edges1 = cv2.Canny(img_e1,100,200)

plt.subplot(121),plt.imshow(img_e1,cmap = 'gray')
plt.title('Original Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(edges1,cmap = 'gray')
plt.title('Edge Image'), plt.xticks([]), plt.yticks([])
plt.show()

edges2 = cv2.Canny(img_e2,100,200)

plt.subplot(121),plt.imshow(img_e2,cmap = 'gray')
plt.title('Original Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(edges2,cmap = 'gray')
plt.title('Edge Image'), plt.xticks([]), plt.yticks([])
plt.show()



In [62]:
sift = cv2.xfeatures2d.SIFT_create()

kp1, des1 = sift.detectAndCompute(edges1, None)
kp2, des2 = sift.detectAndCompute(edges2, None)
        
bf = cv2.BFMatcher()
matches = bf.knnMatch(des1, des2, k = 2)
        
good = []
for m, n in matches:
    if m.distance < 0.9 * n.distance:
        good.append([m])


img3 = cv2.drawMatchesKnn(edges1, kp1, edges2, kp2, good, None, flags = 2)
img4 = cv2.drawMatchesKnn(edges1, kp1, edges2, kp2, matches, None, flags = 2)
img5 = cv2.drawMatchesKnn(edges1, kp1, edges2, kp2, [], None, flags = 2)

plt.title('Original Images')
plt.imshow(img5)
# pylab.savefig('Sift_Original.png')
plt.show()

plt.title('All detected Sift matches.')
print(len(matches))
plt.imshow(img4)
# pylab.savefig('Sift_All_Matches.png')
plt.show()

plt.title('Matches with < 0.9*distance.')
print(len(good))
plt.imshow(img3)
# pylab.savefig('Sift_Distance_ratio_matches.png')
plt.show()


1056
97

In [79]:
matches = [1042, 1491, 1382, 1056]
good = [157, 111, 207, 97]
percent = [157.0/1042.0 * 100, 111.0/1491.0 * 100, 207.0/1382.0 * 100, 97.0/1056.0* 100]
names = ["Original", "Canny Edge", "Gaussian Blur", "G Blur + Canny Edge"]

N = 4
ind = np.arange(N)  # the x locations for the groups
width = 0.1     # the width of the bars

fig = plt.figure(figsize=(8,6))
ax = fig.add_subplot(111)

rects1 = ax.bar(ind-width, matches, width, color='r')
rects2 = ax.bar(ind, good, width, color='g')
rects3 = ax.bar(ind+width, percent, width, color='b')

ax.set_ylabel('Hits')
ax.set_xticks(ind+width)
ax.set_xticklabels(names)
ax.legend( (rects1[0], rects2[0], rects3[0]), ('All matches', 'Good Matches', '%age') )

def autolabel(rects):
    for rect in rects:
        h = rect.get_height()
        ax.text(rect.get_x()+rect.get_width()/2., 1.05*h, '%d'%int(h),
                ha='center', va='bottom')

autolabel(rects1)
autolabel(rects2)
autolabel(rects3)

plt.grid()
plt.show()



In [ ]: