首先需要對cv2.goodFeaturesToTrack函數有一個基本的瞭解:
詳細的介紹在這裡:http://baike.baidu.com/link?url=zPK3iMJJfkKO0k2bniENAvksIRY0-Ci7weIcpE9AdFybc5tyUsbPT9cUTx4-vBDmhAnCsSKToS3QP8n4JaInfa
我要處理的視頻是熱成像儀拍攝的,目的是追蹤高溫的地區:
代碼如下:
#!/usr/bin/env python# -*- coding: utf-8 -*-"""作者:功能:跟蹤溫度高的地區。"""import numpy as npimport cv2import cv2.cv as cvcap=cv2.VideoCapture("my.mp4")feasize=1max=200qua=0.05mindis=7blocksize=10usehaar=Truek=0.04paras=dict(maxCorners=200, qualityLevel=0.05, minDistance=7, blockSize=10, useHarrisDetector=True, k=0.04)keypoints=list()mask=Nonemarker=Nonedef 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 keypointsdef 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()
追蹤效果如下: