思路:利用训练好的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());
}
}

参考:

[1] groupRectangles的说明文档

[2] palm.xml和fist.xml的下载地址

[3] 人脸和眼睛检测的opencv示例代码

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)的更多相关文章

  1. 利用OpenCV检测图像中的长方形画布或纸张并提取图像内容

    基于知乎上的一个答案.问题如下: 也就是在一张照片里,已知有个长方形的物体,但是经过了透视投影,已经不再是规则的长方形,那么如何提取这个图形里的内容呢?这是个很常见的场景,比如在博物馆里看到一幅很喜欢 ...

  2. 如何利用OpenCV自带的级联分类器训练程序训练分类器

    介绍 使用级联分类器工作包括两个阶段:训练和检测. 检测部分在OpenCVobjdetect 模块的文档中有介绍,在那个文档中给出了一些级联分类器的基本介绍.当前的指南描述了如何训练分类器:准备训练数 ...

  3. 利用opencv作透明重叠人群密度热度图

    在作热度图的时候我们经常需要将热度图调整透明度后叠加在原图上达到更好的展示效果.比如检测人气密度的热度图: (来自sensetime) 一般作图的时候会第一时间想到matplotlib,因为可以很方便 ...

  4. xss利用和检测平台

    xssing 是安全研究者Yaseng发起的一个基于 php+mysql的 网站 xss 利用与检测开源项目,可以对你的产品进行黑盒xss安全测试,可以兼容获取各种浏览器客户端的网站url,cooki ...

  5. 用 Python 和 OpenCV 检测图片上的条形码

      用 Python 和 OpenCV 检测图片上的的条形码 这篇博文的目的是应用计算机视觉和图像处理技术,展示一个条形码检测的基本实现.我所实现的算法本质上基于StackOverflow 上的这个问 ...

  6. #利用openCV裁脸

    #利用openCV裁脸import cv2 def draw_rects(img, rects): for x, y, w, h in rects: cv2.rectangle(img, (x, y) ...

  7. 利用OpenCV给图像添加中文标注

    利用OpenCV给图像添加中文标注 : 参考:http://blog.sina.com.cn/s/blog_6bbd2dd101012dbh.html  和https://blog.csdn.net/ ...

  8. 用 Python 和 OpenCV 检测图片上的条形码(转载)

    原文地址:http://python.jobbole.com/80448/ 假设我们要检测下图中的条形码: # load the image and convert it to grayscale 1 ...

  9. 利用WMI检测电脑硬件信息,没办法显示cpu的信息

    但你要给某些系统或软件加密时,需要了解到服务器的硬件信息时,系统和软件会利用WMI检测硬件信息, 而有时我们会遇到检测不到CPU的型号信息,如图 此时的解决方法: 1.确定“服务”里启动了WMI 2. ...

随机推荐

  1. [ASP.NET]JQuery直接调用asp.net后台WebMethod方法

    在项目开发碰到此类需求,特此记录下经项目验证的方法总结. 利用JQuery的$.ajax()可以很方便的调用asp.net的后台方法. [WebMethod] 命名空间 1.无参数的方法调用 注意:方 ...

  2. leetcode 两数之和 II - 输入有序数组

    给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数. 函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2. 说明: 返回的下标值 ...

  3. 为什么不能用Abort退出线程

    在使用线程时,如果线程还未结束直接退出线程很有可能会导致数据丢失. class threadAbort { static void Main(string[] args) { WriteMessage ...

  4. Day 2 Python 基础数据类型

    2.os.path.join()函数 语法:  os.path.join(path1[,path2[,......]]) 返回值:将多个路径组合后返回 注:第一个绝对路径之前的参数将被忽略 1 2 3 ...

  5. Java基础学习篇---------继承

    一.覆写(重写) 1.含义:子类的定义方法.属性和父类的定义方法.属性相同时候 方法名称相同,参数相同以及参数的个数也相同,此时为覆写(重写) 扩充知识点: 覆盖:只有属性名字和方法名字相同,类型.个 ...

  6. OpenSL的使用

    #include <jni.h> #include <string> #include <SLES/OpenSLES.h> #include <SLES/Op ...

  7. python 简单搭建阻塞式单进程,多进程,多线程服务

    由于经常被抓取文章内容,在此附上博客文章网址:,偶尔会更新某些出错的数据或文字,建议到我博客地址 :  --> 点击这里 我们可以通过这样子的方式去理解apache的工作原理 1 单进程TCP服 ...

  8. 978. Longest Turbulent Subarray

    A subarray A[i], A[i+1], ..., A[j] of A is said to be turbulent if and only if: For i <= k < j ...

  9. 650. 2 Keys Keyboard

    Initially on a notepad only one character 'A' is present. You can perform two operations on this not ...

  10. hdu5833----高斯消元

    题目大意: 给你n个整数,从中选一些数,他们的乘积为一个完全平方数 问有多少种这样的方式,已知这些数的素因素不超过2000. 思路: 一个完全平方数素因素的个数肯定是偶数个. 我们只要从n个数中选取所 ...