opencv 通过摄像头捕捉头部
code:
#include <opencv\cv.h>
#include <opencv\highgui.h>
#include <opencv\cxcore.h>
#include <stdio.h> #include <fstream>
#include <string>
#include <opencv\ml.h>
#include <iostream>
#include <fstream>
#include <string>
#include <vector> #include <iostream>
#include <string.h>
#include <ctype.h> using namespace cv;
using namespace std; void detectAndDraw( Mat& img,
CascadeClassifier& cascade, CascadeClassifier& nestedCascade,
double scale);
String cascadeName =
"D:\\Opencv\\opencv\\data\\haarcascades\\haarcascade_frontalface_alt.xml";
String nestedCascadeName =
"D:\\Opencv\\opencv\\data\\haarcascades\\haarcascade_eye_tree_eyeglasses.xml"; int main()
{ IplImage* aplace = 0;
IplImage* colorlaplace = 0; IplImage* planes[3] = {0,0,0};
CvCapture* capture = 0;
Mat frame, frameCopy, image;
double scale = 1;
CascadeClassifier cascade, nestedCascade;
capture = cvCaptureFromCAM(0); if(!cascade.load(cascadeName))
{
cout<<"Load 1 is false!"<<endl;
}
//if(!nestedCascade.load(nestedCascadeName))
//{
// cout<<"Load 2 is false!"<<endl;
//} cvNamedWindow("Example",0);
for(;;)
{
IplImage* frame0 = 0;
int i; frame0 = cvQueryFrame(capture);
frame = frame0;
if(!frame0)
{
break;
}
if( frame0->origin == IPL_ORIGIN_TL )
frame.copyTo( frameCopy );
else
flip( frame, frameCopy, 0 ); detectAndDraw( frameCopy, cascade, nestedCascade, scale );
//cvShowImage("Example",frame);
cvWaitKey(1);
}
cvReleaseCapture(&capture);
cvDestroyWindow("Example");
return 0;
}
void detectAndDraw( Mat& img,
CascadeClassifier& cascade, CascadeClassifier& nestedCascade,
double scale)
{
int i = 0;
double t = 0;
vector<Rect> faces;
const static Scalar colors[] = { CV_RGB(0,0,255),
CV_RGB(0,128,255),
CV_RGB(0,255,255),
CV_RGB(0,255,0),
CV_RGB(255,128,0),
CV_RGB(255,255,0),
CV_RGB(255,0,0),
CV_RGB(255,0,255)} ;
Mat gray, smallImg( cvRound (img.rows/scale), cvRound(img.cols/scale), CV_8UC1 );
cvtColor( img, gray, CV_BGR2GRAY );
resize( gray, smallImg, smallImg.size(), 0, 0, INTER_LINEAR );
equalizeHist( smallImg, smallImg );
t = (double)cvGetTickCount();
cascade.detectMultiScale( smallImg, faces,
1.1, 2, 0
//|CV_HAAR_FIND_BIGGEST_OBJECT
//|CV_HAAR_DO_ROUGH_SEARCH
|CV_HAAR_SCALE_IMAGE
,
Size(30, 30) );
t = (double)cvGetTickCount() - t;
printf( "detection time = %g ms\n", t/((double)cvGetTickFrequency()*1000.) );
for( vector<Rect>::const_iterator r = faces.begin(); r != faces.end(); r++, i++ )
{
Mat smallImgROI;
vector<Rect> nestedObjects;
Point center;
// Scalar color = colors[i%8];
Scalar color = colors[3];
int radius;
center.x = cvRound((r->x + r->width*0.5)*scale);
center.y = cvRound((r->y + r->height*0.5)*scale);
radius = cvRound((r->width + r->height)*0.25*scale);
circle( img, center, radius, color, 3, 8, 0 );
if( nestedCascade.empty() )
continue;
smallImgROI = smallImg(*r);
nestedCascade.detectMultiScale( smallImgROI, nestedObjects,
1.1, 2, 0
//|CV_HAAR_FIND_BIGGEST_OBJECT
//|CV_HAAR_DO_ROUGH_SEARCH
//|CV_HAAR_DO_CANNY_PRUNING
|CV_HAAR_SCALE_IMAGE
,
Size(30, 30) );
for( vector<Rect>::const_iterator nr = nestedObjects.begin(); nr != nestedObjects.end(); nr++ )
{
center.x = cvRound((r->x + nr->x + nr->width*0.5)*scale);
center.y = cvRound((r->y + nr->y + nr->height*0.5)*scale);
radius = cvRound((nr->width + nr->height)*0.25*scale);
circle( img, center, radius, color, 3, 8, 0 );
}
}
cv::imshow( "Example", img );
}
opencv 通过摄像头捕捉头部的更多相关文章
- 使用python-openCV对摄像头捕捉的镜头进行二值化并打上文字
用CaptureFromCAM函数对图像进行提取: capture = cv.CaptureFromCAM(0) 读取直接的视频文件只需将语句改变为: capture = cv.VideoCaptur ...
- opencvbase 实现opencv打开摄像头和初步处理等效果操作(附源码)
// TwoCameraOnTimer2Dlg.cpp : 实现文件 /* CvMat, Mat, IplImage之间的互相转换 IpIImage -> CvMat CvMat mathead ...
- opencv —— 调用摄像头采集图像 VideoCapture capture(0);
如果要调用摄像头进行视频采集,将代码 VideoCapture capture("C:/Users/齐明洋/Desktop/1.mp4"); 中的 "C:/Users/齐 ...
- 基于opencv网络摄像头在ubuntu下的视频获取
基于opencv网络摄像头在ubuntu下的视频获取 1 工具 原料 平台 :UBUNTU12.04 安装库 Opencv-2.3 2 安装编译运行步骤 安装编译opencv-2.3 参 ...
- Opencv——将摄像头拍摄写成视频文件
这里主要利用了Opencv打开摄像头的代码,以及写入视频的函数,只是这里要注意的是摄像头好像没有帧率,在cvCreateVideoWriter,时要自己设置 #include"cv.h&qu ...
- OpenCv调用摄像头拍照代码
近期在研究OpenCv对摄像头的调用.现将代码贴出,供大家批评指正. 1.申明 #include"./opencv2/opencv.hpp" #ifdef _DEBUG #prag ...
- 基于opencv在摄像头ubuntu根据视频获取
基于opencv在摄像头ubuntu根据视频获取 1 工具 原料 平台 :UBUNTU12.04 安装库 Opencv-2.3 2 安装编译执行步骤 安装编译opencv-2.3 參考h ...
- opencv读取摄像头实时流代码
opencv读取摄像头实时流代码: #include <opencv2/opencv.hpp> #include <iostream> using namespace cv; ...
- openCV 调用摄像头
OpenCV调用摄像头 环境 python:python3.6 摄像头:网络摄像头 Python库:openCV # -*- coding: utf-8 -*- # @author leone # @ ...
随机推荐
- SaaS系列介绍之二: SaaS介绍
1 引言 横看成岭侧成峰,远近高低各不同. 不识庐山真面目, 只缘身在此山中. ________苏轼, ...
- if语句写在while语句外面效率更高
为了排除某些特殊的文件后缀名,一开始我自然而然的这样写,判断每一个文件的后缀名: // 去除后缀名 foreach (const QString &strKey, local_map.keys ...
- 落叶枫桥LOGO
LOGO
- POJ2109——Power of Cryptography
Power of Cryptography DescriptionCurrent work in cryptography involves (among other things) large pr ...
- 部分常见ORACLE面试题以及SQL注意事项
部分常见ORACLE面试题以及SQL注意事项 一.表的创建: 一个通过单列外键联系起父表和子表的简单例子如下: CREATE TABLE parent(id INT NOT NULL, PRIMARY ...
- CentOS5.5上安装git
1.尝试用yum安装git失败 [root@localhost usr]# yum install gitLoaded plugins: fastestmirrorLoading mirror spe ...
- 浏览器以外的Javascript
浏览器外要运行javascript的代码,同样需要这个东西. ie老版本的JScript,ie9以后的Chakra,mozilla的SpiderMonkey,chrome的v8,Safari的Nitr ...
- Self-Paced Training (1) - Introduction to Docker
helloworld: wget -qo- https://get.docker.com/ | sh sudo docker run hello-world sudo usermod -aG dock ...
- I.MX6 gpio-keys driver hacking
/**************************************************************************** * I.MX6 gpio-keys driv ...
- Android 自动换行流式布局的RadioGroup
效果图 用法 使用FlowRadioGroup代替RadioGroup 代码 import android.content.Context; import android.util.Attribute ...