import numpy as np
import jedi
import cv2 as cv
import matplotlib.pyplot as plt

img1 = cv.imread('test_book.png',cv.IMREAD_GRAYSCALE) # queryImage
# img2 = cv.imread('test_small.png',cv.IMREAD_GRAYSCALE) # trainImage

# Initiate SIFT detector
sift = cv.xfeatures2d.SIFT_create(contrastThreshold=0.09)
# find the keypoints and descriptors with SIFT
kp1, des1 = sift.detectAndCompute(img1, None)
# print(kp1[0].octave & 0xFF, des1)
img3 = np.array(img1)
img3 = cv.drawKeypoints(img1, kp1, img3, flags=cv.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
img3[:40, :] = 0

# kp2, des2 = sift.detectAndCompute(img2,None)
# # BFMatcher with default params
# bf = cv.BFMatcher()
# matches = bf.knnMatch(des1,des2,k=2)
# print(len(matches))
# # # Apply ratio test
# good = []
# for m,n in matches:
#         if m.distance < 0.75*n.distance:
#                 good.append([m])

# print(len(good))
# # # cv.drawMatchesKnn expects list of lists as matches.
# img3 = cv.drawMatchesKnn(img1,kp1,img2,kp2,good,None, flags=cv.DrawMatchesFlags_NOT_DRAW_SINGLE_POINTS)
cv.imwrite('test_book_sift_or.png', img3)
# print(len(kp1))