1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| from skimage import feature as ft import cv2 import matplotlib.pyplot as plt import numpy as np
img = cv2.imread(r'C:\Users\86181\Desktop\CVLab\lab4\666.jpg', 0)
hog = ft.hog(img, orientations=6, pixels_per_cell=[20,20], cells_per_block=[2,2], visualize=True) img1 = hog[1]
sift = cv2.SIFT_create()
kp,des = sift.detectAndCompute(img,None)
img2=cv2.drawKeypoints(img,kp,img,color=(255,0,0))
img3 = cv2.imread(r'C:\Users\86181\Desktop\CVLab\lab4\666.jpg', 0) img4 = cv2.imread(r'C:\Users\86181\Desktop\CVLab\lab4\revolve.jpg', 0)
psd_kp1, psd_des1 = sift.detectAndCompute(img3, None) psd_kp2, psd_des2 = sift.detectAndCompute(img4, None)
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) matches = flann.knnMatch(psd_des1, psd_des2, k=2) goodMatch = [] for m, n in matches: if m.distance < 0.50*n.distance: goodMatch.append(m)
goodMatch = np.expand_dims(goodMatch, 1)
img5 = cv2.drawMatchesKnn(img3, psd_kp1, img4, psd_kp2, goodMatch, None, flags=2)
titles = ['src','hog', 'sift', 'match'] imgs = [img, img1, img2, img5]
for i in range(4): plt.subplot(1,4,i+1) plt.imshow(imgs[i].astype('uint8')) plt.title(titles[i]) plt.show()
|