Kategori: Computer Vision , 24 Şubat 2020 , JanFranco
Önceki yazılarımda feature extraction algoritmalarını anlattım. Bu yazımızda daha önce çıkardığımız feature descriptorleri eşleştirerek, obje tespiti yapacağız. Feature matching için kullanacağımız ilk yöntem Brute-Force Matcher algoritması olacak. cv2.BFMatcher() sınıfından obje oluşturup kullanabiliriz.
Eşleşmeleri oluşturduğumuzda çizebiliriz. cv2.drawKeypoints methoduna benzer bir şekilde cv2.drawMatches() methodunu çağırabiliriz. knnMatch methodunu kullandıysak cv2.drawMatchesKnn methodunu çizdirmek için kullanabiliriz. Python üzerinden OpenCV kütüphanesini kullanarak bir örnek yapalım. Kütüphaneyi ve resimleri alalım:
>>import cv2 cereal = cv2.imread('../data/reeses_puffs.png', 0) cereals = cv2.imread('../data/many_cereals.jpg', 0) cv2.imshow('cereal', cereal) cv2.waitKey(0) cv2.imshow('cereals', cereals) cv2.waitKey(0) sift = cv2.xfeatures2d.SIFT_create() kp1, des1 = sift.detectAndCompute(cereal, None) kp2, des2 = sift.detectAndCompute(cereals, None)
>>bf = cv2.BFMatcher() matches = bf.match(des1, des2) matches = sorted(matches, key=lambda x: x.distance) img = cv2.drawMatches(cereal, kp1, cereals, kp2, matches[:100], None, flags=2) cv2.imshow('SIFT', img) cv2.waitKey(0)
>>surf = cv2.xfeatures2d.SURF_create() kp1, des1 = surf.detectAndCompute(cereal, None) kp2, des2 = surf.detectAndCompute(cereals, None) matches = bf.match(des1, des2) matches = sorted(matches, key=lambda x: x.distance) img = cv2.drawMatches(cereal, kp1, cereals, kp2, matches[:100], None, flags=2) cv2.imshow('SURF', img) cv2.waitKey(0)
>>bf = cv2.BFMatcher(cv2.NORM_HAMMING) fast = cv2.FastFeatureDetector_create() brief = cv2.xfeatures2d.BriefDescriptorExtractor_create() kp = fast.detect(cereal, None) kp, descriptors = brief.compute(cereal, kp) kp2 = fast.detect(cereals, None) kp2, descriptors2 = brief.compute(cereals, kp2) matches = bf.match(descriptors, descriptors2) matches = sorted(matches, key=lambda x: x.distance) img = cv2.drawMatches(cereal, kp, cereals, kp2, matches[:100], None, flags=2) cv2.imshow('FAST AND BRIEF', img) cv2.waitKey(0) orb = cv2.ORB_create() kp1, des1 = orb.detectAndCompute(cereal, None) kp2, des2 = orb.detectAndCompute(cereals, None) matches = bf.match(des1, des2) matches = sorted(matches, key=lambda x: x.distance) cerealMatches = cv2.drawMatches(cereal, kp1, cereals, kp2, matches[:100], None, flags=2) cv2.imshow('ORB', cerealMatches) cv2.waitKey(0)