Skip to content

Instantly share code, notes, and snippets.

@a4vg
Last active March 10, 2025 23:23
Show Gist options
  • Save a4vg/4c2c87f64b44822eb1e1842fc3594904 to your computer and use it in GitHub Desktop.
Save a4vg/4c2c87f64b44822eb1e1842fc3594904 to your computer and use it in GitHub Desktop.
BRISK keypoint detection
import cv2 as cv
type Image = cv.typing.MatLike
def show_image(img: Image):
cv.imshow("out", img)
cv.waitKey(0)
cv.destroyAllWindows()
def brisk_compare(train_img: Image, query_img: Image):
Brisk = cv.BRISK_create()
kp_train, desc_train = Brisk.detectAndCompute(train_img, None)
kp_query, desc_query = Brisk.detectAndCompute(query_img, None)
# brute force matcher
BFMatcher = cv.BFMatcher(normType = cv.NORM_HAMMING, crossCheck = True)
matches = BFMatcher.match(queryDescriptors = desc_train, trainDescriptors = desc_query)
# only draw the top 50 matches
top_n = 50
matches = sorted(matches, key = lambda x: x.distance)
output = cv.drawMatches(img1 = train_img, keypoints1 = kp_train,
img2 = query_img, keypoints2 = kp_query,
matches1to2 = matches[:top_n],
matchColor = (0,255,0),
outImg = None)
return output
def main():
train_img = cv.imread('fosforos_train.jpg')
query_img = cv.imread('fosforos_query.jpg')
out_img = brisk_compare(train_img, query_img)
cv.imwrite("brisk_out.jpg", out_img)
show_image(out_img)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment