基于颜色跟踪Python脚本
2019-06-05 20:56
423 查看
版权声明:本文为博主原创文章,未经博主允许不得转载,如有侵权,烦请第一时间告知。 https://blog.csdn.net/yufeilongyuan/article/details/90940958
写在前面的话:文章内容来源于但不限于网络、书籍、个人心得体会等,意在总结和方便各位同行快速参考,共同学习进步,记录自己的问题。错误在所难免,有请各位批评斧正。如有侵权,烦请第一时间通知,我会立即删除相关内容,万分感谢!
[code]import numpy as np import argparse import time import cv2 # construct the argument parse and parse the arguments ap = argparse.ArgumentParser() ap.add_argument("-v", "--video", help = "path to the (optional) video file") args = vars(ap.parse_args()) # define the upper and lower boundaries for a color # to be considered "blue" blueLower = np.array([100,67,0],dtype="uint8") blueUpper = np.array([255,128,50],dtype="uint8") # load the video if not args.get("video"): camera = cv2.VideoCapture(0) else: camera = cv2.VideoCapture(args["video"]) # keep looping while True: # grab the current frame (grabbed,frame) = camera.read() # check to see if we have reached the end of the video if not grabbed: break # determine which pixels fall within the blue boundaries # and then blur the binary image blue = cv2.inRange(frame,blueLower,blueUpper) blue = cv2.GaussianBlur(blue,(3,3),0) # find contours in the image (_,cnts,_) = cv2.findContours(blue.copy(),cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # check to see if any contours were found if len(cnts) > 0: # sort the contours and find the largest one -- # we will assume this contour coorespondes to the # area of my phone cnt = sorted(cnts,key=cv2.contourArea,reverse=True)[0] # compute the (rotated) bounding box around then # contour and then draw it rect = np.int32(cv2.boxPoints(cv2.minAreaRect(cnt))) cv2.drawContours(frame,[rect],-1,(0,255,0),2) # show the frame and the binary image cv2.imshow("Traccking",frame) cv2.imshow("Binary",blue) # if your machine is fast, it may display the frames in # what appears to be 'fast forward' since more than 32 # frames per second are being displayed -- a si 4000 mple hack # is just to sleep for a tiny bit in between frames; # however, if your computer is slow, you probably want to # comment out this line time.sleep(0.025) # if the 'q' key is pressed, stop the loop if cv2.waitKey(1) & 0xFF == ord("q"): break # cleanup the camera and close any open windows camera.release() cv2.destroyAllWindows()
相关文章推荐
- 基于颜色模式的颜色条形码检测(使用python 2.7和opencv 2.2)
- 基于七牛Python SDK写的一个同步脚本
- 用Python脚本语言建立一个基于应用程序的GUI快速启动
- [python]基于BeautifulSoup解析百度贴吧water_maker(谁最水)的脚本,练手打趣之作[更新0.3]
- 基于python智慧树刷课脚本在C#语言下的设计与实现
- 怎样开始写第一个基于python的selenium脚本
- [置顶] Webdriver(selenium2)基于Python脚本实现登录填写日志(分步讲解)
- 基于Python的黑色星期五查找脚本
- 基于Python的网页文档处理脚本实现
- 【selenium】Selenium基于Python3的Web自动化测试脚本在IE上运行慢的解决方法
- OpenCV视频分析与对象跟踪C++(一)视频读写、背景消除建模(BSM)、对象检测与跟踪(基于颜色)
- python3小项目2-基于autopy的游戏脚本
- 用Python编写一个基于终端的实现翻译的脚本
- DIY一个基于树莓派和Python的无人机视觉跟踪系统
- DIY一个基于树莓派和Python的无人机视觉跟踪系统
- 基于python3的手机号生成脚本
- 使用python脚本实现基于指定字符串的文本排序
- 基于Tkinter利用python实现颜色空间转换程序
- 基于python的json脚本解析
- Apache-tomcat-6.0.36配置基于Python的CGI脚本解析