原创博客:转载请标明出处:http://www.cnblogs.com/zxouxuewei/

关键点:是多个方向上亮度变化强的区域。

opencv:版本是2.4.

光学流函数:calcOpticalFlowPyrLK()。(关键点侦测器使用goodFeaturesToTrack())二者结合。

相应的启动文件为:lk_tracker.launch

首先确保你的kinect驱动或者uvc相机驱动能正常启动:(如果你使用的是kinect,请运行openni驱动)

roslaunch openni_launch openni.launch

  如果你没有安装kinect深度相机驱动,请看我前面的博文。

然后运行下面的launch文件:

roslaunch rbx1_vision good_features.launch

当视频出现时,通过鼠标画矩形将图像中的某个对象框住。这个矩形表示所选的区域,你会看到这个区域中会出现一些绿色的小圆点,他们是goodFeaturesToTrack()。侦测器在该区域中发现的关键点。然后试着移动你所选择的区域,你会看到光学流函数:calcOpticalFlowPyrLK()跟踪关键点。

以下是我的运行结果:

移动后:

下面让我们来看看代码:主要是lk_tracker.py脚本

#!/usr/bin/env python""" lk_tracker.py - Version 1.1 2013-12-20
    Based on the OpenCV lk_track.py demo codeCreated for the Pi Robot Project: http://www.pirobot.orgCopyright (c) 2011 Patrick Goebel.  All rights reserved.This program is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2 of the License, or(at your option) any later version.This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See theGNU General Public License for more details at:http://www.gnu.org/licenses/gpl.html
"""

import rospy
import cv2
import cv2.cv as cv
import numpy as np
from rbx1_vision.good_features import GoodFeaturesclass LKTracker(GoodFeatures):def __init__(self, node_name):super(LKTracker, self).__init__(node_name)self.show_text = rospy.get_param("~show_text", True)self.feature_size = rospy.get_param("~feature_size", 1)# LK parametersself.lk_winSize = rospy.get_param("~lk_winSize", (10, 10))self.lk_maxLevel = rospy.get_param("~lk_maxLevel", 2)self.lk_criteria = rospy.get_param("~lk_criteria", (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 20, 0.01))self.lk_params = dict( winSize  = self.lk_winSize, maxLevel = self.lk_maxLevel, criteria = self.lk_criteria)    self.detect_interval = 1self.keypoints = Noneself.detect_box = Noneself.track_box = Noneself.mask = Noneself.grey = Noneself.prev_grey = Nonedef process_image(self, cv_image):try:# If we don't yet have a detection box (drawn by the user
            # with the mouse), keep waitingif self.detect_box is None:return cv_image# Create a greyscale version of the imageself.grey = cv2.cvtColor(cv_image, cv2.COLOR_BGR2GRAY)# Equalize the grey histogram to minimize lighting effectsself.grey = cv2.equalizeHist(self.grey)# If we haven't yet started tracking, set the track box to the
            # detect box and extract the keypoints within itif self.track_box is None or not self.is_rect_nonzero(self.track_box):self.track_box = self.detect_boxself.keypoints = self.get_keypoints(self.grey, self.track_box)else:if self.prev_grey is None:self.prev_grey = self.grey# Now that have keypoints, track them to the next frame# using optical flowself.track_box = self.track_keypoints(self.grey, self.prev_grey)# Process any special keyboard commands for this moduleif self.keystroke != -1:try:cc = chr(self.keystroke & 255).lower()if cc == 'c':# Clear the current keypointsself.keypoints = Noneself.track_box = Noneself.detect_box = Noneexcept:passself.prev_grey = self.greyexcept:passreturn cv_image               def track_keypoints(self, grey, prev_grey):# We are tracking points between the previous frame and the# current frameimg0, img1 = prev_grey, grey# Reshape the current keypoints into a numpy array required# by calcOpticalFlowPyrLK()p0 = np.float32([p for p in self.keypoints]).reshape(-1, 1, 2)# Calculate the optical flow from the previous frame to the current framep1, st, err = cv2.calcOpticalFlowPyrLK(img0, img1, p0, None, **self.lk_params)# Do the reverse calculation: from the current frame to the previous frametry:p0r, st, err = cv2.calcOpticalFlowPyrLK(img1, img0, p1, None, **self.lk_params)# Compute the distance between corresponding points in the two flowsd = abs(p0-p0r).reshape(-1, 2).max(-1)# If the distance between pairs of points is < 1 pixel, set# a value in the "good" array to True, otherwise Falsegood = d < 1# Initialize a list to hold new keypointsnew_keypoints = list()# Cycle through all current and new keypoints and only keep# those that satisfy the "good" condition abovefor (x, y), good_flag in zip(p1.reshape(-1, 2), good):if not good_flag:continuenew_keypoints.append((x, y))# Draw the keypoint on the imagecv2.circle(self.marker_image, (x, y), self.feature_size, (0, 255, 0, 0), cv.CV_FILLED, 8, 0)# Set the global keypoint list to the new list    self.keypoints = new_keypoints# Convert the keypoints list to a numpy arraykeypoints_array = np.float32([p for p in self.keypoints]).reshape(-1, 1, 2)  # If we have enough points, find the best fit ellipse around themif len(self.keypoints) > 6:track_box = cv2.fitEllipse(keypoints_array)else:# Otherwise, find the best fitting rectangletrack_box = cv2.boundingRect(keypoints_array)except:track_box = Nonereturn track_boxif __name__ == '__main__':try:node_name = "lk_tracker"LKTracker(node_name)rospy.spin()except KeyboardInterrupt:print "Shutting down LK Tracking node."cv.DestroyAllWindows()

转载于:https://www.cnblogs.com/zxouxuewei/p/5409961.html

利用光学流跟踪关键点---30相关推荐

  1. 翻译:Identifying Encrypted Malware Traffic with Contextual Flow Data利用上下文流数据识别加密恶意软件流量

    利用上下文流数据识别加密恶意软件流量 blake anderson思科blake.anderson@cisco.com 摘要 识别加密网络流量中包含的威胁是一组独特的挑战.监视此通信量以防威胁和恶意软 ...

  2. MedianFlow中值流跟踪算法源码

    一.MedianFlow算法简介 该算法属于TLD跟踪算法中的Tracking部分.TLD算法可参考:Tracking-Learning-Detection原理分析.它基于LK光流跟踪算法,并使用FB ...

  3. java基础5:工厂模式、单例模式、File文件类、递归、IO流、Properties配置文件、网络编程、利用IO流模拟注册登录功能、关于反射、JDK动态代理

    1.工厂模式 23种java设计模式之一 1)提供抽象类(基类) 2)提供一些子类,完成方法重写 3)提供一个接口:完成具体子类的实例化对象的创建,不能直接new子类,构造函数私有化. 优点:具体的子 ...

  4. 如何利用ide进行跟踪调试_使用调试器进行事后跟踪

    如何利用ide进行跟踪调试 我最近一直在使用的大多数调试器的好功能是能够在断点上记录信息. 这对于理解代码而无需修改是非常有用的,因为它涉及字节码修改. 让我们考虑一下这种非常琐碎且效率低下的函数实现 ...

  5. 利用缓冲流读取跟写入

    import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.Fi ...

  6. java用于保存登录对象怎么写,利用对象流模仿登陆注册功能——Java对象流应用...

    要求: 1.建一个User类,类中结构有用户名.密码.注册日期三个属性, 一个注册方法和一个登录方法. 2.建一个Data类,用一个List装载用户数据,有保存数据,读取数据的方法. 3.利用对象流, ...

  7. 利用CUDA流重叠计算和数据传输

    利用CUDA流重叠计算和数据传输 CUDA中有一个重要的概念是 流(stream). 其实它代表着一系列的指令的执行队列. 这个执行队列就像他的名字一样, 有着固定的执行顺序(就像河流只能向一个方向固 ...

  8. Java裁剪音乐,利用IO流

    ** Java裁剪音乐,利用IO流 ** 主方法 public static void main(String[] args) throws IOException {//输入流对象,路径要裁剪文件路 ...

  9. 利用转换流将GBK格式文件以UTF-8输出

    3.利用转换流将GBK格式文件以UTF-8输出 解题思路:       1,InputStreamReader(File file,"gbk");读入文件       2,Outp ...

最新文章

  1. 性能提升3倍的树莓派4,被爆设计缺陷!
  2. C++的一维数组键盘输入问题
  3. python操作系统-Python操作系统
  4. HDU 2079-课程时间(生成函数)
  5. Visual Guide to NoSQL Systems
  6. elementui树状菜单tree_vue.js+element-ui做出菜单树形结构
  7. 《机器学习实战》笔记(04):基于概率论的分类方法 - 朴素贝叶斯分类
  8. VB界面设计与测试规则
  9. OSPF基本概念单与区域配置
  10. kettle执行结果面板步骤度量(一)——转换
  11. Ubuntu设置自动登录
  12. 智能眼镜的两种显示方式
  13. 洛谷P1526 智破连环阵
  14. kubelet启动pod源码分析(三)
  15. Pdf.js 解决电子印章问题(最新)
  16. css隐藏浏览器的x/y轴
  17. Android课程设计-体育新闻app
  18. jzoj 3837 心灵终结
  19. 【Linux系统】第10节 linux系统文件及目录权限详解
  20. 【Unity3D】AR应用中,关于东南西北方位的判断。

热门文章

  1. 【牛客 - 315F】美丽的项链(线性dp,递推,我为人人)
  2. 【POJ - 3048】Max Factor (数论,打表,水题)
  3. Java (jdk win 10)
  4. android studio 库工程,Android Studio 添加已有工程方法
  5. java私有成员的访问_java – 使用私有成员或公共访问器的方法
  6. 微信公众平台网站开发JS_SDK遇到的bug——wx.config注册提示成功,但部分接口注册失败问题
  7. pycharm 开发app_windows及mac下开发Excel, python+xlwings开发环境配置
  8. linux如何用cat看一行数据库,linux的cat命令
  9. android 虚拟键盘改变单个按键颜色_这款机械键盘很特别!一亿次按键寿命还有高颜值...
  10. Struts2的action中处理JSONP方式提交的中文乱码问题: