使用多个图像实现SIFT

时间:2017-08-01 09:10:01

标签: python opencv sift

我是python和图像处理方面的新手。我遵循了关于SIFT的教程,它实际上适用于放置在文件夹中的一个图像。我想检测文件夹中的多个图像,它将显示该图像的名称。

import cv2
import numpy as np
MIN_MATCH_COUNT=40

detector4=cv2.xfeatures2d.SIFT_create()

FLANN_INDEX_KDITREE4=0
flannParam4=dict(algorithm=FLANN_INDEX_KDITREE4,tree=5)
flann4=cv2.FlannBasedMatcher(flannParam4,{})

trainImg4=cv2.imread("data/s1.jpg",0)
trainKP4,trainDesc4=detector4.detectAndCompute(trainImg4,None)

cam4=cv2.VideoCapture(0)
while True:
    ret4, QueryImgBGR4=cam4.read()
    QueryImg4=cv2.cvtColor(QueryImgBGR4,cv2.COLOR_BGR2GRAY)
    queryKP4,queryDesc4=detector4.detectAndCompute(QueryImg4,None)
    matches4=flann4.knnMatch(queryDesc4,trainDesc4,k=2)

    goodMatch4=[]
    for m4,n4 in matches4:
        if(m4.distance<0.75*n4.distance):
            goodMatch4.append(m4)

    if(len(goodMatch4)>15):
        tp4=[]
        qp4=[]
        for m4 in goodMatch4:
            tp4.append(trainKP4[m4.trainIdx].pt)
            qp4.append(queryKP4[m4.queryIdx].pt)
       tp4,qp4=np.float32((tp4,qp4))
       H4,status=cv2.findHomography(tp4,qp4,cv2.RANSAC,3.0)
       hhh,www=trainImg4.shape
       trainBorder4=np.float32([[[0,0],[0,hhh-1],[www-1,hhh-1],[www-1,0]]])
       queryBorder4=cv2.perspectiveTransform(trainBorder4,H4)
       cv2.polylines(QueryImgBGR4,[np.int32(queryBorder4)],True,(0,255,0),5)
       print "water  found- %d/%d"%(len(goodMatch4),MIN_MATCH_COUNT)

    cv2.imshow('GO ',QueryImgBGR4)
    if cv2.waitKey(10)==ord('q'):
        break
cam4.release()
cv2.destroyAllWindows()

0 个答案:

没有答案