博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用GoodFeaturesToTrack进行关键点检测---29
阅读量:6957 次
发布时间:2019-06-27

本文共 5281 字,大约阅读时间需要 17 分钟。

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

 

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

opencv:版本是2.4.

侦测器:opencv有大量的关键点侦测器,我们本次采用goodFeaturesToTrack()。

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

侦测器返回的关键点变量:

    maxCorners : 设置最多返回的关键点数量。

    qualityLevel : 反应一个像素点强度有多强才能成为关键点。

    minDistance : 关键点之间的最少像素点。

    blockSize : 计算一个像素点是否为关键点时所取的区域大小。
    useHarrisDetector :使用原声的 Harris 角侦测器或最小特征值标准。
    k : 一个用在Harris侦测器中的自由变量。

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

roslaunch openni_launch openni.launch

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

然后运行下面的launch文件:

roslaunch rbx1_vision good_features.launch

当视频出现时,通过鼠标画矩形将图像中的某个对象框住。这个矩形表示所选的区域,你会看到这个区域中会出现一些绿色的小圆点,他们是goodFeaturesToTrack()。侦测器在该区域中发现的关键点,

以下是我的运行结果:

下面我们看看代码,主要是good_features.py脚本。

#!/usr/bin/env python""" good_features.py - Version 1.1 2013-12-20    Locate the Good Features To Track in a video stream.        Created for the Pi Robot Project: http://www.pirobot.org    Copyright (c) 2011 Patrick Goebel.  All rights reserved.    This program is free software; you can redistribute it and/or modify    it under the terms of the GNU General Public License as published by    the 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 of    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    GNU General Public License for more details at:        http://www.gnu.org/licenses/gpl.html"""import rospyimport cv2import cv2.cv as cvfrom rbx1_vision.ros2opencv2 import ROS2OpenCV2import numpy as npclass 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.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)                # 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):        try:            # 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 self.keystroke != -1:                try:                    cc = chr(self.keystroke & 255).lower()                    if cc == 'c':                        # Clear the current keypoints                        keypoints = list()                        self.detect_box = None                except:                    pass        except:            pass                                        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 keypointsif __name__ == '__main__':    try:        node_name = "good_features"        GoodFeatures(node_name)        rospy.spin()    except KeyboardInterrupt:        print "Shutting down the Good Features node."        cv.DestroyAllWindows()

 

你可能感兴趣的文章
NYOJ 题目77 开灯问题(简单模拟)
查看>>
模式识别
查看>>
设置文本编辑器的按回车时触发的事件
查看>>
关于android手机不能打印Log日志
查看>>
hdu 2157 How many ways?? **
查看>>
xcode8 更新cocoapods
查看>>
习题:海盗船(广搜)
查看>>
auto_ptr
查看>>
Windows系统的消息机制
查看>>
谈谈java中静态变量与静态方法在有继承关系的两个类中调用
查看>>
基础练习 十六进制转八进
查看>>
人工智能赢了人类,那又如何?
查看>>
Shell中字符串、数值的比较
查看>>
内存管理
查看>>
Arg_parser 1.8 发布,C++ 参数解析库
查看>>
DAC--解决windows验证无法登陆的问题
查看>>
[CALayer release]: message sent to deallocated instance iOS内存过度释放问题
查看>>
WPF界面设计技巧(4)—自定义列表项样式
查看>>
git push的时候每次都要输入用户名和密码的问题解决
查看>>
精益开发实战:用看板管理大型项目
查看>>