Opencv Python realizes two image matching

Time:2022-8-2

This example shares the specific code of OpenCV Python to realize the matching of two images for your reference. The specific contents are as follows

Original drawing

import cv2

img1 = cv2.imread('SURF_2.jpg', cv2.IMREAD_GRAYSCALE)
img1 = cv2.resize(img1,dsize=(600,400))
img2 = cv2.imread('SURF_1.jpg', cv2.IMREAD_GRAYSCALE)
img2 = cv2.resize(img2,dsize=(600,400))
image1 = img1.copy()
image2 = img2.copy()


#Create a surf object
surf = cv2.xfeatures2d.SURF_create(25000)
#Sift objects will use Hessian algorithm to detect key points, and calculate feature vectors for the region around each key point. This function returns information and descriptors of key points
keypoints1,descriptor1 = surf.detectAndCompute(image1,None)
keypoints2,descriptor2 = surf.detectAndCompute(image2,None)
# print('descriptor1:',descriptor1.shape(),'descriptor2',descriptor2.shape())
#Draw keys on the image
image1 = cv2.drawKeypoints(image=image1,keypoints = keypoints1,outImage=image1,color=(255,0,255),flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
image2 = cv2.drawKeypoints(image=image2,keypoints = keypoints2,outImage=image2,color=(255,0,255),flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
#Display image
cv2.imshow('surf_keypoints1',image1)
cv2.imshow('surf_keypoints2',image2)
cv2.waitKey(20)


matcher = cv2.FlannBasedMatcher()
matchePoints = matcher.match(descriptor1,descriptor2)
# print(type(matchePoints),len(matchePoints),matchePoints[0])

#Extracting strong matching feature points
minMatch = 1
maxMatch = 0
for i in range(len(matchePoints)):
    if minMatch > matchePoints[i].distance:
        minMatch = matchePoints[i].distance
    if maxMatch < matchePoints[i].distance:
        maxMatch = matchePoints[i].distance
    Print ('best match value is: ', minmatch)
    Print ('the worst matching value is: ', maxmatch)

#Obtain several optimal matching results of demining in front
goodMatchePoints = []
for i in range(len(matchePoints)):
    if matchePoints[i].distance < minMatch + (maxMatch-minMatch)/16:
        goodMatchePoints.append(matchePoints[i])

#Draw the best match point
outImg = None
outImg = cv2.drawMatches(img1,keypoints1,img2,keypoints2,goodMatchePoints,outImg,
                         matchColor=(0,255,0),flags=cv2.DRAW_MATCHES_FLAGS_DEFAULT)
cv2.imshow('matche',outImg)
cv2.waitKey(0)
cv2.destroyAllWindows()

Original drawing

#coding=utf-8
import cv2
from matplotlib import pyplot as plt

img=cv2.imread('xfeatures2d.SURF_create2.jpg',0)
#Surf=cv2.surf (400) \hessian threshold 400
# kp,des=surf.detectAndCompute(img,None)
# leng=len(kp)
# print(leng)
#There are too many key points, and the threshold is retrieved

surf=cv2.cv2.xfeatures2d. SURF_ Create (50000) \hessian threshold 50000
kp,des=surf.detectAndCompute(img,None)
leng=len(kp)
print(leng)

img2=cv2.drawKeypoints(img,kp,None,(255,0,0),4)
plt.imshow(img2)
plt.show()

#The following is the u-surf algorithm. The key points are in the same direction and the operation speed is faster.
surf.upright=True
kp=surf.detect(img,None)
img3=cv2.drawKeypoints(img,kp,None,(255,0,0),4)

plt.imshow(img3)
plt.show()

#Detect the size of key descriptor and change it from 64 dimension to 128 dimension
surf.extended=True
kp,des=surf.detectAndCompute(img,None)
dem1=surf.descriptorSize()
print(dem1)
shp1=des.shape()
print(shp1)

design sketch

import cv2
from matplotlib import pyplot as plt

leftImage = cv2.imread('xfeatures2d.SURF_create_1.jpg')
rightImage = cv2.imread('xfeatures2d.SURF_create_2.jpg')

#Create sift
sift = cv2.xfeatures2d.SIFT_create()
kp1, des1 = sift.detectAndCompute(leftImage, None)
kp2, des2 = sift. Detectandcompute (rightimage, none) # returns key information and descriptor

FLANN_INDEX_KDTREE = 0
indexParams = dict(algorithm=FLANN_INDEX_KDTREE, trees=5)
Searchparams = dict (checks=50) # specifies the number of times the index tree is traversed

flann = cv2.FlannBasedMatcher(indexParams, searchParams)
matches = flann.knnMatch(des1, des2, k=2)
matchesMask = [[0, 0] for i in range(len(matches))]
print("matches", matches[0])
for i, (m, n) in enumerate(matches):
    if m.distance < 0.07 * n.distance:
        matchesMask[i] = [1, 0]

drawParams = dict(matchColor=(0, 255, 0), singlePointColor=None,
                  Matchesmask=matchesmask, flags=2) \flag=2 draw only matching points, flag=0 draw all points
resultImage = cv2.drawMatchesKnn(leftImage, kp1, rightImage, kp2, matches, None, **drawParams)
plt.imshow(resultImage)
plt.show()

The above is the whole content of this article. I hope it will help you in your study, and I hope you can support developer.

Recommended Today

Responsibility chain mode of JavaScript Design Pattern

catalogue summary code implementation Parameter definition realization Implementation of responsibility chain mode improvement summary Responsibility chain model is a behavioral design model among design models; Definition: make multiple objects have the opportunity to process the request, so as to avoid the coupling relationship between the sender and receiver of the request. These objects that process […]