特征点检测及跟踪

13 篇文章 0 订阅

Haar脸部探测器扫描图像的特定类型的对象。不同的策略
需要寻找更小的图像特征,从一个帧中可以很容易地跟踪
到下一个。这些特性称为关键点或兴趣点。重点倾向于
在多个方向上有剧烈变化的区域。
例如如下图所示:
这里写图片描述

左边的图像显示了左眼区域的像素正确的。左边的正方形表示强度变化最大的区域各个方向。这样一个区域的中心是图像的一个关键点,它很可能会出现无论其方向或比例如何,在脸部的相同位置被重新检测。

OpenCV包括一些关键点探测器包括goodFeaturesToTrack(),cornerHarris()和SURF()。我们将使用goodFeaturesToTrack()给我们编程的例子。右边的图像说明返回的关键点goodFeaturesToTrack()。

您可以看到,关键点集中在强度梯度最大的区域。相反地,画的区域相当同质的几乎没有或没有关键点。(复制这张图片或找到关键点在其他图像中,请查看Python项目叫script_good_features.py在rbx1_vision /scripts。)我们现在已经准备好了在现场检测关键点视频流。调用我们的ROS节点
good_features.py和它可以在rbx1_vision /nodes的子目录。是相应的启动文件是good_features。在启动子目录中启动。是启动文件包含一些参数,这些参数影响返回的关键点
goodFeaturesToTrack():

•maxcorner:为返回的关键点设置上限。
•qualityLevel ::反映出一个角形特征在它之前的强度
数量是一个关键点。设置较低的值会返回更多的点。
•minDistance:键点之间的最小像素数。
•blockSize:一个用于计算的像素周围的大小
那里是否有一个角落。
•useHarrisDetector:是否使用原来的哈里斯角探测器
或一个最小特征值准则。
•k:哈里斯角探测器的一个自由参数。
good_features.launch为这些参数设置合理的默认值,但请尝试试验他们的效果。
要运行检测器,首先要确保你的相机的驱动程序已经启动并运行前面描述的,然后运行:

当视频出现时,用鼠标在某个对象上画一个矩形的形象。一个矩形应该显示选定区域,您应该看到一些绿色点表示由goodFeaturesToTrack()在该区域发现的关键点。
试着在图片的其他区域画一个方框,看看你能不能猜出它的位置要点将会出现。注意,我们还没有跟踪这些关键字——我们只是简单的计算它们在你的选择框内的任何一个场景。
您可能会注意到,一些关键点甚至在a上出现了一些抖动场景的固定部分。这是由于视频中的噪音。强大的要点不通过在表格的一角画出你的测试框,你可以看到噪声或者其他高对比度。
现在我们来看看代码。

#!/usr/bin/env python
import roslib; roslib.load_manifest('rbx1_vision')
import rospy
import cv2
import cv2.cv as cv
from ros2opencv2 import ROS2OpenCV2
import numpy as np
Robot Vision - 1569.
class GoodFeatures(ROS2OpenCV2):

def __init__(self, node_name):
super(GoodFeatures, self).__init__(node_name)
# Do we show text on the display?
self.show_text = rospy.get_param("~show_text", True)
# How big should the feature points be (in pixels)?
self.feature_size = rospy.get_param("~feature_size", 1)
# Good features parameters
self.gf_maxCorners = rospy.get_param("~gf_maxCorners", 200)
self.gf_qualityLevel = rospy.get_param("~gf_qualityLevel", 0.05)
self.gf_minDistance = rospy.get_param("~gf_minDistance", 7)
self.gf_blockSize = rospy.get_param("~gf_blockSize", 10)
self.gf_useHarrisDetector =
rospy.get_param("~gf_useHarrisDetector", True)
self.gf_k = rospy.get_param("~gf_k", 0.04)
# Store all parameters together for passing to the detector
self.gf_params = dict(maxCorners = self.gf_maxCorners,
qualityLevel = self.gf_qualityLevel,
minDistance = self.gf_minDistance,
blockSize = self.gf_blockSize,
useHarrisDetector = self.gf_useHarrisDetector,
k = self.gf_k)
# Initialize key variables
self.keypoints = list()
self.detect_box = None
self.mask = None
def process_image(self, cv_image):
# If the user has not selected a region, just return the image
if not self.detect_box:
return cv_image
# Create a greyscale version of the image
grey = cv2.cvtColor(cv_image, cv2.COLOR_BGR2GRAY)
# Equalize the histogram to reduce lighting effects
grey = cv2.equalizeHist(grey)
# Get the good feature keypoints in the selected region
keypoints = self.get_keypoints(grey, self.detect_box)
# If we have points, display them
if keypoints is not None and len(keypoints) > 0:
for x, y in keypoints:
cv2.circle(self.marker_image, (x, y), self.feature_size,
(0, 255, 0, 0), cv.CV_FILLED, 8, 0)
# Process any special keyboard commands
if 32 <= self.keystroke and self.keystroke < 128:
cc = chr(self.keystroke).lower()
if cc == 'c':
# Clear the current keypoints
Robot Vision - 15764.
keypoints = list()
self.detect_box = None
return cv_image
def get_keypoints(self, input_image, detect_box):
# Initialize the mask with all black pixels
self.mask = np.zeros_like(input_image)
# Get the coordinates and dimensions of the detect_box
try:
x, y, w, h = detect_box
except:
return None
# Set the selected rectangle within the mask to white
self.mask[y:y+h, x:x+w] = 255
# Compute the good feature keypoints within the selected region
keypoints = list()
kp = cv2.goodFeaturesToTrack(input_image, mask = self.mask,
**self.gf_params)
if kp is not None and len(kp) > 0:
for x, y in np.float32(kp).reshape(-1, 2):
keypoints.append((x, y))
return keypoints
if __name__ == '__main__':
try:
node_name = "good_features"
GoodFeatures(node_name)
rospy.spin()
except KeyboardInterrupt:
print "Shutting down the Good Features node."
cv.DestroyAllWindows()

总的来说,我们看到script具有与face_decetor.py节点相同的结构,我们初始化了扩展ROS2OpenCV2类的GoodFeatures类。然后,定义处理大多数工作的process_image()函数。

self.gf_maxCorners = rospy.get_param("~gf_maxCorners", 200)
self.gf_qualityLevel = rospy.get_param("~gf_qualityLevel", 0.02)
self.gf_minDistance = rospy.get_param("~gf_minDistance", 7)
self.gf_blockSize = rospy.get_param("~gf_blockSize", 10)
self.gf_useHarrisDetector =
rospy.get_param("~gf_useHarrisDetector", True)
self.gf_k = rospy.get_param("~gf_k", 0.04)

与Haar检测器一样,好的特性检测器也需要一些参数
调整其行为。也许上面两个最重要的参数是qualityLevel 和minDistance。质量级别的较小值将导致的特征点,但其中很多是由于噪音而不是非常从一个图像帧到另一个图像。设置更高的值,这是太高的意志在最强大的角落只产生几个关键点。大约是0.02在自然场景的视频中,似乎也有很好的平衡。
minDistance参数指定我们允许的像素的最小距离要点之间。这个值越大,就必须得到进一步的键值在较少的地方。

def process_image(self, cv_image):
# If the user has not selected a region, just return the image
if not self.detect_box:
return cv_image
# Create a greyscale version of the image
grey = cv2.cvtColor(cv_image, cv2.COLOR_BGR2GRAY)
# Equalize the histogram to reduce lighting effects
grey = cv2.equalizeHist(grey)
# Get the good feature keypoints in the selected region
keypoints = self.get_keypoints(grey, self.detect_box)

与face检测器节点一样,我们首先定义process_image()函数
将图像转换成灰度,并将直方图均衡。由此产生的图像是传递给get_keypoints()函数,该函数执行所有查找正确的工作
特性。

def get_keypoints(self, input_image, detect_box):
# Initialize the mask with all black pixels
self.mask = np.zeros_like(input_image)
# Get the coordinates and dimensions of the detect_box
try:
  x, y, w, h = detect_box
except:
  return None
# Set the selected rectangle within the mask to white
  self.mask[y:y+h, x:x+w] = 255
# Compute the good feature keypoints within the selected region
  keypoints = list()
  kp = cv2.goodFeaturesToTrack(input_image, mask = self.mask,
**self.gf_params)
if kp is not None and len(kp) > 0:
for x, y in np.float32(kp).reshape(-1, 2):
   keypoints.append((x, y))
return keypoints

get_keypoints()函数实现OpenCV的GoodFeaturesToTrack
探测器。因为我们只希望用户选择的方框内的关键点(detect_box)我们用一个所有零的掩码来掩模图像
机器人视觉- 159(黑色),然后填充所有白色像素(255)的检测盒的输出cv2 . goodfeaturestotrack()函数是一个关键坐标的向量所以我们使用一个小小的numpy整形来将其转换为Python列表(x,y)对。由此产生的将列表返回给process_image()函数,该函数将在该函数中绘制点的形象。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

穿着帆布鞋也能走猫步

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值