从视频中抓拍图片
2015-10-08 09:57
375 查看
#include "opencv2/highgui/highgui.hpp"
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <vector>
#include <stdio.h>
using namespace cv;
using namespace std;
int process(VideoCapture& capture)
{
int n = 0;
char filename[200];
string window_name = "video | q or esc to quit";
//窗口名称
cout << " \n\t按下【Space】空格键可以截图,图片将存放在工程目录下。\n\n\t【Esc】和【q】键 - 退出程序。 " << endl;
namedWindow(window_name, CV_WINDOW_KEEPRATIO); // CV_WINDOW_KEEPRATIO:保持图像的比例
Mat frame;
for (;;)
{
capture >> frame;
if (frame.empty())
break;
Mat gray;
cvtColor(frame,gray,COLOR_RGB2GRAY);
//彩色转化为灰度图
vector<string> codes;
Mat corners;
findDataMatrix(gray, codes, corners);
drawDataMatrixCodes(frame, codes, corners);
imshow(window_name, frame);
char key = (char) waitKey(1); //delay N millis, usually long enough to display and capture input
switch (key)
{
case 'q':
case 'Q':
case 27: //escape key
return 0;
case ' ': //Save an image
sprintf(filename, "视频截图%.3d.jpg", n++);
//格式化字符串
imwrite(filename, frame);
//保存图片
cout << "\n\t>保存了 " << filename <<"文件到工程目录下"<< endl;
break;
default:
break;
}
}
return 0;
}
int main( )
{
//VideoCapture capture("E:\\图片\\bike.avi");
//从本地载入视频
VideoCapture capture(0);//从摄像头载入视频
if (!capture.isOpened())
{
cerr << "Failed to open a video device or video file!\n" << endl;
return 1;
}
return process(capture);
}
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <vector>
#include <stdio.h>
using namespace cv;
using namespace std;
int process(VideoCapture& capture)
{
int n = 0;
char filename[200];
string window_name = "video | q or esc to quit";
//窗口名称
cout << " \n\t按下【Space】空格键可以截图,图片将存放在工程目录下。\n\n\t【Esc】和【q】键 - 退出程序。 " << endl;
namedWindow(window_name, CV_WINDOW_KEEPRATIO); // CV_WINDOW_KEEPRATIO:保持图像的比例
Mat frame;
for (;;)
{
capture >> frame;
if (frame.empty())
break;
Mat gray;
cvtColor(frame,gray,COLOR_RGB2GRAY);
//彩色转化为灰度图
vector<string> codes;
Mat corners;
findDataMatrix(gray, codes, corners);
drawDataMatrixCodes(frame, codes, corners);
imshow(window_name, frame);
char key = (char) waitKey(1); //delay N millis, usually long enough to display and capture input
switch (key)
{
case 'q':
case 'Q':
case 27: //escape key
return 0;
case ' ': //Save an image
sprintf(filename, "视频截图%.3d.jpg", n++);
//格式化字符串
imwrite(filename, frame);
//保存图片
cout << "\n\t>保存了 " << filename <<"文件到工程目录下"<< endl;
break;
default:
break;
}
}
return 0;
}
int main( )
{
//VideoCapture capture("E:\\图片\\bike.avi");
//从本地载入视频
VideoCapture capture(0);//从摄像头载入视频
if (!capture.isOpened())
{
cerr << "Failed to open a video device or video file!\n" << endl;
return 1;
}
return process(capture);
}
相关文章推荐
- mysql创建定时任务
- [RFID]IC卡克隆(三)Proxmark3测试存在默认密码的IC卡
- 懒汉式单例模式的多线程问题
- 自动将字符串切分(Split功能)
- 插入排序(顺序数组中找要插入元素的位置)
- 将带图片的Java工程用eclipse打包成jar,然后用exe4j将jar打包成exe,并附带jre库
- Web.xml详解
- LeetCode 51 N-Queens
- ZOJ 3471 Most Powerful (状压DP,经典)
- XAMPP+Mantis安装步骤
- jdk1.7 windows
- ATL opengl
- Binary Search Tree Iterator 解答
- Dynamics CRM Import Solution Attribute Display Name description is null or empty
- DG相关查询
- cocos 游戏移植到 Windows10 之路 -- 环境搭建
- 实用的java代码片段
- Dynamics CRM Import Solution Attribute Display Name description is null or empty
- Dynamics CRM Import Solution Attribute Display Name description is null or empty
- Dynamics CRM Import Solution Attribute Display Name description is null or empty