Feature matching code

From FDHwiki
Jump to navigation Jump to search

Video to frame

import cv2

name='DroneSurvolParis'
vidcap=cv2.VideoCapture(name+'.mp4')
success,image=vidcap.read()
count=0
success=True
while success:
    cv2.imwrite('frames/'+name+'_'+str(count)+'.jpg',image)
    success,image=vidcap.read()
    print('Read a new frame: ',count,success)
    count+=1

Matches computing

import os
import cv2
import numpy as np
from datetime import datetime

def drawmatches(name_1,name_2,matches,name):
    img1=cv2.imread('frames/'+name_1)
    img2=cv2.imread('frames/'+name_2)
    dim=img1.shape[:2]
    b=cv2.cvtColor(img1,cv2.COLOR_BGR2GRAY)
    g=cv2.cvtColor(img2,cv2.COLOR_BGR2GRAY)
    r=np.zeros((dim[0],dim[1],1),dtype="uint8")
    for matche in matches:
        coord1=(int(matche[0][0]),int(matche[0][1]))
        coord2=(int(matche[1][0]),int(matche[1][1]))
        cv2.line(r,coord1,coord2,255,4)
    img=cv2.merge((b,g,r))
    cv2.imwrite(name+'.jpg',img)

def find_matches_couple(name_1,name_2,method,num):
    if method=='sift':
        algo=cv2.xfeatures2d.SIFT_create()
    if method=='surf':
        algo=cv2.xfeatures2d.SURF_create()
    if method=='orb':
        algo=cv2.ORB_create()
    if method=='gms':
        algo=cv2.ORB_create(10000,fastThreshold=0)
    img1=cv2.imread('frames/'+name_1)
    img2=cv2.imread('frames/'+name_2)
    des1,kp1=algo.detectAndCompute(img1,None)
    des2,kp2=algo.detectAndCompute(img2,None)
    dim=img1.shape[:2]
    dim=(dim[1],dim[0])
    if method=='gms':
        bf=cv2.BFMatcher(cv2.NORM_HAMMING,crossCheck=True)
    else:
        bf=cv2.BFMatcher()
    matches=bf.match(kp1,kp2)
    if method=='gms':
        matches=cv2.xfeatures2d.matchGMS(dim,dim,des1,des2,matches,withRotation=True,withScale=True)
    matches=sorted(matches,key=lambda x:x.distance)
    long=min(num,len(matches))
    matches_list=[]
    for matche in matches[:long]:
        matches_list.append([des1[matche.queryIdx].pt,des2[matche.trainIdx].pt])
    return matches_list

def go(method,name,num):
    deb=datetime.now()
    os.mkdir(name)
    fichiers=os.listdir('frames')
    nb_images=len(fichiers)
    for i in range(nb_images-1):
        matches=find_matches_couple(fichiers[i],fichiers[i+1],method,num)
        drawmatches(fichiers[i],fichiers[i+1],matches,name+'/'+name+'_'+str(i+1)+'_'+str(i+2))
    fin=datetime.now()
    print(method+' done => '+str(fin-deb))

go('sift','sift',500)
go('surf','surf',500)
go('orb','orb',500)
go('gms','gms',500)

go('sift','sift_best',50)
go('surf','surf_best',50)
go('orb','orb_best',50)
go('gms','gms_best',50)