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

原创博客:转载请标明出处: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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/458081.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

register_globals(全局变量注册开关)

register_globals&#xff0c;是php.ini文件里面的一个配置选项&#xff0c;接下来&#xff0c;我们可以通过例程来分析一下&#xff0c;当register_globals on 与 register_globals off 的时候&#xff0c;对php语言的一些安全影响。测试源代码如下&#xff1a;index.html 源…

精述IBM的MQTT协议和MQTT-S协议

一&#xff0e;MQTT简介 MQTT (Message Queuing Telemetry Transport) 是由IBM研发的构建在TCP/IP之上的简单轻量的消息协议&#xff0c;目标使用场景为受限制环境&#xff0c;如低带宽、高延迟、不可靠网络&#xff0c;很适用于M2M和IoT中。它的竞争协议包括XMPP协议和IETF的C…

20150103--SQL连接查询+视图-02

20150103--SQL连接查询视图-02 子查询 一条查询语句出现在另外一条查询语句的内部&#xff0c;这条语句就被称之为子查询语句。 子查询分类 子查询可以根据子查询返回的结果以及子查询出现的位置两种方式进行分类 按结果分类&#xff1a; 标量子查询&#xff1a;子查询返回的结…

QtQuick controls和controls2 自定义样式

2019独角兽企业重金招聘Python工程师标准>>> controls import QtQuick 2.7 import QtQuick.Controls 1.4 import QtQuick.Controls.Styles 1.4Rectangle {color: "green"Button {id:buttontext: qsTr("Second page")anchors.centerIn: parentpr…

电脑开机3秒就重启循环_电脑修好后客户不愿支付上门费,行,那电脑开机60秒自动关机吧!...

上门维修电脑已经非常普及了&#xff0c;上门维修电脑费用要比送修(送到维修店)费用高&#xff0c;送修电脑只有一项维修费用&#xff0c;上门维修电脑比送修多了一项费用&#xff0c;这项费用就是上门费。有人说维修电脑收取上门费用不合理&#xff0c;其实这样的说法非常自私…

jQuery属性筛选选择器

2019独角兽企业重金招聘Python工程师标准>>> 代码一 <h2>属性筛选选择器</h2><h3>[attval]、[att]、[att|val]、[att~val]</h3><div class"left" testattr"true" ><div class"div" testattr"t…

java 代码优化

Java程序中的内存管理机制是通过GC完成的&#xff0c;“一个对象创建后被放置在JVM的堆内存中&#xff0c;当永远不在应用这个对象的时候将会被JVM在堆内存中回收。被创建的对象不能再生&#xff0c;同时也没有办法通过程序语句释放”&#xff08;这个是《Java的GC机制》中提到…

MATLAB 长度和像素_Matlab中短时傅里叶变换 spectrogram和stft的用法

在Matlab中&#xff0c;做短时傅里叶变换需要使用函数spectrogram&#xff0c;而在Matlab2019中&#xff0c;引入了一个新的函数stft&#xff0c;下面我们就来看下这两个函数都如何使用。短时傅里叶变换的基本原理就是将数据分段加窗&#xff0c;做fft&#xff0c;在分段时会有…

图像处理基础——灰度共生矩阵

标准定义如下&#xff1a;对于取定的方向θ 和距离 d, 在方向为θ的直线上, 一个像元灰度为 i, 另一个与其相距为 d 像元的灰度为 j 的点对出现的频数即为灰度共生矩阵第(i, j)阵元的值。 怎样理解呢&#xff1f;看起来好复杂呀 呜呜呜 小白理解&#xff1a;灰度共生矩阵就…

iphone查看删除的短信_iPhone12发布!刚买的苹果手机短信全部消失了怎么办?

原标题&#xff1a;iPhone12发布&#xff01;刚买的苹果手机短信全部消失了怎么办&#xff1f;目前&#xff0c;人们的社交除了面对面交谈&#xff0c;用的最多的就是通过手机进行聊天&#xff0c;比如用QQ、微信和短信、邮件等方式&#xff0c;虽然短信不会用来一般的聊天&…

python基础之01数据类型-变量-运算浅解

python的数据类型 1 数字 数字分为整型&#xff08;int&#xff09;&#xff0c;长整型&#xff08;long&#xff09;&#xff0c;浮点型&#xff08;float&#xff09;&#xff0c;复数&#xff08;complex&#xff09; 整型较为常用的功能&#xff1a; >>> a-4 >…

使用Caffe进行手写数字识别执行流程解析

之前在 http://blog.csdn.net/fengbingchun/article/details/50987185 中仿照Caffe中的examples实现对手写数字进行识别&#xff0c;这里详细介绍下其执行流程并精简了实现代码&#xff0c;使用Caffe对MNIST数据集进行train的文章可以参考 http://blog.csdn.net/fengbingchun/…

obs可以装手机吗?_原神PC和手机数据互通吗 PC和手机可以一起玩吗

在原神中&#xff0c;很多玩家都在PC端创建了角色&#xff0c;那么疑问来了&#xff0c;PC端与手机端的账号会是互通的吗&#xff1f;下面小编就为大家带来原神PC和手机数据互通吗的相关内容&#xff0c;一起来看看吧&#xff01;更多攻略&#xff1a;原神攻略大全PC和手机数据…

三维点云目标提取总结(续)

三维点云目标提取&#xff08;续&#xff09; 3.三维点云目标提取 3.1一般流程 先根据个人认识总结一下目标提取的一般性步骤&#xff1a; 如上所示&#xff0c;三维点云的目标提取关键性的两步即为&#xff1a;特征提取与选择、分类&#xff0c;是不是整个方法流程与图像中的目…

安卓高手之路之java层Binder

很多人一提到Binder就说代理模式&#xff0c;人云亦云的多&#xff0c;能理解精髓的少。 本篇文章就从设计角度分析一下java层BInder的设计目标&#xff0c;以及设计思路&#xff0c;设计缺陷&#xff0c;从而驾驭它。 对于【邦德儿】的理解, 从通信的角度来看&#xff0c;就是…

ftp改为sftp_浅谈 FTP、FTPS 与 SFTP

二狗子最近搭建了一个图片分享网站&#xff0c;每天都有好多人在他的网站上传许多照片&#xff0c;这些照片还会通过内部的逻辑同步到又拍云存储中&#xff0c;非常方便。但不久后问题就来了&#xff0c;由于刚开始的用户照片管理规划没有做好&#xff0c;随着用户上传的图片越…

如何解决秒杀的性能问题和超卖的讨论

2019独角兽企业重金招聘Python工程师标准>>> 最近业务试水电商&#xff0c;接了一个秒杀的活。之前经常看到淘宝的同行们讨论秒杀&#xff0c;讨论电商&#xff0c;这次终于轮到我们自己理论结合实际一次了。 ps&#xff1a;进入正文前先说一点个人感受&#xff0c;…

C# 从Excel中读取时间数据

之前写到从Excel中读取时间数据 //读取Excel数据Excel.Application xapp new Excel.Application();string filepath txt_Excel.Text;Excel.Workbook xbook xapp.Workbooks._Open(filepath, Missing.Value, Missing.Value,Missing.Value, Missing.Value, Missing.Value, Miss…

grid autosport额外内容下载慢_清理大王app下载-清理大王v1.0安卓下载

清理大王&#xff0c;下面由小编给大家介绍一下这款软件&#xff0c;该软件是一款非常不错的手机清理服务应用软件&#xff0c;清理大王app为用户提供了手机垃圾清理&#xff0c;内存加速&#xff0c;优化手机&#xff0c;解决手机卡顿的情况。感兴趣的朋友欢迎使用微侠下载&am…

怎么看cudnn的版本好_祖坟风水怎么看,好祖坟有什么征兆?

人们之所以看重祖坟的风水&#xff0c;是因为祖坟的风水与后代子孙的运势密切相关&#xff0c;可以说祖坟的风水好不好关系着子孙后代的运势顺不顺&#xff0c;因此对于祖坟的风水好坏人们是非常在意的&#xff0c;那么祖坟风水怎么看,好祖坟有什么征兆呢&#xff1f;下面是小编…