python里使用cv2.goodFeaturesToTrack()函数追踪图像里的关键点

版权声明:欢迎联系我转载!欢迎交流分享!转载和分享请注明出处! https://blog.csdn.net/u014303046/article/details/51023202

    首先需要对cv2.goodFeaturesToTrack函数有一个基本的了解:

    详细的介绍在这里:http://baike.baidu.com/link?url=zPK3iMJJfkKO0k2bniENAvksIRY0-Ci7weIcpE9AdFybc5tyUsbPT9cUTx4-vBDmhAnCsSKToS3QP8n4JaInfa

    我要处理的视频是热成像仪拍摄的,目的是追踪高温的区域:

代码如下:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
作者:
功能:跟踪温度高的区域。
"""

import numpy as np
import cv2
import cv2.cv as cv

cap=cv2.VideoCapture("my.mp4")

feasize=1
max=200
qua=0.05
mindis=7
blocksize=10
usehaar=True
k=0.04

paras=dict(maxCorners=200,
           qualityLevel=0.05,
           minDistance=7,
           blockSize=10,
           useHarrisDetector=True,
           k=0.04)

keypoints=list()
mask=None
marker=None


def getkpoints(imag,input1):
    mask1=np.zeros_like(input1)
    x=0
    y=0
    w1,h1=input1.shape
    #print 666
    #print input1.shape
    input1=input1[0:w1,200:h1]
    #print input1.shape
    try:
        w,h=imag.shape

        #w=w/2
        #h=h/2
        #print w,h
    except:
        return None

    mask1[y:y+h,x:x+w]=255

    keypoints=list()

    #kp=cv2.goodFeaturesToTrack(input1,
                                #mask1,
                                #**paras)
    #input1=input1.fromarray
    kp=cv2.goodFeaturesToTrack(input1,200,0.04,7)                               
   
    #cv2.goodFeaturesToTrack(image, maxCorners, qualityLevel, minDistance)
    if kp is not None and len(kp)>0:
        for x,y in np.float32(kp).reshape(-1,2):
            keypoints.append((x,y))
    return keypoints


def process(image):
    grey1=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
    grey=cv2.equalizeHist(grey1)
    keypoints=getkpoints(grey,grey1)
    #print keypoints

    #print image.shape
    if keypoints is not None and len(keypoints)>0:
        
        for x,y in keypoints:
            
            cv2.circle(image, (int(x+200),y), 3, (255,255,0))
    return image



#p=cv2.imread('1.png')
#p2=process(p)

#cv2.imshow('my',p2)

#cv2.waitKey(0)
#cv2.destroyAllWindows()

while (cap.isOpened()):
    ret,frame=cap.read()
    frame=process(frame)
    cv2.imshow('frame',frame)
    if cv2.waitKey(1)&0xFF==ord('q'):
        break    
    
cv2.waitKey(0)
cv2.destroyAllWindows()
   


    追踪效果如下:


展开阅读全文

没有更多推荐了,返回首页