码迷,mamicode.com
首页 > 编程语言 > 详细

(转)python+opencv实现动态物体追踪

时间:2018-05-16 16:39:40      阅读:520      评论:0      收藏:0      [点我收藏+]

标签:camera   分享图片   gaussian   show   blog   ide   ons   gray   release   

原文链接:https://blog.csdn.net/cike14/article/details/50649811

 

import cv2  
import numpy as np  
camera=cv2.VideoCapture(0)  
firstframe=None  
while True:  
    ret,frame = camera.read()  
    if not ret:  
        break  
    gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)  
    gray=cv2.GaussianBlur(gray,(21,21),0)  
    if firstframe is None:  
        firstframe=gray  
        continue  
      
    frameDelta = cv2.absdiff(firstframe,gray)  
    thresh = cv2.threshold(frameDelta, 25, 255, cv2.THRESH_BINARY)[1]  
    thresh = cv2.dilate(thresh, None, iterations=2)  
    # cnts= cv2.findContours(thresh.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)  
      
    x,y,w,h=cv2.boundingRect(thresh)  
    frame=cv2.rectangle(frame,(x,y),(x+w,y+h),(0,0,255),2)  
    cv2.imshow("frame", frame)  
    cv2.imshow("Thresh", thresh)  
    cv2.imshow("frame2", frameDelta)  
    key = cv2.waitKey(1)&0xFF  
      
    if key == ord("q"):  
        break  
  
camera.release()  
cv2.destroyAllWindows()  

效果图

技术分享图片

(转)python+opencv实现动态物体追踪

标签:camera   分享图片   gaussian   show   blog   ide   ons   gray   release   

原文地址:https://www.cnblogs.com/6-6-8-8/p/9046426.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!