源码在这

#!/usr/bin/env python

'''
Camshift tracker
================ This is a demo that shows mean-shift based tracking
You select a color objects such as your face and it tracks it.
This reads from video camera (0 by default, or the camera number the user enters) http://www.robinhewitt.com/research/track/camshift.html Usage:
------
camshift.py [<video source>] To initialize tracking, select the object with mouse Keys:
-----
ESC - exit
b - toggle back-projected probability visualization
''' import numpy as np
import cv2
import video class App(object):
def __init__(self, video_src):
self.cam = video.create_capture(video_src) # 开启摄像头
ret, self.frame = self.cam.read() # 读取一帧图片
cv2.namedWindow('camshift') #创建 名为 camshift的窗口
cv2.setMouseCallback('camshift', self.onmouse) #在窗口上增加回调函数 self.selection = None
self.drag_start = None
self.tracking_state = 0
self.show_backproj = False def onmouse(self, event, x, y, flags, param):
x, y = np.int16([x, y]) # BUG
if event == cv2.EVENT_LBUTTONDOWN:
self.drag_start = (x, y)
self.tracking_state = 0
return
if self.drag_start:
if flags & cv2.EVENT_FLAG_LBUTTON:
h, w = self.frame.shape[:2]
xo, yo = self.drag_start
x0, y0 = np.maximum(0, np.minimum([xo, yo], [x, y]))
x1, y1 = np.minimum([w, h], np.maximum([xo, yo], [x, y]))
self.selection = None
if x1-x0 > 0 and y1-y0 > 0:
self.selection = (x0, y0, x1, y1)
else:
self.drag_start = None
if self.selection is not None:
self.tracking_state = 1 def show_hist(self):
bin_count = self.hist.shape[0]
bin_w = 24
img = np.zeros((256, bin_count*bin_w, 3), np.uint8)
for i in xrange(bin_count):
h = int(self.hist[i])
cv2.rectangle(img, (i*bin_w+2, 255), ((i+1)*bin_w-2, 255-h), (int(180.0*i/bin_count), 255, 255), -1)
img = cv2.cvtColor(img, cv2.COLOR_HSV2BGR)
cv2.imshow('hist', img) def run(self):
while True:
ret, self.frame = self.cam.read() #读取一帧图片
vis = self.frame.copy() # 复制一份
hsv = cv2.cvtColor(self.frame, cv2.COLOR_BGR2HSV) # 将图片从 BGR 空间转换到 HSV 空间
mask = cv2.inRange(hsv, np.array((0., 60., 32.)), np.array((180., 255., 255.))) # 找出颜色区间在 np.array((0., 60., 32.)), np.array((180., 255., 255.) if self.selection:
x0, y0, x1, y1 = self.selection
self.track_window = (x0, y0, x1-x0, y1-y0)
hsv_roi = hsv[y0:y1, x0:x1]
mask_roi = mask[y0:y1, x0:x1]
hist = cv2.calcHist( [hsv_roi], [0], mask_roi, [16], [0, 180] )
cv2.normalize(hist, hist, 0, 255, cv2.NORM_MINMAX);
self.hist = hist.reshape(-1)
self.show_hist() vis_roi = vis[y0:y1, x0:x1]
cv2.bitwise_not(vis_roi, vis_roi)
vis[mask == 0] = 0 if self.tracking_state == 1:
self.selection = None
prob = cv2.calcBackProject([hsv], [0], self.hist, [0, 180], 1)
prob &= mask
term_crit = ( cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1 )
track_box, self.track_window = cv2.CamShift(prob, self.track_window, term_crit) if self.show_backproj:
vis[:] = prob[...,np.newaxis]
try: cv2.ellipse(vis, track_box, (0, 0, 255), 2)
except: print track_box cv2.imshow('camshift', vis) ch = 0xFF & cv2.waitKey(5)
if ch == 27:
break
if ch == ord('b'):
self.show_backproj = not self.show_backproj
cv2.destroyAllWindows() if __name__ == '__main__':
import sys
try: video_src = sys.argv[1]
except: video_src = 0
print __doc__
App(video_src).run()

第117行:sys.argv[]  是用来获取命令行参数的,常见的sys.argv[0]表示本身文件路径,所以一般都从1 开始 这里我将官方文档的教程源码抄下来大家看看就懂了

# jack.py
#!/usr/bin/python
# Filename: using_sys.py import sys print 'The command line arguments are:'
for i in sys.argv:
print i print '\n\nThe PYTHONPATH is', sys.path, '\n'

  在终端输入

python jack.py ba la ba la 

  结果显示

The command line arguments are:
jack.py
ba
la
ba
la The PYTHONPATH is ['/home/x-power/OpenCV', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/home/x-power/.local/lib/python2.7/site-packages', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/PILcompat', '/usr/lib/python2.7/dist-packages/gtk-2.0']



camshift.py OpenCv例程阅读的更多相关文章

  1. common.py OpenCv例程阅读

    #!/usr/bin/env python ''' This module contais some common routines used by other samples. ''' import ...

  2. video.py OpenCv例程阅读

    #!/usr/bin/env python ''' Video capture sample. Sample shows how VideoCapture class can be used to a ...

  3. 【双目备课】OpenCV例程_stereo_calib.cpp解析

    stereo_calib是OpenCV官方代码中提供的最正统的双目demo,无论数据集还是代码都有很好实现. 一.代码效果: 相关的内容包括28张图片,1个xml和stereo_calib.cpp的代 ...

  4. OpenCV例程实现人脸检测

    前段时间看的OpenCV,其实有很多的例子程序,参考代码值得我们学习,对图像特征提取三大法宝:HOG特征,LBP特征,Haar特征有一定了解后. 对本文中的例子程序刚开始没有调通,今晚上调通了,试了试 ...

  5. python中 __init__.py的例程

    __init__.py一般是为空,用在一个python目录中,标识该目录是一个python的模块包 先上来看一个例子: .: test1 test2 test_init.py ./test1: tim ...

  6. OpenCV 例程

    采集图片显示视频: #include <iostream> #include <opencv2/opencv.hpp> using namespace std; using n ...

  7. Opencv Cookbook阅读笔记(四):用直方图统计像素

    灰度直方图的定义 灰度直方图是灰度级的函数,描述图像中该灰度级的像素个数(或该灰度级像素出现的频率):其横坐标是灰度级,纵坐标表示图像中该灰度级出现的个数(频率). #include <open ...

  8. [OpenCV-Python] OpenCV 中视频分析 部分 VI

    部分 VI视频分析 OpenCV-Python 中文教程(搬运)目录 39 Meanshift 和 和 Camshift 目标 • 本节我们要学习使用 Meanshift 和 Camshift 算法在 ...

  9. python + opencv: kalman 跟踪

    之前博文中讲解过kalman滤波的原理和应用,这里用一个跟踪鼠标的例程来演示怎么在opencv里用自带的kalman函数进行目标跟踪,文章的内容对做图像跟踪有借鉴意义.文章主要是网络资源进行整理和简单 ...

随机推荐

  1. Metasploit学习笔记之——情报搜集

    1.情报搜集 1.1外围信息搜索 1.1.1通过DNS和IP地址挖掘目标网络信息 (1)whois域名注冊信息查询(BT5.kali专有):root@kali:~# whois testfire.ne ...

  2. WPF绑定各种数据源之object数据源

    一.WPF绑定各种数据源索引 WPF 绑定各种数据源之Datatable WPF绑定各种数据源之object数据源 WPF绑定各种数据源之xml数据源 WPF绑定各种数据源之元素控件属性 Bindin ...

  3. CentOS笔记-常用网络命令

    1.curl & wget 使用curl或wget命令,不用离开终端就可以下载文件.如你用curl,键入curl -O后面跟一个文件路径.wget则不需要任何选项.下载的文件在当前目录. cu ...

  4. NameNode和JobTracker的网络接口

    Hadoop快速入门 http://hadoop.apache.org/docs/r1.0.4/cn/quickstart.html

  5. 求a + aa + aaa + aaaa + aaaaa ...的值,例如:1 + 11 + 111,2 + 22 + 222 + 2222 + 22222

    #include <stdio.h> unsigned superposition(unsigned m, unsigned n); int main() { printf("1 ...

  6. 20170221 SE03 打包请求

    请求处理:SE03 只会有其中两三个功能,还需要学习. 请求打包,是传输请求时候常用的一种方法. 步骤如下: 注意点:1.打包请求一定仔细检查,不要遗漏           如果请求不多,分开传是更好 ...

  7. spark集成hivecontext配置

    spark版本:spark-1.6.0-bin-hadoop2.6hive版本:hive 1.2.1 1.hive-site.xml<property>  <name>hive ...

  8. DataContractAttribute.IsReference

    IsReference property in data contract It determines how objects are serialized, by default, IsRefere ...

  9. ${varname:-defaultvalue}

    ${varname:-defaultvalue}的意思是:如果varname存在且非null,则返回其值:否则,返回defaultvalue. 用途:如果变量未定义,则返回默认值.

  10. iOS 生成随机数

    arc4random 1.获取一个随机整数范围在:[0,100)包括0,不包括100 int x = arc4random() % 100; 2.  获取一个随机数范围在:[500,1000],包括5 ...