opencv 重用代码块记录
Created on 2013-8-7
URL : http://blog.sina.com.cn/s/blog_a502f1a30101mfj4.html
@author: zhxfl
转载请说明出处
一 从视频中提取帧并且保存图片
#include <iostream>
#include <stdio.h>
#include "opencv2/highgui/highgui.hpp"
using namespace std;
using namespace cv; bool VedioToImage(char *from, char *to)
{
CvCapture *capture = cvCreateFileCapture(from);
if(!capture)
return false;
IplImage *frame = ;
for(int i = , j = ;; j++)
{
frame = cvQueryFrame( capture );
if(frame)
{
if(j % == )
{
char str[];
sprintf(str,"%s/%d.png",to,i);
cvSaveImage(str,frame);
printf("%d.png\n",i++);
}
}
else
{
break;
}
} return true;
} int main()
{
VedioToImage("../fly.mp4","../fly");
return ;
}
从视频中提取帧并且保存成图片
GINC=$(shell pkg-config --cflags opencv)
GLIB=$(shell pkg-config --libs opencv)
all:build
build:a.out
a.o:a.cpp
g++ $(GINC) -c a.cpp
a:a.o
g++ -o a a.o $(GLIB)
clean:
rm *.o
run:build
./a.out
Makefile
二 对一系列图片进行处理,去掉没有检测到人脸的照片
#include <iostream>
#include <iomanip>
#include "opencv2/core/core.hpp"
#include "opencv2/contrib/contrib.hpp"
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc_c.h"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/gpu/gpu.hpp"
#include <stdio.h>
#include <time.h>
using namespace std;
using namespace cv;
using namespace cv::gpu; std::string getWorkspace()
{
#if (defined _WIN32 || defined _WIN64) && defined _MSC_VER
std::string workspace = std::string("E:/Resources");
#else
std::string workspace = std::string("/data/ubuntu/Resources");
#endif
return workspace;
} string path = getWorkspace() + "/haarcascade_frontalface_alt.xml";
CascadeClassifier cascade_cpu;//使用cpu的分类器器
CascadeClassifier cascade_gpu;//使用cpu的分类器器
int detectFaceCpu(std::string file)
{
string input = getWorkspace() + string("/image/") + file;
Mat frame_cpu = cv::imread(input.c_str()); Mat gray_cpu;
//转化成灰度图
if (frame_cpu.channels() == )
{
cvtColor(frame_cpu, gray_cpu, CV_BGR2GRAY );
}
else
{
gray_cpu = frame_cpu;
}
vector<Rect> facesBuf_cpu; cascade_cpu.detectMultiScale(gray_cpu, facesBuf_cpu); for(size_t i = ; i < facesBuf_cpu.size();i++)
{
rectangle(frame_cpu, facesBuf_cpu[i], Scalar());
} if(facesBuf_cpu.size())
{
string path = getWorkspace() + string("/image_output/") + file;
IplImage tmp (frame_cpu);
cvSaveImage(path.c_str(), &tmp);
}
return true;
} int detectFaceGpu(std::string file)
{
string input = getWorkspace() + string("/image/") + file;
Mat frame_gpu = cv::imread(input.c_str());
Mat gray_gpu;
//转化成灰度图
if (frame_gpu.channels() == )
{
cvtColor(frame_gpu, gray_gpu, CV_BGR2GRAY );
}
else
{
gray_gpu = frame_gpu;
}
vector<Rect> facesBuf_gpu; cascade_gpu.detectMultiScale(gray_gpu, facesBuf_gpu); for(size_t i = ; i < facesBuf_gpu.size();i++)
{
rectangle(frame_gpu, facesBuf_gpu[i], Scalar());
} if(facesBuf_gpu.size())
{
string path = getWorkspace() + string("/image_output/") + file;
IplImage tmp (frame_gpu);
cvSaveImage(path.c_str(), &tmp);
}
return true;
} int main(int argc, unsigned char* argv[])
{
if (!cascade_cpu.load(path))
{
printf("error cpu\n");
return ;
} if (!cascade_gpu.load(path))
{
printf("errror gpu\n");
return ;
} for(int i = ; i < ;i++)
{
char str[];
sprintf(str, "%03d.tif", i);
float start = clock();
detectFaceGpu(string(str));
float end = clock();
printf("%03d.tif time = %f\n",i ,(end - start) / );
}
return ;
}
三 使用摄像头录制视频
#include <iostream>
#include <iomanip>
#include "opencv2/core/core.hpp"
#include "opencv2/contrib/contrib.hpp"
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc_c.h"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/gpu/gpu.hpp"
#include <stdio.h>
#include <time.h>
using namespace std;
using namespace cv;
using namespace cv::gpu; std::string getWorkspace()
{
#if (defined _WIN32 || defined _WIN64) && defined _MSC_VER
std::string workspace = std::string("E:/Resources");
#else
std::string workspace = std::string("/data/ubuntu/Resources");
#endif
return workspace;
} int main(int argc, unsigned char* argv[])
{
for(int k = ; k < ; k++)
{
char str[];
sprintf(str, "/%d.avi",k);
string path = getWorkspace() + string(str);
CvVideoWriter * writer = cvCreateVideoWriter(path.c_str(),CV_FOURCC('X','V','I','D'),,cvSize(,)); // 需要安装xvid编码器 CvCapture* capture1 = cvCaptureFromCAM();
int w = , h = ;
cvSetCaptureProperty ( capture1, CV_CAP_PROP_FRAME_WIDTH, w );
cvSetCaptureProperty ( capture1, CV_CAP_PROP_FRAME_HEIGHT, h );
cvNamedWindow( "Camera_1", CV_WINDOW_AUTOSIZE );
IplImage* frame1;
int n = ;
int i =;
while()
{
frame1 = cvQueryFrame( capture1 );
if( !frame1 ) break;
{
cvShowImage( "Camera_1", frame1 );
cvWriteFrame(writer,frame1); //写入文件
}
int key = cvWaitKey();
if( key == ) break;
if(i >= )break;
else i++;
printf("%d\n",i);
}
cvReleaseVideoWriter(&writer);
cvReleaseCapture( &capture1 );
cvDestroyWindow( "Camera_1" );
}
return ;
}
四 将视频流分成两部分,一部分是能够圈到人脸,一部分不能。
#include <iostream>
#include <iomanip>
#include "opencv2/core/core.hpp"
#include "opencv2/contrib/contrib.hpp"
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc_c.h"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/gpu/gpu.hpp"
#include <stdio.h>
#include <time.h>
using namespace std;
using namespace cv;
using namespace cv::gpu; std::string getWorkspace()
{
#if (defined _WIN32 || defined _WIN64) && defined _MSC_VER
std::string workspace = std::string("E:/Resources");
#else
std::string workspace = std::string("/data/hadoop/Resources");
#endif
return workspace;
} CascadeClassifier cascade_cpu;//使用cpu的分类器器
CascadeClassifier_GPU cascade_gpu;//使用cpu的分类器器 bool detectFaceCpu(Mat &frame_cpu)
{
Mat gray_cpu;
//转化成灰度图
if (frame_cpu.channels() == )
{
cvtColor(frame_cpu, gray_cpu, CV_BGR2GRAY );
}
else
{
gray_cpu = frame_cpu;
}
vector<Rect> facesBuf_cpu; cascade_cpu.detectMultiScale(gray_cpu, facesBuf_cpu, 1.2, ); for(size_t i = ; i < facesBuf_cpu.size();i++)
{
rectangle(frame_cpu, facesBuf_cpu[i], Scalar());
}
if(facesBuf_cpu.size())
{
return true;
}
else
{
return false;
}
} int detectFaceGpu(Mat &frame)
{
GpuMat frame_gpu(frame);
GpuMat gray_gpu;
//转化成灰度图
if (frame_gpu.channels() == )
{
cvtColor(frame_gpu, gray_gpu, CV_BGR2GRAY );
}
else
{
gray_gpu = frame_gpu;
}
GpuMat facesBuf_gpu;
Mat faces_downloaded; cascade_gpu.findLargestObject = true;
int detections_num =
cascade_gpu.detectMultiScale(gray_gpu, facesBuf_gpu, 1.2, ); facesBuf_gpu.colRange(, detections_num).download(faces_downloaded); for (int i = ; i < detections_num; ++i)
{
rectangle(frame, faces_downloaded.ptr<cv::Rect>()[i], Scalar());
} if(detections_num)
{
return true;
}
else
{
return false;
}
} void decompound_cpu(string in,string out_1, string out_2)
{
//摄像头对象
CvCapture *capture = cvCreateFileCapture(in.c_str());
int w = , h = ;
cvSetCaptureProperty ( capture, CV_CAP_PROP_FRAME_WIDTH, w );
cvSetCaptureProperty ( capture, CV_CAP_PROP_FRAME_HEIGHT, h ); CvVideoWriter * writer1 =
cvCreateVideoWriter(out_1.c_str(),CV_FOURCC('X','V','I','D'),,cvSize(w,h));
CvVideoWriter * writer2 =
cvCreateVideoWriter(out_2.c_str(),CV_FOURCC('X','V','I','D'),,cvSize(w,h));
IplImage* frame;
cvNamedWindow("Camera_1", CV_WINDOW_AUTOSIZE );
cvNamedWindow("Camera_2", CV_WINDOW_AUTOSIZE );
static int i = ;
while()
{
frame = cvQueryFrame(capture);
Mat mat(frame);
if(mat.empty())
{
printf("finish !!!\n");
break;
} if(detectFaceGpu(mat))
{
cvShowImage("Camera_1", frame);
printf("y = %d\n", i++);
cvWriteFrame(writer1, frame); //写入文件
}
else
{
printf("n = %d\n", i++);
cvShowImage( "Camera_2", frame);
cvWriteFrame(writer2, frame); //写入文件
} int key = cvWaitKey();
if( key == ) break;
}
cvReleaseVideoWriter(&writer1);
cvReleaseVideoWriter(&writer2);
cvReleaseCapture(&capture);
cvDestroyWindow("Camera_1");
cvDestroyWindow("Camera_2");
} bool init()
{
string path = getWorkspace() + "/haarcascade_frontalface_alt.xml";
if (!cascade_cpu.load(path))
{
printf("error cascade_cpu load\n");
return ;
} if (!cascade_gpu.load(path))
{
printf("errror cascade_gpu load\n");
return ;
}
return true;
} int main(int argc, char* argv[])
{
init();
string x1 = getWorkspace() + string("/4.avi");
string x2 = getWorkspace() + string("/image_output/4_1.avi");
string x3 = getWorkspace() + string("/image_output/4_2.avi");
decompound_cpu(x1,x2,x3);
return ;
}
五从视频流中提取出某个人的视频来
#include <iostream>
#include <iomanip>
#include "opencv2/core/core.hpp"
#include "opencv2/contrib/contrib.hpp"
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc_c.h"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/gpu/gpu.hpp"
#include <stdio.h>
#include <time.h>
#include "personName.h"
#include "util.h" using namespace std;
using namespace cv;
using namespace cv::gpu; CascadeClassifier cascade_cpu;//使用cpu的分类器器
CascadeClassifier_GPU cascade_gpu;//使用cpu的分类器器
int predict_id = -;
float s_confidence = ;
IplImage * getFace(IplImage *img, CvRect rect)
{
static int id = ;
cvSetImageROI(img,rect);
IplImage *tmp;
tmp=cvCreateImage(cvSize(,),IPL_DEPTH_8U,);
cvResize(img,tmp,CV_INTER_LINEAR);
cvResetImageROI(img);
return tmp;
} int predict(Mat &mat)
{
static bool init = true;
static Ptr<FaceRecognizer> model;
if(init)
{
model = createEigenFaceRecognizer();
string str = getWorkspace() + string("/face.xml");
model->load(str.c_str());
init = false;
} int predictedLabel = -;
double confidence = 0.0; model->predict(mat, predictedLabel, confidence); string result_message = format("%10s confidence = %lf", PersonName::build()->getName(predictedLabel).c_str(), confidence);
printf("%s\n",result_message.c_str());
if(confidence < s_confidence) return predictedLabel;
else return -;
} bool detectFaceCpu(Mat &frame_cpu)
{
Mat gray_cpu;
//转化成灰度图
if (frame_cpu.channels() == )
{
cvtColor(frame_cpu, gray_cpu, CV_BGR2GRAY );
}
else
{
gray_cpu = frame_cpu;
}
vector<Rect> facesBuf_cpu; cascade_cpu.detectMultiScale(gray_cpu, facesBuf_cpu, 1.2, ); IplImage img(gray_cpu);
for(size_t i = ; i < facesBuf_cpu.size();i++)
{
rectangle(frame_cpu, facesBuf_cpu[i], Scalar());
Mat tmp(getFace(&img, facesBuf_cpu[i]));
int id = predict(tmp);
if(id != predict_id)continue;
return true;
}
return false;
} int detectFaceGpu(Mat &frame)
{
GpuMat frame_gpu(frame);
GpuMat gray_gpu;
Mat gray_cpu;
//转化成灰度图
if (frame_gpu.channels() == )
{
cvtColor(frame_gpu, gray_gpu, CV_BGR2GRAY);
cvtColor(frame, gray_cpu, CV_BGR2GRAY);
}
else
{
gray_gpu = frame_gpu;
gray_cpu = frame;
}
GpuMat facesBuf_gpu;
Mat faces_downloaded; cascade_gpu.findLargestObject = true;
int detections_num =
cascade_gpu.detectMultiScale(gray_gpu, facesBuf_gpu, 1.2, ); facesBuf_gpu.colRange(, detections_num).download(faces_downloaded); IplImage img(gray_cpu);
for (int i = ; i < detections_num; ++i)
{
rectangle(frame, faces_downloaded.ptr<cv::Rect>()[i], Scalar());
Mat tmp(getFace(&img, faces_downloaded.ptr<cv::Rect>()[i]));
int id = predict(tmp);
if(id != predict_id)continue;
return true;
}
return false;
} void decompound_cpu(string in,string out_1, string out_2)
{
//摄像头对象
CvCapture *capture = cvCreateFileCapture(in.c_str());
int w = , h = ;
cvSetCaptureProperty ( capture, CV_CAP_PROP_FRAME_WIDTH, w );
cvSetCaptureProperty ( capture, CV_CAP_PROP_FRAME_HEIGHT, h ); CvVideoWriter * writer1 =
cvCreateVideoWriter(out_1.c_str(),CV_FOURCC('X','V','I','D'),,cvSize(w,h));
CvVideoWriter * writer2 =
cvCreateVideoWriter(out_2.c_str(),CV_FOURCC('X','V','I','D'),,cvSize(w,h));
IplImage* frame;
cvNamedWindow("Camera_1", CV_WINDOW_AUTOSIZE );
cvNamedWindow("Camera_2", CV_WINDOW_AUTOSIZE );
static int i = ;
while()
{
frame = cvQueryFrame(capture);
Mat mat(frame);
if(mat.empty())
{
printf("finish !!!\n");
break;
} if(detectFaceGpu(mat))
{
cvShowImage("Camera_1", frame);
printf("y = %d\n", i++);
cvWriteFrame(writer1, frame); //写入文件
}
else
{
printf("n = %d\n", i++);
cvShowImage( "Camera_2", frame);
cvWriteFrame(writer2, frame); //写入文件
} int key = cvWaitKey();
if( key == ) break;
}
cvReleaseVideoWriter(&writer1);
cvReleaseVideoWriter(&writer2);
cvReleaseCapture(&capture);
cvDestroyWindow("Camera_1");
cvDestroyWindow("Camera_2");
} bool init()
{
predict_id = ;
s_confidence = ;
string path = getWorkspace() + "/haarcascade_frontalface_alt.xml";
if (!cascade_cpu.load(path))
{
printf("error cascade_cpu load\n");
return ;
} if (!cascade_gpu.load(path))
{
printf("errror cascade_gpu load\n");
return ;
}
return true;
} int main(int argc, char* argv[])
{
init();
string x1 = getWorkspace() + string("/17.avi");
string x2 = getWorkspace() + string("/image_output/17_1.avi");
string x3 = getWorkspace() + string("/image_output/17_2.avi");
decompound_cpu(x1,x2,x3);
return ;
}
opencv 重用代码块记录的更多相关文章
- python功能代码块记录
python Autopep8——按PEP8风格自动排版Python代码(参考链接) autopep8 --in-place --aggressive --aggressive test_autope ...
- 记录我的 python 学习历程-Day06 is id == / 代码块 / 集合 / 深浅拷贝
一.is == id 用法 在Python中,id是内存地址, 你只要创建一个数据(对象)那么就会在内存中开辟一个空间,将这个数据临时加载到内存中,这个空间有一个唯一标识,就好比是身份证号,标识这个空 ...
- python代码块,小数据池,驻留机制深入剖析
一,什么是代码块. 根据官网提示我们可以获知: 根据提示我们从官方文档找到了这样的说法: A Python program is constructed from code blocks. A blo ...
- Python小数据池,代码块
今日内容一些小的干货 一. id is == 二. 代码块 三. 小数据池 四. 总结 python小数据池,代码块的最详细.深入剖析 一. id is == 二. 代码块 三. 小数据池 四. ...
- python小数据池,代码块知识
一.什么是代码块? 根据官网提示我们可以获知: A Python program is constructed from code blocks. A block is a piece of Pyth ...
- Python基础学习Day6 is id == 区别,代码块,小数据池 ---->>编码
一.代码块 Python程序是由代码块构造的.块是一个python程序的文本,他是作为一个单元执行的. 代码块:一个模块,一个函数,一个类,一个文件等都是一个代码块. 而作为交互方式输入的每个命令都是 ...
- python小数据池,代码块的最详细、深入剖析
代码块: Python程序是由代码块构造的.块是 一个python程序的文本,他是作为一个单元执行的. 代码块:一个模块,一个函数,一个类,一个文件等都是一个代码块. 而作为交互方式输入的每个命令都是 ...
- 五.python小数据池,代码块的最详细、深入剖析
一,id,is,== 在Python中,id是什么?id是内存地址,那就有人问了,什么是内存地址呢? 你只要创建一个数据(对象)那么都会在内存中开辟一个空间,将这个数据临时加在到内存中,那么这个空间是 ...
- python 小数据池,代码块, is == 深入剖析
python小数据池,代码块的最详细.深入剖析 一. id is == 二. 代码块 三. 小数据池 四. 总结 一,id,is,== 在Python中,id是什么?id是内存地址,那就有人问了, ...
随机推荐
- [翻译][MVC 5 + EF 6] 3:排序、过滤、分页
原文:Sorting, Filtering, and Paging with the Entity Framework in an ASP.NET MVC Application 1.添加排序: 1. ...
- Python中 if __name__ == '__main__': 详解
一个python文件就可以看作是一个python的模块,这个python模块(.py文件)有两种使用方式:直接运行和作为模块被其他模块调用. __name__:每一个模块都有一个内置属性__name_ ...
- 思维导图软件VYM
http://www.insilmaril.de/vym/ 点击打开链接http://www.oschina.net/p/vym 有人说VYM就跟目录是一回事,确实是这样, 只不过与excel之类的比 ...
- STM32之系统滴答定时器
一.SysTick(系统滴答定时器)概述 操作系统需要一个滴答定时器周期性产生中断,以产生系统运行的节拍.在中断服务程序里,基于优先级调度的操作系统会根据进程优先级切换任务,基于时间片轮转系统会根据时 ...
- MDK常用快捷键
一.常用编译相关的快捷键 1.编译(单个文件) Ctrl+F7 2.连接 F7 二.常用调试相关的快捷键 1.运行/停止 Ctrl+F5 2.Run(全速运行) F5 3.Stop Deb ...
- MINA源码阅读之ACP
Processor在XXAcceptor以及XXConnector中所扮演的只能就是:作为Acceptor以及Connetor所创建的Session的Processor: IoAcceptor作为他所 ...
- vs2010 使用SignalR 提高B2C商城用户体验(三)
vs2010 使用SignalR 提高B2C商城用户体验(三) 上一章节,我们的web即时通讯已经可以实现跨域了,但针对我们的需求,还希望,一些客户端程序可以和我们的web用户,在线聊天,所以到Sig ...
- python之--条件判断和循环
Python之判断 和其他语言一样,python同样具有条件判断和循环的操作,比如我们可以编写一个简单的判断操作:使用if关键字可以达到判断的效果,如下例: >>> test_if ...
- 用 NSURProtocol 注入测试数据
在之前的几篇博文中,笔者介绍过访问异步网络的单元测试方法及如何使用模拟对象来进一步控制单元测试的范围.在今天的教程中,笔者将展示另一种方法,即:通过自定义 NSURProtocol 类来获取静态测试数 ...
- CISCO的HTTP/HTTPS/SSH配置测试完成
按实验一步一步,倒是很容易的,也理解罗~~ START-CONFIG粗配置文件如下: r1#show run Building configuration... Current configurati ...