利用OpenCV检测手掌(palm)和拳头(fist)
思路:利用训练好的palm.xml和fist.xml文件,用OpenCV的CascadeClassifier对每一帧图像检测palm和fist,之后对多帧中检测到的palm和fist进行聚类分组,满足分组条件的区域为最终检测结果。
代码:
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp" #include <iostream>
#include <stdio.h> using namespace std;
using namespace cv; /** Function Headers */
void detectAndDisplay( Mat frame );
void RestoreVectors(vector<vector<Rect>>& vecs_bank, vector<Rect>& vecAll); /** Global variables */
String palm_cascade_name = "palm.xml";
String fist_cascade_name = "fist.xml";
CascadeClassifier palm_cascade;
CascadeClassifier fist_cascade;
string window_name = "Capture - Palm and fist detection"; /** @function main */
int main( int argc, const char** argv )
{
CvCapture* capture;
Mat frame; //-- 1. Load the cascades
if( !palm_cascade.load( palm_cascade_name ) ){ printf("--(!)Error loading\n"); return -; };
if( !fist_cascade.load( fist_cascade_name ) ){ printf("--(!)Error loading\n"); return -; }; //-- 2. Read the video stream
capture = cvCaptureFromCAM( - );
if( capture )
{
while( true )
{
frame = cvQueryFrame( capture ); //-- 3. Apply the classifier to the frame
if( !frame.empty() )
{ detectAndDisplay( frame ); }
else
{ printf(" --(!) No captured frame -- Break!"); break; } int c = waitKey();
if( (char)c == 'q' || (char)c == 'Q' || == c) { break; }
}
} cvReleaseCapture(&capture);
return ;
} /** @function detectAndDisplay */
void detectAndDisplay( Mat frame )
{
std::vector<Rect> faces;
std::vector<Rect> palms;
std::vector<Rect> fists;
static vector<vector<Rect>> palms_bank;
static vector<vector<Rect>> fists_bank;
const int MAX_NUM = ;
Mat frame_gray; cvtColor( frame, frame_gray, CV_BGR2GRAY );
equalizeHist( frame_gray, frame_gray ); //-- Palm detection
palm_cascade.detectMultiScale( frame_gray, palms, 1.1, , |CV_HAAR_SCALE_IMAGE, Size(, ) );
palms_bank.push_back(palms);
if(palms_bank.size() > MAX_NUM)
palms_bank.erase(palms_bank.begin()); vector<Rect> palmAll;
RestoreVectors(palms_bank, palmAll);
groupRectangles(palmAll, ); for( size_t j = ; j < palmAll.size(); j++ )
{
rectangle(frame, palmAll[j], Scalar(,,), );
} //-- Fist detection
fist_cascade.detectMultiScale( frame_gray, fists, 1.1, , |CV_HAAR_SCALE_IMAGE, Size(, ) );
fists_bank.push_back(fists);
if(fists_bank.size() > MAX_NUM)
fists_bank.erase(fists_bank.begin()); vector<Rect> fistAll;
RestoreVectors(fists_bank, fistAll);
groupRectangles(fistAll, ); for( size_t j = ; j < fistAll.size(); j++ )
{
rectangle(frame, fistAll[j], Scalar(,,), );
} //-- Show what you got
imshow( window_name, frame );
} void RestoreVectors(vector<vector<Rect>>& vecs_bank, vector<Rect>& vecAll)
{
for(size_t i = ; i < vecs_bank.size(); i++){
vecAll.insert(vecAll.end(), vecs_bank[i].begin(), vecs_bank[i].end());
}
}
参考:
groupRectangles
Groups the object candidate rectangles.
- C++: void groupRectangles(vector<Rect>& rectList, int groupThreshold, double eps=0.2)
- C++: void groupRectangles(vector<Rect>& rectList, vector<int>& weights, intgroupThreshold, double eps=0.2)
- Python: cv2.groupRectangles(rectList, groupThreshold[, eps]) → rectList, weights
-
Parameters: - rectList – Input/output vector of rectangles. Output vector includes retained and grouped rectangles. (The Python list is not modified in place.)
- groupThreshold – Minimum possible number of rectangles minus 1. The threshold is used in a group of rectangles to retain it.
- eps – Relative difference between sides of the rectangles to merge them into a group.
The function is a wrapper for the generic function partition() . It clusters all the input rectangles using the rectangle equivalence criteria that combines rectangles with similar sizes and similar locations. The similarity is defined by eps. When eps=0 , no clustering is done at all. If
, all the rectangles are put in one cluster. Then, the small clusters containing less than or equal to groupThreshold rectangles are rejected. In each other cluster, the average rectangle is computed and put into the output rectangle list.
原文:http://blog.csdn.net/lichengyu/article/details/38544189
利用OpenCV检测手掌(palm)和拳头(fist)的更多相关文章
- 利用OpenCV检测图像中的长方形画布或纸张并提取图像内容
基于知乎上的一个答案.问题如下: 也就是在一张照片里,已知有个长方形的物体,但是经过了透视投影,已经不再是规则的长方形,那么如何提取这个图形里的内容呢?这是个很常见的场景,比如在博物馆里看到一幅很喜欢 ...
- 如何利用OpenCV自带的级联分类器训练程序训练分类器
介绍 使用级联分类器工作包括两个阶段:训练和检测. 检测部分在OpenCVobjdetect 模块的文档中有介绍,在那个文档中给出了一些级联分类器的基本介绍.当前的指南描述了如何训练分类器:准备训练数 ...
- 利用opencv作透明重叠人群密度热度图
在作热度图的时候我们经常需要将热度图调整透明度后叠加在原图上达到更好的展示效果.比如检测人气密度的热度图: (来自sensetime) 一般作图的时候会第一时间想到matplotlib,因为可以很方便 ...
- xss利用和检测平台
xssing 是安全研究者Yaseng发起的一个基于 php+mysql的 网站 xss 利用与检测开源项目,可以对你的产品进行黑盒xss安全测试,可以兼容获取各种浏览器客户端的网站url,cooki ...
- 用 Python 和 OpenCV 检测图片上的条形码
用 Python 和 OpenCV 检测图片上的的条形码 这篇博文的目的是应用计算机视觉和图像处理技术,展示一个条形码检测的基本实现.我所实现的算法本质上基于StackOverflow 上的这个问 ...
- #利用openCV裁脸
#利用openCV裁脸import cv2 def draw_rects(img, rects): for x, y, w, h in rects: cv2.rectangle(img, (x, y) ...
- 利用OpenCV给图像添加中文标注
利用OpenCV给图像添加中文标注 : 参考:http://blog.sina.com.cn/s/blog_6bbd2dd101012dbh.html 和https://blog.csdn.net/ ...
- 用 Python 和 OpenCV 检测图片上的条形码(转载)
原文地址:http://python.jobbole.com/80448/ 假设我们要检测下图中的条形码: # load the image and convert it to grayscale 1 ...
- 利用WMI检测电脑硬件信息,没办法显示cpu的信息
但你要给某些系统或软件加密时,需要了解到服务器的硬件信息时,系统和软件会利用WMI检测硬件信息, 而有时我们会遇到检测不到CPU的型号信息,如图 此时的解决方法: 1.确定“服务”里启动了WMI 2. ...
随机推荐
- win10 打开sql server配置管理器
win10 安装 sql server之后无法在开始菜单找到“sql server 配置管理器(SQL server configuration manager 1)在开始菜单中,无法找到 配置管理器 ...
- Keil下Debug随笔
很多时候我们需要通过硬件仿真来调试程序,在仿真时有时候会遇到这样的情况,那就是选择全速运行时,我们的全局变量无法随时更新,而在那设一个断点后发现值是变化的,那么为什么会出现这种情况呢,那就是可能是我们 ...
- T-SQL判断是否存在表、临时表
利用SQL SERVER的系统函数 object_id() 可以判断是否存在表.临时表, object_id() 的作用是返回架构范围内对象的数据库对象标识.(即返回系统视图 sys.objects ...
- onsrcoll和scrollTop兼容与实现
对于onscroll事件的支持 各浏览器 document.document.body.document.documentElement 对象的 onscroll 事件的支持存在差异. 所谓的支持性存 ...
- Reddit CEO亲自诠释内容审核的无奈
本文由 网易云发布. 导语:继数据泄露危机之后,Facebook将会雇用数千名新员工来负责新的验证系统,这个系统将首先在美国广告客户中生效,并将在未来几个月内涵盖其他国家.与此同时,如何让自己的社区 ...
- 四,mysql优化——sql语句优化之索引二
1,在什么列适合添加索引 (1)较频繁的作为查询条件字段应该添加索引 select * from emp where empid = 2; (2)唯一性太差的字段不适合添加索引,即时频繁作为查询条件. ...
- LINUX中如何查看某个端口是否被占用
之前查询端口是否被占用一直搞不明白,问了好多人,终于搞懂了,现在总结下: 1.netstat -anp |grep 端口号 如下,我以3306为例,netstat -anp |grep ...
- python求100以内素数
python求100以内素数之和 from math import sqrt # 使用isPrime函数 def isPrime(n): if n <= 1: return False for ...
- thinkphp5动态生成二维码实例总结
thinkphp5关于动态生成二维码类库总结: 遇到的最大问题如下:我想大部分人也碰到过,所有觉得有必要总结下: thinkphp5提示找不到Qrcode类,可是自己明明都放置到了,vendor 目录 ...
- 【bzoj3028】 食物 生成函数+隔板法
题目传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=3028 这题的推导很妙啊,裸的推母函数的题. 我们首先构造出每种食物的母函数: 汉堡:$ ...