利用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. ...
随机推荐
- android应用搬家的实现
android手机上的应用搬家功能,具体的介绍和原理参考: 系统目录及应用搬家的研究 应用搬家的实现 这里主要是作为一个补充,因为上面两篇文章虽然讲的挺好的,但是给出的例子不能直接运行,还是需要一些准 ...
- Alwayson--问题总结二
1. 备份首选项作用 答:备份首选项并不影响实际的备份操作,只是在备份前提供标示当前副本是否是推荐的备份副本.管理员可以忽略备份首选项在任意副本上执行完整备份和日志备份. 2. 在辅助副本和主副本备份 ...
- 记一次IIS站点出错的解决过程
记一次IIS站点出错的解决过程 以前一直都是人家用着系统出问题了反馈过来这边改,没想到这回就发生在自己使用的过程中 问题经过 我正在执行一个操作,保存了没有返回提示,打开浏览器控制台查看网络,请求返回 ...
- SQL分组合并
STUFF ( character_expression , start , length ,character_expression ) select TcodMedInst_GUID,stuff( ...
- QuartzNet3.0实现作业调度
Quartz是一个完全由JAVA编写的开源作业调度框架. Quartz.NET是Quartz的.NET移植,它用C#写成,可用于.Net以及.Net Core的应用中. 目前最新的quartz.net ...
- C# 生产成条形码3种方法
首先效果: 1:首先下载BarcodeLib.dll 下载地址 http://pan.baidu.com/share/link?shareid=2590968386&uk=2148890391 ...
- 2D Convex Hulls and Extreme Points( Convex Hull Algorithms) CGAL 4.13 -User Manual
1 Introduction A subset S⊆R2 is convex if for any two points p and q in the set the line segment wit ...
- c# 求第30位数的值
1,1,2,3,5,8,13,21,34,55.... 求第30位数的值: 递归方法: class Program { static void Main(string[] args) { //找规律: ...
- linux的hostname(主机名)修改详解
Linux操作系统的hostname是一个kernel变量,可以通过hostname命令来查看本机的hostname.也可以直接cat /proc/sys/kernel/hostname查看. #ho ...
- JVM内存参数( -Xms -Xmx -Xmn -Xss 直接内存)
JVM调优总结 -Xms -Xmx -Xmn -Xss jvm 内存 在不同的情况下如何增大 及 PermGen space 相关 JVM日志和参数的理解 JVM崩溃Log日志分析 -Xms 为jvm ...