I. Environmental preparedness
At present, there are 2.x and 3.x versions of the Opencv , the difference between the two versions is mainly that some functional functions are placed in different functional modules, so most of the two versions of the code is not universal. It is recommended that you install Anaconda and download the appropriate version yourself. Direct command to install OPENCV3, Lake:
Conda install-c Menpo opencv3
pip Install Lake
second, Sift/surf feature extraction and matching
# Coding:utf-8 from matplotlib import Pyplot as plt from lake.decorator import time_cost import cv2 print ' CV version: ', cv2.__version__ def Bgr_rgb (IMG): (R, g, b) = Cv2.split (img) return Cv2.merge ([B, G, R]) def orb_detect (image _a, Image_b): # feature Match orb = Cv2.
Orb_create () kp1, des1 = Orb.detectandcompute (image_a, None) kp2, des2 = Orb.detectandcompute (Image_b, none) # Create Bfmatcher Object BF = Cv2. Bfmatcher (Cv2.
Norm_hamming, Crosscheck=true) # Match descriptors.
matches = Bf.match (Des1, des2) # Sort them in ' order of their distance.
matches = sorted (matches, Key=lambda x:x.distance) # Draw a matches. IMG3 = Cv2.drawmatches (image_a, Kp1, Image_b, KP2, matches[:100], None, flags=2) return Bgr_rgb (IMG3) @time_cost def Sift_detect (IMG1, Img2, detector= ' surf '): If Detector.startswith (' Si '): print "Sift detector ..." s IFT = Cv2.xfeatures2d.SURF_create () else:priNT "Surf detector ..." sift = cv2.xfeatures2d.SURF_create () # Find the keypoints and descriptors with sift KP1, des1 = Sift.detectandcompute (IMG1, none) kp2, Des2 = Sift.detectandcompute (Img2, None) # Bfmatcher with Default params bf = Cv2. Bfmatcher () matches = Bf.knnmatch (Des1, Des2, k=2) # Apply ratio Test good = [[M] for M, N in matches if M.di
Stance < 0.5 * N.distance] # CV2.DRAWMATCHESKNN expects list of lists as matches. IMG3 = CV2.DRAWMATCHESKNN (IMG1, Kp1, Img2, KP2, Good, None, flags=2) return Bgr_rgb (IMG3) If __name__ = "__main__" : # Load Image image_a = Cv2.imread ('./img1.jpg ') Image_b = Cv2.imread ('./img2.png ') # ORB # img = or B_detect (Image_a, Image_b) # SIFT or SURF img = Sift_detect (image_a, Image_b) plt.imshow (IMG) plt.show ()
third, output display
CV version: 3.1.0
Surf Detector
... ==> time-cost:0.187422 Sift_detect
Output:
IMG1
Img2