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

颜色追踪块CamShift滤波器。

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

roslaunch openni_launch openni.launch

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

然后运行下面的launch文件:

roslaunch rbx1_vision camshift.launch

当视频出现时,通过鼠标画矩形将图像中的某个对象框住。这个矩形表示所选的区域,试着移动所选的区域。

以下是我的实验结果:

看看代码:启动文件为:camshift.launch。

rbx1/rbx1_vision/nodes/camshift.py节点代码:
#!/usr/bin/env python

""" camshift_node.py - Version 1.1 2013-12-20
Modification of the ROS OpenCV Camshift example using cv_bridge and publishing the ROI
coordinates to the /roi topic.
""" import rospy
import cv2
from cv2 import cv as cv
from rbx1_vision.ros2opencv2 import ROS2OpenCV2
from std_msgs.msg import String
from sensor_msgs.msg import Image
import numpy as np class CamShiftNode(ROS2OpenCV2):
def __init__(self, node_name):
ROS2OpenCV2.__init__(self, node_name) self.node_name = node_name # The minimum saturation of the tracked color in HSV space,
# as well as the min and max value (the V in HSV) and a
# threshold on the backprojection probability image.
self.smin = rospy.get_param("~smin", )
self.vmin = rospy.get_param("~vmin", )
self.vmax = rospy.get_param("~vmax", )
self.threshold = rospy.get_param("~threshold", ) # Create a number of windows for displaying the histogram,
# parameters controls, and backprojection image
cv.NamedWindow("Histogram", cv.CV_WINDOW_NORMAL)
cv.MoveWindow("Histogram", , )
cv.NamedWindow("Parameters", )
cv.MoveWindow("Parameters", , )
cv.NamedWindow("Backproject", )
cv.MoveWindow("Backproject", , ) # Create the slider controls for saturation, value and threshold
cv.CreateTrackbar("Saturation", "Parameters", self.smin, , self.set_smin)
cv.CreateTrackbar("Min Value", "Parameters", self.vmin, , self.set_vmin)
cv.CreateTrackbar("Max Value", "Parameters", self.vmax, , self.set_vmax)
cv.CreateTrackbar("Threshold", "Parameters", self.threshold, , self.set_threshold) # Initialize a number of variables
self.hist = None
self.track_window = None
self.show_backproj = False # These are the callbacks for the slider controls
def set_smin(self, pos):
self.smin = pos def set_vmin(self, pos):
self.vmin = pos def set_vmax(self, pos):
self.vmax = pos def set_threshold(self, pos):
self.threshold = pos # The main processing function computes the histogram and backprojection
def process_image(self, cv_image):
try:
# First blur the image
frame = cv2.blur(cv_image, (, )) # Convert from RGB to HSV space
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) # Create a mask using the current saturation and value parameters
mask = cv2.inRange(hsv, np.array((., self.smin, self.vmin)), np.array((., ., self.vmax))) # If the user is making a selection with the mouse,
# calculate a new histogram to track
if self.selection is not None:
x0, y0, w, h = self.selection
x1 = x0 + w
y1 = y0 + h
self.track_window = (x0, y0, x1, y1)
hsv_roi = hsv[y0:y1, x0:x1]
mask_roi = mask[y0:y1, x0:x1]
self.hist = cv2.calcHist( [hsv_roi], [], mask_roi, [], [, ] )
cv2.normalize(self.hist, self.hist, , , cv2.NORM_MINMAX);
self.hist = self.hist.reshape(-)
self.show_hist() if self.detect_box is not None:
self.selection = None # If we have a histogram, track it with CamShift
if self.hist is not None:
# Compute the backprojection from the histogram
backproject = cv2.calcBackProject([hsv], [], self.hist, [, ], ) # Mask the backprojection with the mask created earlier
backproject &= mask # Threshold the backprojection
ret, backproject = cv2.threshold(backproject, self.threshold, , cv.CV_THRESH_TOZERO) x, y, w, h = self.track_window
if self.track_window is None or w <= or h <=:
self.track_window = , , self.frame_width - , self.frame_height - # Set the criteria for the CamShift algorithm
term_crit = ( cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, , ) # Run the CamShift algorithm
self.track_box, self.track_window = cv2.CamShift(backproject, self.track_window, term_crit) # Display the resulting backprojection
cv2.imshow("Backproject", backproject)
except:
pass return cv_image def show_hist(self):
bin_count = self.hist.shape[]
bin_w =
img = np.zeros((, bin_count*bin_w, ), np.uint8)
for i in xrange(bin_count):
h = int(self.hist[i])
cv2.rectangle(img, (i*bin_w+, ), ((i+)*bin_w-, -h), (int(180.0*i/bin_count), , ), -)
img = cv2.cvtColor(img, cv2.COLOR_HSV2BGR)
cv2.imshow('Histogram', img) def hue_histogram_as_image(self, hist):
""" Returns a nice representation of a hue histogram """
histimg_hsv = cv.CreateImage((, ), , ) mybins = cv.CloneMatND(hist.bins)
cv.Log(mybins, mybins)
(_, hi, _, _) = cv.MinMaxLoc(mybins)
cv.ConvertScale(mybins, mybins, . / hi) w,h = cv.GetSize(histimg_hsv)
hdims = cv.GetDims(mybins)[]
for x in range(w):
xh = ( * x) / (w - ) # hue sweeps from - across the image
val = int(mybins[int(hdims * x / w)] * h / )
cv2.rectangle(histimg_hsv, (x, ), (x, h-val), (xh,,), -)
cv2.rectangle(histimg_hsv, (x, h-val), (x, h), (xh,,), -) histimg = cv2.cvtColor(histimg_hsv, cv.CV_HSV2BGR) return histimg if __name__ == '__main__':
try:
node_name = "camshift"
CamShiftNode(node_name)
try:
rospy.init_node(node_name)
except:
pass
rospy.spin()
except KeyboardInterrupt:
print "Shutting down vision node."
cv.DestroyAllWindows()

颜色追踪块CamShift---33的更多相关文章

  1. SPOJ 16549 - QTREE6 - Query on a tree VI 「一种维护树上颜色连通块的操作」

    题意 有操作 $0$ $u$:询问有多少个节点 $v$ 满足路径 $u$ 到 $v$ 上所有节点(包括)都拥有相同的颜色$1$ $u$:翻转 $u$ 的颜色 题解 直接用一个 $LCT$ 去暴力删边连 ...

  2. SP16549 QTREE6 - Query on a tree VI LCT维护颜色联通块

    \(\color{#0066ff}{ 题目描述 }\) 给你一棵n个点的树,编号1~n.每个点可以是黑色,可以是白色.初始时所有点都是黑色.下面有两种操作请你操作给我们看: 0 u:询问有多少个节点v ...

  3. bzoj2906 颜色 分块+块大小分析

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=2906 题解 如果可以离线的话,那么这个题目就是一个莫队的裸题. 看上去这个数据范围也还会一个根 ...

  4. OpenGL学习进程(11)第八课:颜色绘制的详解

        本节是OpenGL学习的第八个课时,下面将详细介绍OpenGL的颜色模式,颜色混合以及抗锯齿.     (1)颜色模式: OpenGL支持两种颜色模式:一种是RGBA,一种是颜色索引模式. R ...

  5. iOS 中实现随机颜色

    开发中为了测试能够快速看到效果很多时候我们对颜色采用随机颜色 代码块如下 UIColor * randomColor= [UIColor colorWithRed:((float)arc4random ...

  6. Sass函数--颜色函数--HSL函数

    HSL函数简介HSL颜色函数包括哪些具体的函数,所起的作用是什么: hsl($hue,$saturation,$lightness):通过色相(hue).饱和度(saturation)和亮度(ligh ...

  7. printf 字体颜色打印

    为了给printf着色方便, 我们可以定义一些宏: view plain copy to clipboard print ? #define NONE          "/033[m&qu ...

  8. 2-4 Sass的函数功能-颜色函数

    RGB颜色函数-RGB()颜色函数 在 Sass 的官方文档中,列出了 Sass 的颜色函数清单,从大的方面主要分为 RGB , HSL 和 Opacity 三大函数,当然其还包括一些其他的颜色函数, ...

  9. python----模块知识拓展

    1.hashlib ------configpraser-------- xml hashlib 模块导入:import hashlib 模块说明:用于将信息加密,明文变成密文 功能说明 MD5算法 ...

随机推荐

  1. [转]change the linux startup logo

    1. Make sure that you have the kernel sources installed. As annoying as this may seem, you will need ...

  2. 图像显示与加载——opencv(转)

    cvLoadImage() 函数:IplImage* cvLoadImage("图像名称",参数): 函数作用:加载图片: 函数返回值:为IplImage结构体: 参数说明:参数值 ...

  3. yeild

    正在使用cpu的线程,退出,返回等待池,其他优先级相同的线程竞争上岗.

  4. java,android获取系统当前时间

    SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss ");Date curDate = ...

  5. php的数组与数据结构

    一.数组的分类与定义 分类: 1.索引数组  $array = array(1,2,3,4,5); 2.关联数组  $array=array(1=>"aa","bb ...

  6. linux 杀死进程的方法

    # kill -pid 注释:标准的kill命令通常都能达到目的.终止有问题的进程,并把进程的资源释放给系统.然而,如果进程启动了子进程,只杀死父进程,子进程仍在运行,因此仍消耗资源.为了防止这些所谓 ...

  7. IIS 发布后文件拒绝访问

    今天遇到一个很小的问题,代码中写XML文件,本地运行没有问题,一发布到服务器上就出现 代码如下: XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load ...

  8. keil(持续更新)

    1函数格式提示 2  cording时有警告和错误提示 3 类的成员 提示

  9. matlab的正则表达式讲解[转]

    引言.啥是正则表达式?正则表达式是干啥的?我理解就和我们在word或者其他编辑软件里点的查找.替换的作用是差不多的,不过功能要强大的多,当然使用起来也稍微复杂一些.书上的定义差不多是这样的:正则表达式 ...

  10. Unity3D入门(一):环境搭建

    1.Unity3D 目前最新正式版本是4.2.1f  官网下载,以前的版本安装时候需要序列号激活,新版本4.2.1f 不需要,完全免费,但发布的时候需要许可证 2.要学习的同学,下载频道可以找到破解补 ...