颜色追踪块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算法 ...
随机推荐
- C-crash的方法
#include <iostream> using namespace std; int main() { #if 0 //devide by 0 ; ; double d = i/j; ...
- [网络技术]网关 路由器 OSI
tracert 1.网关与路由 关键的区别:网关是这样一个网络节点:以两个不同协议搭建的网络可以通过它进行通信.路由器是这样一种设备:它能在计算机网络间收发数据包,同时创建一个覆盖网络(overlay ...
- App Store--心酸的上线路,说说那些不可思议的被拒理由
yoyeayoyea 您的应用包括色情内容(色情交易,色情展示). 原因是我们的销售人员,把几张艺术照放在个人相册里(头像),换成卡通头像,通过. 颜小风 被拒很正常 一次通过不正常. 之前上线 ...
- 打饭助手之NABC
Need: 同学们在早上跑操后要吃早饭,还有中午打饭时人更是多.常常要排很长的队伍,造成时间的浪费,和焦急的等待.因此我们需要错开打饭的高峰期,来避免打饭排队的悲哀. Approach: 通过获取摄像 ...
- JS内置对象
字符串对象 <script> //字符串对象 var str = "Hello worldlsgjlsjg"; document.write('string.lengt ...
- Hadoop c++开发
假设你有上百G的数据,你要统计出这些数据中,含有某些你感兴趣的内容的数据的有多少条,你会怎么做?在硬件条件允许的情况下,用hadoop并行计算是一个不错的选择. 为了使本文得以清晰地说明,我们不妨假设 ...
- 针对初学者的A*算法入门详解(附带Java源码)
英文题目,汉语内容,有点挂羊头卖狗肉的嫌疑,不过请不要打击我这颗想学好英语的心.当了班主任我才发现大一18本书,11本是英语的,能多用两句英语就多用,个人认为这样也是积累的一种方法. Thanks o ...
- hibernate实现增删改查的各种方法
1>接口(主要是增删改查的接口)BaseDao.java /** * * @author fly.zhou */ public interface IBaseDao { //增加对应实体的一条记 ...
- (spring-第16回【AOP基础篇】)基本概念
AOP(Aspect Oriented Programing),面向切面方程.介绍具体定义前,先看一个例子: package com.baobaotao.concept; public class F ...
- VS2010编译Qt5.4.0静态库
http://www.kavenblog.com/?p=375 1.Qt的跨平台十分优秀,但是在Windows上是还是会有许多问题,其中之一就是动态链接库的问题,Qt程序的发布必须带一个体积不小的DL ...