Skip to content

Instantly share code, notes, and snippets.

@Winand
Created May 27, 2018 19:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Winand/97ec77f05cc57a07a2bc5dcd8c43ff84 to your computer and use it in GitHub Desktop.
Save Winand/97ec77f05cc57a07a2bc5dcd8c43ff84 to your computer and use it in GitHub Desktop.
cv2 image matching test
# -*- coding: utf-8 -*-
"""
Редактор Spyder
Это временный скриптовый файл.
"""
#import numpy as np
#import cv2 as cv
#filename = r"F:\Captura3.PNG"
#img = cv.imread(filename)
#gray = cv.cvtColor(img,cv.COLOR_BGR2GRAY)
#gray = np.float32(gray)
#dst = cv.cornerHarris(gray,2,3,0.04)
##result is dilated for marking the corners, not important
#dst = cv.dilate(dst,None)
## Threshold for an optimal value, it may vary depending on the image.
#img[dst>0.01*dst.max()]=[0,0,255]
#cv.imshow('dst',img)
#if cv.waitKey(0) & 0xff == 27:
# cv.destroyAllWindows()
#import numpy as np
#import cv2
#from matplotlib import pyplot as plt
#
#img1 = cv2.imread(r"F:\Captura3.PNG", 0) # queryImage
#img2 = cv2.imread(r"F:\Captura2.PNG", 0) # trainImage
#
## Initiate SIFT detector
#orb = cv2.xfeatures2d.SIFT_create()
#
## find the keypoints and descriptors with SIFT
#kp1, des1 = orb.detectAndCompute(img1, None)
#kp2, des2 = orb.detectAndCompute(img2, None)
## create BFMatcher object
#bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
#
## Match descriptors.
#matches = bf.match(des1, des2)
#
## Sort them in the order of their distance.
#matches = sorted(matches, key=lambda x: x.distance)
#
## Draw first 10 matches.
#img3 = cv2.drawMatches(img1, kp1, img2, kp2, matches[:10], None, flags=2)
#print(len(img3), len(img3[0]))
#plt.imshow(img3),plt.show()
## FLANN parameters
#FLANN_INDEX_KDTREE = 1
#index_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 5)
#search_params = dict(checks=50) # or pass empty dictionary
#flann = cv2.FlannBasedMatcher(index_params,search_params)
#matches = flann.knnMatch(des1,des2,k=2)
## Need to draw only good matches, so create a mask
#matchesMask = [[0,0] for i in range(len(matches))]
## ratio test as per Lowe's paper
#for i,(m,n) in enumerate(matches):
# if m.distance < 0.7*n.distance:
# matchesMask[i]=[1,0]
#draw_params = dict(matchColor = (0,255,0),
# singlePointColor = (255,0,0),
# matchesMask = matchesMask,
# flags = 0)
#img3 = cv2.drawMatchesKnn(img1,kp1,img2,kp2,matches,None,**draw_params)
#plt.imshow(img3,),plt.show()
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
#img_rgb = cv.imread('mario.png')
img_bgr = cv.imread(r"F:\Captura3.PNG")
img_rgb = cv.cvtColor(img_bgr, cv.COLOR_BGR2RGB)
img_gray = cv.cvtColor(img_bgr, cv.COLOR_BGR2GRAY)
#template = cv.imread('mario_coin.png',0)
template = cv.imread(r"F:\head.PNG", 0)
w, h = template.shape[::-1]
res = cv.matchTemplate(img_gray,template,cv.TM_CCOEFF_NORMED)
threshold = 0.9
loc = np.where( res >= threshold)
for pt in zip(*loc[::-1]):
cv.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0,0,255), 2)
plt.imshow(img_rgb,),plt.show()
#cv.imshow('window',img_rgb)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment