颜色追踪块CamShift---33
原创博客:转载请标明出处:http://www.cnblogs.com/zxouxuewei/
颜色追踪块CamShift滤波器。
首先确保你的kinect驱动或者uvc相机驱动能正常启动:(如果你使用的是kinect,请运行openni驱动)
roslaunch openni_launch openni.launch
如果你没有安装kinect深度相机驱动,请看我前面的博文。
然后运行下面的launch文件:
roslaunch rbx1_vision camshift.launch
当视频出现时,通过鼠标画矩形将图像中的某个对象框住。这个矩形表示所选的区域,试着移动所选的区域。
以下是我的实验结果:


看看代码:启动文件为:camshift.launch。
#!/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的更多相关文章
- SPOJ 16549 - QTREE6 - Query on a tree VI 「一种维护树上颜色连通块的操作」
题意 有操作 $0$ $u$:询问有多少个节点 $v$ 满足路径 $u$ 到 $v$ 上所有节点(包括)都拥有相同的颜色$1$ $u$:翻转 $u$ 的颜色 题解 直接用一个 $LCT$ 去暴力删边连 ...
- SP16549 QTREE6 - Query on a tree VI LCT维护颜色联通块
\(\color{#0066ff}{ 题目描述 }\) 给你一棵n个点的树,编号1~n.每个点可以是黑色,可以是白色.初始时所有点都是黑色.下面有两种操作请你操作给我们看: 0 u:询问有多少个节点v ...
- bzoj2906 颜色 分块+块大小分析
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=2906 题解 如果可以离线的话,那么这个题目就是一个莫队的裸题. 看上去这个数据范围也还会一个根 ...
- OpenGL学习进程(11)第八课:颜色绘制的详解
本节是OpenGL学习的第八个课时,下面将详细介绍OpenGL的颜色模式,颜色混合以及抗锯齿. (1)颜色模式: OpenGL支持两种颜色模式:一种是RGBA,一种是颜色索引模式. R ...
- iOS 中实现随机颜色
开发中为了测试能够快速看到效果很多时候我们对颜色采用随机颜色 代码块如下 UIColor * randomColor= [UIColor colorWithRed:((float)arc4random ...
- Sass函数--颜色函数--HSL函数
HSL函数简介HSL颜色函数包括哪些具体的函数,所起的作用是什么: hsl($hue,$saturation,$lightness):通过色相(hue).饱和度(saturation)和亮度(ligh ...
- printf 字体颜色打印
为了给printf着色方便, 我们可以定义一些宏: view plain copy to clipboard print ? #define NONE "/033[m&qu ...
- 2-4 Sass的函数功能-颜色函数
RGB颜色函数-RGB()颜色函数 在 Sass 的官方文档中,列出了 Sass 的颜色函数清单,从大的方面主要分为 RGB , HSL 和 Opacity 三大函数,当然其还包括一些其他的颜色函数, ...
- python----模块知识拓展
1.hashlib ------configpraser-------- xml hashlib 模块导入:import hashlib 模块说明:用于将信息加密,明文变成密文 功能说明 MD5算法 ...
随机推荐
- UITableViewCell 单元格样式
UITableViewCell 单元格样式作用 typedef NS_ENUM(NSInteger, UITableViewCellStyle) { UITableViewCellStyleDefau ...
- Oracle top N实现
在Oracle中实现select top N:由于Oracle不支持select top 语句,所以在Oracle中经常是用order by 跟rownum的组合来实现select top n的查询. ...
- 使用AppCan自带的升级功能实现移动端升级
1.需要在AppCan项目的config.xml文件中设置“更新地址”,即在执行uexWidget.checkUpdate();时访问的后台页面地址,比如: http://192.168.0.10:8 ...
- 支持多人协作的在线免费作图工具:ProcessOn
之前朋友给我推荐一款作图工具ProcessOn,出于好奇我就研究了一下它,今天我就给大家简单介绍一下这款免费的在线作图工具:ProcessOn 首先使用ProcessOn我们需要有一个帐号,这样每次操 ...
- linux之开发板与宿主机通信--ftp使用
在目标板终端上输入命令: # ftp 192.16.77.66 //192.16.77.66是宿主机IP # cd /home/ //这里可以使用linux命令,但不能使有TAB键 # get ...
- 介绍NSURLSession网络请求套件
昨天翻译了一篇<NSURLSession的使用>的文章,地址:http://www.cnblogs.com/JackieHoo/p/4995733.html,原文是来自苹果官方介绍NSUR ...
- linux下文件系统的介绍
一.linux文件系统的目录结构 目录 描述 / 根目录 /bin 做为基础系统所需要的最基础的命令就是放在这里.比如 ls.cp.mkdir等命令:功能和/usr/bin类似,这个目录中的文件都是可 ...
- 《使用this作为返回值的相关问题》
//使用this作为返回值的相关问题: /* 如果在某个方法中把this作为返回值,则可以多次连续的调用同一个方法,从而使得代码 更加简洁,但是,这种把this作为返回值的方法可能造成实际意义的模糊, ...
- C语言 约瑟夫圈问题:N个人围成一圈,从第一个人开始按顺序报数并编号1,2,3,……N,然后开始从第一个人转圈报数,凡是报到3的退出圈子。则剩下的最后一个人编号是多少。
样例输入3 输出2 输入100 输出91 代码及分析: #include<stdio.h> int main() { int i,n,N,out,a[1000]; out=i=n=0 ...
- SQL2008:在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器。请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接。
错误: 解决方案: 1.查看服务是否开启,如果没有开启,请开启服务. 2.服务器名称栏:格式:主机名\实例名或者ip\实例名 如:10.10.4.81\SQLSERVER