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

首先看看face_tracker2.launch启动文件中的参数:(括号中的值表示默认值)

use_depth_for_tracking:(True) 如果你使用的是kienct,将此值设置为真则会丢弃那些离脸部太远的点。
min_keypoints:(20) 在我们添加新的关键点之前的最小关键点的数量。

abs_min_keypoints:(6) 在我们发现丢失了当前的人脸追踪并重新检测之前,关键点的绝对最低数量。
add_keypoint_distance:(10) 一个新的关键点和任意现有关键点之间的最小值(以像素点为单位)。
std_err_xy:(2.5) 标准误差(以像素为单位)用于判断关键点是否为异常值。
pct_err_z:(1.5) 深度阈值(以百分比表示)确定一个离开面部关键点何时被删除。
max_mse:(10000)在重新开始检测人脸开始,面部的当前特征值的最大的总均方差。
expand_roi:(1.02) 寻找新的关键点时,用于在每个循环中增加扩张系数。
add_keypoints_interval:(1)尝试添加新的关键点的频繁程度,为1 表示每一帧都添加。
drop_keypoints_interval:(1) 尝试删除新的关键点的频繁程度,为1 表示每一帧都删除。

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

roslaunch openni_launch openni.launch

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

然后运行下面的launch文件:

roslaunch rbx1_vision face_tracker2.launch

在视频窗口的最前端,在键盘上输入‘c’键。清除当前的关键点,强制重新检测检测面部。

这是我的运行结果:

rbx1/rbx1_vision/nodes/face_tracker2.py查看代码:
#!/usr/bin/env python""" face_tracker2.py - Version 1.1 2013-12-20Combines the OpenCV Haar face detector with Good Features to Track and Lucas-Kanadeoptical flow tracking.  Keypoints are added and dropped according to simple statisicalclustering rules.Created for the Pi Robot Project: http://www.pirobot.orgCopyright (c) 2012 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 math import isnan, isinf
from rbx1_vision.face_detector import FaceDetector
from rbx1_vision.lk_tracker import LKTrackerclass FaceTracker(FaceDetector, LKTracker):def __init__(self, node_name):super(FaceTracker, self).__init__(node_name)self.n_faces = rospy.get_param("~n_faces", 1)self.show_text = rospy.get_param("~show_text", True)self.show_add_drop = rospy.get_param("~show_add_drop", False)self.feature_size = rospy.get_param("~feature_size", 1)self.use_depth_for_tracking = rospy.get_param("~use_depth_for_tracking", False)self.min_keypoints = rospy.get_param("~min_keypoints", 20)self.abs_min_keypoints = rospy.get_param("~abs_min_keypoints", 6)self.std_err_xy = rospy.get_param("~std_err_xy", 2.5) self.pct_err_z = rospy.get_param("~pct_err_z", 0.42) self.max_mse = rospy.get_param("~max_mse", 10000)self.add_keypoint_distance = rospy.get_param("~add_keypoint_distance", 10)self.add_keypoints_interval = rospy.get_param("~add_keypoints_interval", 1)self.drop_keypoints_interval = rospy.get_param("~drop_keypoints_interval", 1)self.expand_roi_init = rospy.get_param("~expand_roi", 1.02)self.expand_roi = self.expand_roi_initself.face_tracking = Trueself.frame_index = 0self.add_index = 0self.drop_index = 0self.keypoints = list()self.detect_box = Noneself.track_box = Noneself.grey = Noneself.prev_grey = Nonedef process_image(self, cv_image):try:# 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)# Step 1: Detect the face if we haven't alreadyif self.detect_box is None:self.keypoints = list()self.track_box = Noneself.detect_box = self.detect_face(self.grey)else:# Step 2: If we aren't yet tracking keypoints, get them nowif not self.track_box or not self.is_rect_nonzero(self.track_box):self.track_box = self.detect_boxself.keypoints = self.get_keypoints(self.grey, self.track_box)# Store a copy of the current grey image used for LK tracking                   if self.prev_grey is None:self.prev_grey = self.grey           # Step 3: If we have keypoints, track them using optical flowself.track_box = self.track_keypoints(self.grey, self.prev_grey)# Step 4: Drop keypoints that are too far from the main clusterif self.frame_index % self.drop_keypoints_interval == 0 and len(self.keypoints) > 0:((cog_x, cog_y, cog_z), mse_xy, mse_z, score) = self.drop_keypoints(self.abs_min_keypoints, self.std_err_xy, self.max_mse)if score == -1:self.detect_box = Noneself.track_box = Nonereturn cv_image# Step 5: Add keypoints if the number is getting too low if self.frame_index % self.add_keypoints_interval == 0 and len(self.keypoints) < self.min_keypoints:self.expand_roi = self.expand_roi_init * self.expand_roiself.add_keypoints(self.track_box)else:self.frame_index += 1self.expand_roi = self.expand_roi_init# Store a copy of the current grey image used for LK tracking            self.prev_grey = self.grey# Process any special keyboard commands for this moduleif self.keystroke != -1:try:cc = chr(self.keystroke & 255).lower()print ccif cc == 'c':self.keypoints = []self.track_box = Noneself.detect_box = Noneelif cc == 'd':self.show_add_drop = not self.show_add_dropexcept:passexcept AttributeError:passreturn cv_imagedef add_keypoints(self, track_box):# Look for any new keypoints around the current keypoints# Begin with a mask of all black pixelsmask = np.zeros_like(self.grey)# Get the coordinates and dimensions of the current track boxtry:((x,y), (w,h), a) = track_boxexcept:try:x,y,w,h = track_boxx = x + w / 2y = y + h / 2a = 0except:rospy.loginfo("Track box has shrunk to zero...")returnx = int(x)y = int(y)# Expand the track box to look for new keypointsw_new = int(self.expand_roi * w)h_new = int(self.expand_roi * h)pt1 = (x - int(w_new / 2), y - int(h_new / 2))pt2 = (x + int(w_new / 2), y + int(h_new / 2))mask_box = ((x, y), (w_new, h_new), a)# Display the expanded ROI with a yellow rectangleif self.show_add_drop:cv2.rectangle(self.marker_image, pt1, pt2, cv.RGB(255, 255, 0))# Create a filled white ellipse within the track_box to define the ROIcv2.ellipse(mask, mask_box, cv.CV_RGB(255,255, 255), cv.CV_FILLED)if self.keypoints is not None:# Mask the current keypointsfor x, y in [np.int32(p) for p in self.keypoints]:cv2.circle(mask, (x, y), 5, 0, -1)new_keypoints = cv2.goodFeaturesToTrack(self.grey, mask = mask, **self.gf_params)# Append new keypoints to the current list if they are not# too far from the current cluster      if new_keypoints is not None:for x, y in np.float32(new_keypoints).reshape(-1, 2):distance = self.distance_to_cluster((x,y), self.keypoints)if distance > self.add_keypoint_distance:self.keypoints.append((x,y))# Briefly display a blue disc where the new point is addedif self.show_add_drop:cv2.circle(self.marker_image, (x, y), 3, (255, 255, 0, 0), cv.CV_FILLED, 2, 0)# Remove duplicate keypointsself.keypoints = list(set(self.keypoints))def distance_to_cluster(self, test_point, cluster):min_distance = 10000for point in cluster:if point == test_point:continue# Use L1 distance since it is faster than L2distance = abs(test_point[0] - point[0])  + abs(test_point[1] - point[1])if distance < min_distance:min_distance = distancereturn min_distancedef drop_keypoints(self, min_keypoints, outlier_threshold, mse_threshold):sum_x = 0sum_y = 0sum_z = 0sse = 0keypoints_xy = self.keypointskeypoints_z = self.keypointsn_xy = len(self.keypoints)n_z = n_xy#         if self.use_depth_for_tracking:
#             if self.depth_image is None:
#                 return ((0, 0, 0), 0, 0, -1)# If there are no keypoints left to track, start overif n_xy == 0:return ((0, 0, 0), 0, 0, -1)# Compute the COG (center of gravity) of the clusterfor point in self.keypoints:sum_x = sum_x + point[0]sum_y = sum_y + point[1]mean_x = sum_x / n_xymean_y = sum_y / n_xymean_z = 0if self.use_depth_for_tracking and not self.depth_image is None:for point in self.keypoints:                              try:z = self.depth_image[point[1], point[0]]except:n_z = n_z - 1continue        if not isnan(z):sum_z = sum_z + zelse:n_z = n_z - 1continuetry:mean_z = sum_z / n_zexcept:mean_z = -1else:mean_z = -1# Compute the x-y MSE (mean squared error) of the cluster in the camera planefor point in self.keypoints:sse = sse + (point[0] - mean_x) * (point[0] - mean_x) + (point[1] - mean_y) * (point[1] - mean_y)#sse = sse + abs((point[0] - mean_x)) + abs((point[1] - mean_y))# Get the average over the number of feature pointsmse_xy = sse / n_xy# The MSE must be > 0 for any sensible feature clusterif mse_xy == 0 or mse_xy > mse_threshold:return ((0, 0, 0), 0, 0, -1)# Throw away the outliers based on the x-y variancemax_err = 0for point in self.keypoints:std_err = ((point[0] - mean_x) * (point[0] - mean_x) + (point[1] - mean_y) * (point[1] - mean_y)) / mse_xyif std_err > max_err:max_err = std_errif std_err > outlier_threshold:keypoints_xy.remove(point)if self.show_add_drop:# Briefly mark the removed points in redcv2.circle(self.marker_image, (point[0], point[1]), 3, (0, 0, 255), cv.CV_FILLED, 2, 0)   try:keypoints_z.remove(point)n_z = n_z - 1except:passn_xy = n_xy - 1# Now do the same for depthif self.use_depth_for_tracking and not self.depth_image is None:sse = 0for point in keypoints_z:try:z = self.depth_image[point[1], point[0]]sse = sse + (z - mean_z) * (z - mean_z)except:n_z = n_z - 1try:mse_z = sse / n_zexcept:mse_z = 0# Throw away the outliers based on depth using percent error # rather than standard error since depth values can jump# dramatically at object boundariesfor point in keypoints_z:try:z = self.depth_image[point[1], point[0]]except:continuetry:pct_err = abs(z - mean_z) / mean_zif pct_err > self.pct_err_z:keypoints_xy.remove(point)if self.show_add_drop:# Briefly mark the removed points in redcv2.circle(self.marker_image, (point[0], point[1]), 2, (0, 0, 255), cv.CV_FILLED)  except:passelse:mse_z = -1self.keypoints = keypoints_xy# Consider a cluster bad if we have fewer than min_keypoints leftif len(self.keypoints) < min_keypoints:score = -1else:score = 1return ((mean_x, mean_y, mean_z), mse_xy, mse_z, score)if __name__ == '__main__':try:node_name = "face_tracker"FaceTracker(node_name)rospy.spin()except KeyboardInterrupt:print "Shutting down face tracker node."cv.DestroyAllWindows()

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

动态的添加和丢弃关键点---32相关推荐

  1. Windows x64为动态代码添加unwind信息

    Windows x64为动态代码添加unwind信息 0x00 0x01 0x00 64位程序的堆栈回溯与32位差别很大,调试器和 RtlCaptureStackBackTrace 无法再使用 ebp ...

  2. 牛客刷题-Java面试题库【动态更新添加题目】(2023.06.19更新)

    讲在前面 ✨ 牛客刷题日记–理解为重中之重 刷题一方面是持续的了解到自己哪方面比较欠缺,另一方面也是从各大厂的面试题可以看出当前所需的技术栈的偏重点,持续的巩固基础和查漏补缺,一如代码深似海–学无止境 ...

  3. gif加文字 php,gif动态图片添加文字 gif制作软件 怎样给gif动态图片添加文字

    gif动态图片添加文字 gif制作软件 怎样给gif动态图片添加文字 现在有各种各样的表情包.搞笑图片以及搞笑视频都深受大家的喜爱,之前咱们也有一起学习过怎样用视频制作gif动态图片,相信大家肯定还记 ...

  4. 《ActionScript 3.0基础教程》——第2章 往舞台动态地添加对象2.1 创建库资源,并为它命名...

    本节书摘来自异步社区<ActionScript 3.0基础教程>一书中的第2章,第2.1节,作者: [美]Doug Winnie 更多章节内容可以访问云栖社区"异步社区" ...

  5. 动态引入/添加js脚本

    前言 动态添加脚本到页面中 动态引入/添加js脚本 var script=document.createElement("script"); script.type="t ...

  6. js 给动态li添加动态点击事件

    一.给动态li添加动态点击事件 试了网上的on,live,bind绑定,都不起作用.最后看到https://bbs.csdn.net/topics/390414057 上的代码.试了一遍,demo o ...

  7. html动态网页添加音频_将音频添加到网页

    html动态网页添加音频 In principle, adding audio to a web page is quite straightforward. Assuming you have a ...

  8. django的view中或者前台中动态的添加属性

    1.view中动态的添加属性,到时候这个queryset集合就有了添加的属性,前台就可以获取数据了 news_info = NewsInfo.objects.all() for new_info in ...

  9. jq 在元素中追加html代码,jquery添加html代码,怎样动态的添加一条html代码?

    下面的文章内容要给大家介绍的就是jquery动态的添加html代码的内容,那么你知道应该如何添加吗?一起来看看方式吧. 添加新bai内容的4个jquery方法,分别是append() - 在被bai选 ...

最新文章

  1. Redis 5.0.3默认配置启动报错解决方法
  2. Counting power sets
  3. 使用WampServer搭建本地PHP环境,绑定域名,配置伪静态
  4. ESRGAN:基于GAN的增强超分辨率方法(附代码解析)
  5. 机器人实现屠宰自动化
  6. ios apple pay 证书配置
  7. Oracle Controlfile控制文件中记录的信息片段sections
  8. 【SSL】HTTPS配置全过程
  9. C语言如何编辑资源文件,用C语言做个简单的计算机,上面是代码,请教上资源文件在哪里编写,要新建什么文件...
  10. 分页插件pagehelper ,在sql server 中是怎么配置的
  11. linux tail 命令
  12. matlab中英文文献,matlab外文文献
  13. 2022最新第四方聚合支付系统源码+详细搭建教程
  14. 开源的物理引擎_开源物理引擎
  15. cfd计算机模拟,CFD软件数值模拟分析(中联智泵)
  16. 【电子知识篇】放大器定义与分类
  17. 先天八卦与后天八卦原理及区别
  18. 音程示范歌曲,lilypond example
  19. Python实现网络聊天
  20. 学习笔记2018-10-26 读论文A single algorithm to retrieve turbidity from remotely-sensed data in all coastal

热门文章

  1. c++中的左移、右移运算
  2. Pixhawk---超声波模块添加说明(I2C方式)
  3. python 求一个矩阵的距离矩阵的方法:
  4. Mysql 图像二进制保存-Blocb、TinyBlob、MediumBlob、LongBlob
  5. ofstream与ate的故事 经典!
  6. 《汇编语言》-王爽-实验7
  7. 15.17 对缺乏潜在类型机制的补偿
  8. Eclipse console 中文乱码解决
  9. 【转】xargs命令详解,xargs与管道的区别
  10. nodejs(6)express学习