代码出处,opencv2 cookbook:

/*------------------------------------------------------------------------------------------*\
This file contains material supporting chapter 10 of the cookbook:
Computer Vision Programming using the OpenCV Library.
by Robert Laganiere, Packt Publishing, 2011. This program is free software; permission is hereby granted to use, copy, modify,
and distribute this source code, or portions thereof, for any purpose, without fee,
subject to the restriction that the copyright notice may not be removed
or altered from any source or altered source distribution.
The software is released on an as-is basis and without any warranties of any kind.
In particular, the software is not guaranteed to be fault-tolerant or free from failure.
The author disclaims all warranties with regard to this software, any use,
and any consequent failure, is purely the responsibility of the user. Copyright (C) 2010-2011 Robert Laganiere, www.laganiere.name
\*------------------------------------------------------------------------------------------*/ #if !defined VPROCESSOR
#define VPROCESSOR #include <iostream>
#include <iomanip>
#include <sstream>
#include <string>
#include <vector>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp> #include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp> #pragma comment(lib,"opencv_core2410d.lib")
#pragma comment(lib,"opencv_highgui2410d.lib")
#pragma comment(lib,"opencv_imgproc2410d.lib") // The frame processor interface
class FrameProcessor { public:
// processing method
virtual void process(cv:: Mat &input, cv:: Mat &output)= 0;
}; class VideoProcessor { private: // the OpenCV video capture object
cv::VideoCapture capture;
// the callback function to be called
// for the processing of each frame
void (*process)(cv::Mat&, cv::Mat&);
// the pointer to the class implementing
// the FrameProcessor interface
FrameProcessor *frameProcessor;
// a bool to determine if the
// process callback will be called .是否使用回调函数的bool变量
bool callIt;
// Input display window name
std::string windowNameInput;
// Output display window name
std::string windowNameOutput;
// delay between each frame processing
int delay;
// number of processed frames
long fnumber;
// stop at this frame number
long frameToStop;
// to stop the processing
bool stop; // vector of image filename to be used as input
std::vector<std::string> images;
// image vector iterator
std::vector<std::string>::const_iterator itImg; // the OpenCV video writer object
cv::VideoWriter writer;
// output filename
std::string outputFile; // current index for output images
int currentIndex;
// number of digits in output image filename
int digits;
// extension of output images
std::string extension; // to get the next frame
// could be: video file; camera; vector of images
bool readNextFrame(cv::Mat& frame)
{ if (images.size()==0)
return capture.read(frame);
else { if (itImg != images.end())
{ frame= cv::imread(*itImg);
itImg++;
return frame.data != 0;
}
}
} // to write the output frame
// could be: video file or images
void writeNextFrame(cv::Mat& frame)
{ if (extension.length()) { // then we write images std::stringstream ss;
ss << outputFile << std::setfill('0') << std::setw(digits) << currentIndex++ << extension;
cv::imwrite(ss.str(),frame); }
else
{ // then write video file writer.write(frame);
}
} public: // Constructor setting the default values
VideoProcessor() : callIt(false), delay(-1),
fnumber(0), stop(false), digits(0), frameToStop(-1),
process(0), frameProcessor(0) {} // set the name of the video file
bool setInput(std::string filename) { fnumber= 0;
// In case a resource was already
// associated with the VideoCapture instance
capture.release();
images.clear(); // Open the video file
return capture.open(filename);
} // set the camera ID
bool setInput(int id)
{ fnumber= 0;
// In case a resource was already
// associated with the VideoCapture instance
capture.release();
images.clear(); // Open the video file
return capture.open(id);
} // set the vector of input images
bool setInput(const std::vector<std::string>& imgs)
{ fnumber= 0;
// In case a resource was already
// associated with the VideoCapture instance
capture.release(); // the input will be this vector of images
images= imgs;
itImg= images.begin(); return true;
} // set the output video file
// by default the same parameters than input video will be used
bool setOutput(const std::string &filename, int codec=0, double framerate=0.0, bool isColor=true)
{ outputFile= filename;
extension.clear(); if (framerate==0.0)
framerate= getFrameRate(); // same as input char c[4];
// use same codec as input
if (codec==0) {
codec= getCodec(c);
} // Open output video
return writer.open(outputFile, // filename
codec, // codec to be used
framerate, // frame rate of the video
getFrameSize(), // frame size
isColor); // color video?
} // set the output as a series of image files
// extension must be ".jpg", ".bmp" ...
bool setOutput(const std::string &filename, // filename prefix
const std::string &ext, // image file extension
int numberOfDigits=3, // number of digits
int startIndex=0)
{ // start index // number of digits must be positive
if (numberOfDigits<0)
return false; // filenames and their common extension
outputFile= filename;
extension= ext; // number of digits in the file numbering scheme
digits= numberOfDigits;
// start numbering at this index
currentIndex= startIndex; return true;
} // set the callback function that will be called for each frame
void setFrameProcessor(void (*frameProcessingCallback)(cv::Mat&, cv::Mat&))
{ // invalidate frame processor class instance
frameProcessor= 0;
// this is the frame processor function that will be called
process= frameProcessingCallback;
callProcess();
} // set the instance of the class that implements the FrameProcessor interface
void setFrameProcessor(FrameProcessor* frameProcessorPtr)
{ // invalidate callback function
process= 0;
// this is the frame processor instance that will be called
frameProcessor= frameProcessorPtr;
callProcess();
} // stop streaming at this frame number
void stopAtFrameNo(long frame)
{ frameToStop= frame;
} // process callback to be called
void callProcess()
{ callIt= true;
} // do not call process callback
void dontCallProcess()
{ callIt= false;
} // to display the processed frames
void displayInput(std::string wn)
{ windowNameInput= wn;
cv::namedWindow(windowNameInput);
} // to display the processed frames
void displayOutput(std::string wn)
{ windowNameOutput= wn;
cv::namedWindow(windowNameOutput);
} // do not display the processed frames
void dontDisplay()
{ cv::destroyWindow(windowNameInput);
cv::destroyWindow(windowNameOutput);
windowNameInput.clear();
windowNameOutput.clear();
} // set a delay between each frame
// 0 means wait at each frame
// negative means no delay
void setDelay(int d)
{ delay= d;
} // a count is kept of the processed frames
long getNumberOfProcessedFrames()
{ return fnumber;
} // return the size of the video frame
cv::Size getFrameSize()
{ if (images.size()==0)
{ // get size of from the capture device
int w= static_cast<int>(capture.get(CV_CAP_PROP_FRAME_WIDTH));
int h= static_cast<int>(capture.get(CV_CAP_PROP_FRAME_HEIGHT)); return cv::Size(w,h); }
else
{ // if input is vector of images cv::Mat tmp= cv::imread(images[0]);
if (!tmp.data) return cv::Size(0,0);
else return tmp.size();
}
} // return the frame number of the next frame
long getFrameNumber()
{ if (images.size()==0)
{ // get info of from the capture device
long f= static_cast<long>(capture.get(CV_CAP_PROP_POS_FRAMES));
return f; } else { // if input is vector of images return static_cast<long>(itImg-images.begin());
}
} // return the position in ms
double getPositionMS()
{ // undefined for vector of images
if (images.size()!=0) return 0.0; double t= capture.get(CV_CAP_PROP_POS_MSEC);
return t;
} // return the frame rate
double getFrameRate()
{ // undefined for vector of images
if (images.size()!=0) return 0; double r= capture.get(CV_CAP_PROP_FPS);
return r;
} // return the number of frames in video
long getTotalFrameCount()
{ // for vector of images
if (images.size()!=0) return images.size(); long t= capture.get(CV_CAP_PROP_FRAME_COUNT);
return t;
} // get the codec of input video
int getCodec(char codec[4])
{ // undefined for vector of images
if (images.size()!=0) return -1; union {
int value;
char code[4]; } returned; returned.value= static_cast<int>(capture.get(CV_CAP_PROP_FOURCC)); codec[0]= returned.code[0];
codec[1]= returned.code[1];
codec[2]= returned.code[2];
codec[3]= returned.code[3]; return returned.value;
} // go to this frame number
bool setFrameNumber(long pos)
{ // for vector of images
if (images.size()!=0)
{ // move to position in vector
itImg= images.begin() + pos;
// is it a valid position?
if (pos < images.size())
return true;
else
return false; }
else
{ // if input is a capture device return capture.set(CV_CAP_PROP_POS_FRAMES, pos);
}
} // go to this position
bool setPositionMS(double pos)
{ // not defined in vector of images
if (images.size()!=0)
return false;
else
return capture.set(CV_CAP_PROP_POS_MSEC, pos);
} // go to this position expressed in fraction of total film length
bool setRelativePosition(double pos)
{ // for vector of images
if (images.size()!=0)
{ // move to position in vector
long posI= static_cast<long>(pos*images.size()+0.5);
itImg= images.begin() + posI;
// is it a valid position?
if (posI < images.size())
return true;
else
return false; }
else
{ // if input is a capture device return capture.set(CV_CAP_PROP_POS_AVI_RATIO, pos);
}
} // Stop the processing
void stopIt()
{ stop= true;
} // Is the process stopped?
bool isStopped()
{ return stop;
} // Is a capture device opened?
bool isOpened()
{ return capture.isOpened() || !images.empty();
} // to grab (and process) the frames of the sequence
void run()
{ // current frame
cv::Mat frame;
// output frame
cv::Mat output; // if no capture device has been set
if (!isOpened())
return; stop= false; while (!isStopped())
{ // read next frame if any
if (!readNextFrame(frame))
break; // display input frame
if (windowNameInput.length()!=0)
cv::imshow(windowNameInput,frame); // calling the process function or method
if (callIt)
{ // process the frame
if (process)
process(frame, output);
else if (frameProcessor)
frameProcessor->process(frame,output);
// increment frame number
fnumber++; }
else
{ output= frame;
} // write output sequence
if (outputFile.length()!=0)
writeNextFrame(output); // display output frame
if (windowNameOutput.length()!=0)
cv::imshow(windowNameOutput,output); // introduce a delay
if (delay>=0 && cv::waitKey(delay)>=0)
stopIt(); // check if we should stop
if (frameToStop>=0 && getFrameNumber()==frameToStop)
stopIt();
}
}
}; #endif
/*------------------------------------------------------------------------------------------*\
This file contains material supporting chapter 10 of the cookbook:
Computer Vision Programming using the OpenCV Library.
by Robert Laganiere, Packt Publishing, 2011. This program is free software; permission is hereby granted to use, copy, modify,
and distribute this source code, or portions thereof, for any purpose, without fee,
subject to the restriction that the copyright notice may not be removed
or altered from any source or altered source distribution.
The software is released on an as-is basis and without any warranties of any kind.
In particular, the software is not guaranteed to be fault-tolerant or free from failure.
The author disclaims all warranties with regard to this software, any use,
and any consequent failure, is purely the responsibility of the user. Copyright (C) 2010-2011 Robert Laganiere, www.laganiere.name
\*------------------------------------------------------------------------------------------*/ #include "videoprocessor.h" void draw(cv::Mat& img, cv::Mat& out)
{ img.copyTo(out);
cv::circle(out, cv::Point(100,100),5,cv::Scalar(255,0,0),2);
} void canny(cv::Mat& img, cv::Mat& out) { // Convert to gray
cv::cvtColor(img,out,CV_BGR2GRAY);
// Compute Canny edges
cv::Canny(out,out,100,200);
// Invert the image
cv::threshold(out,out,128,255,cv::THRESH_BINARY_INV);
} int main()
{
// Open the video file
cv::VideoCapture capture("../bike.avi");
// check if video successfully opened
if (!capture.isOpened())
return 1; // Get the frame rate
double rate= capture.get(CV_CAP_PROP_FPS); bool stop(false);
cv::Mat frame; // current video frame
cv::namedWindow("Extracted Frame"); // Delay between each frame
// corresponds to video frame rate
int delay= 1000/rate; // for all frames in video
while (!stop) { // read next frame if any
if (!capture.read(frame))
break; cv::imshow("Extracted Frame",frame); // introduce a delay
// or press key to stop
if (cv::waitKey(delay)>=0)
stop= true;
} // Close the video file
capture.release(); cv::waitKey(); // Now using the VideoProcessor class // Create instance
VideoProcessor processor;
// Open video file
processor.setInput("../bike.avi");
// Declare a window to display the video
processor.displayInput("Input Video");
processor.displayOutput("Output Video");
// Play the video at the original frame rate
processor.setDelay(1000./processor.getFrameRate());
// Set the frame processor callback function
processor.setFrameProcessor(canny);
// Start the process
processor.run();
cv::waitKey(); // Second test
// Create instance
// VideoProcessor processor;
// Open video file
processor.setInput("../bike.avi"); // Get basic info about video file
cv::Size size= processor.getFrameSize();
std::cout << size.width << " " << size.height << std::endl;
std::cout << processor.getFrameRate() << std::endl;
std::cout << processor.getTotalFrameCount() << std::endl;
std::cout << processor.getFrameNumber() << std::endl;
std::cout << processor.getPositionMS() << std::endl; // No processing
processor.dontCallProcess();
// Output filename
// processor.setOutput("../output/bikeOut",".jpg");
char codec[4];
processor.setOutput("../output/bike.avi",processor.getCodec(codec),processor.getFrameRate());
std::cout << "Codec: " << codec[0] << codec[1] << codec[2] << codec[3] << std::endl; // Position the stream at frame 300
// processor.setFrameNumber(300);
// processor.stopAtFrameNo(120); // Declare a window to display the video
processor.displayInput("Current Frame");
processor.displayOutput("Output Frame"); // Play the video at the original frame rate
processor.setDelay(1000./processor.getFrameRate()); // Start the process
processor.run(); std::cout << processor.getFrameNumber() << std::endl;
std::cout << processor.getPositionMS() << std::endl; cv::waitKey();
}

OpenCV处理视频序列的类的更多相关文章

  1. Atitit Java OpenCV 捕获视频

    Atitit Java  OpenCV 捕获视频 ,打开一段视频或默认的摄像头 有两种方法,一种是在定义类的时候,一种是用open()方法. 一. 读取视频序列 OpenCV提供了一个简便易用的框架以 ...

  2. [AI开发]零代码分析视频结构化类应用结构设计

    视频结构化类应用涉及到的技术栈比较多,而且每种技术入门门槛都较高,比如视频接入存储.编解码.深度学习推理.rtmp流媒体等等.每个环节的水都非常深,单独拿出来可以写好几篇文章,如果没有个几年经验基本很 ...

  3. OpenCV的视频读取

    现在找一个能拍摄视频的设备真是太容易了.结果大家都用视频来代替以前的序列图像.视频可能由两种形式得到,一个是像网络摄像头那样实时视频流,或者由其他设备产生的压缩编码后的视频文件.幸运的是,OpenCV ...

  4. OpenCV读写视频文件解析

    OpenCV读写视频文件解析 一.视频读写类 视频处理的是运动图像,而不是静止图像.视频资源可以是一个专用摄像机.网络摄像头.视频文件或图像文件序列. 在 OpenCV 中,VideoCapture ...

  5. 【计算机视觉】OpenCV读取视频获取时间戳等信息(PS:经测试并不是时间戳,与FFMPEG时间戳不一样)

    OpenCV中通过VideoCaptrue类对视频进行读取操作以及调用摄像头,下面是该类的API. 1.VideoCapture类的构造函数: C++: VideoCapture::VideoCapt ...

  6. 使用opencv显示视频的方法

    下面对使用opencv显示视频做一个简单的记录.当然,网上这方面的资料已经数不胜数了,我只是将其简单记录,总结一下. 在opencv中显示视频主要有: (1)从本地读取视频和调用摄像头读取视频 (2) ...

  7. FFmpeg YUV视频序列编码为视频

    上一篇已经写了如何配置好开发环境,这次就先小试牛刀,来个视频的编码.搞视频处理的朋友肯定比较熟悉YUV视频序列,很多测试库提供的视频数据都是YUV视频序列,我们这里就用用YUV视频序列来做视频.关于Y ...

  8. java 实现视频转换通用工具类:视频截图-Ffmpeg(四)

    java 实现视频转换通用工具类:获取视频元数据信息(一) java 实现视频转换通用工具类:视频相互转换-总方法及Mencoder(二) java 实现视频转换通用工具类:视频相互转换-Ffmpeg ...

  9. java 实现视频转换通用工具类:视频相互转换-Ffmpeg(三)

    java 实现视频转换通用工具类:获取视频元数据信息(一) java 实现视频转换通用工具类:视频相互转换-总方法及Mencoder(二) 这节主要是ffmpeg的相关方法封装,在实际调用中主要使用f ...

随机推荐

  1. 【C++】处理CSDN博文源码

    为了简化CSDN写博客的字体问题,给出一段代码,用于处理使用默认格式写完博客后,处理一次来解决字体问题. 代码片段 代码片段如下所示: #include <iostream> #inclu ...

  2. SpriteKit物理引擎碰撞中5个重要信息

    我们知道在SpriteKit物理引擎实际是基于Box2D! 在SpriteKit中当你设置好适当的碰撞参数后,通过遵守SKPhysicsContactDelegate,你可以选择实现2各碰撞回调方法: ...

  3. 2 TileMapObject的使用

    1 CCTMXObjectGroup的使用方法 为了取以下内容: 操作代码如下: T27TileMapObject.h #ifndef __T27TileMapObject_H__ #define _ ...

  4. 【Unity Shaders】使用CgInclude让你的Shader模块化——使用#define指令创建Shader

    本系列主要参考<Unity Shaders and Effects Cookbook>一书(感谢原书作者),同时会加上一点个人理解或拓展. 这里是本书所有的插图.这里是本书所需的代码和资源 ...

  5. 【Unity Shaders】Mobile Shader Adjustment—— 什么是高效的Shader

    本系列主要参考<Unity Shaders and Effects Cookbook>一书(感谢原书作者),同时会加上一点个人理解或拓展. 这里是本书所有的插图.这里是本书所需的代码和资源 ...

  6. 《java入门第一季》之HashSet存储自定义对象问题以及注意事项

    上一篇http://blog.csdn.net/qq_32059827/article/details/51578158 写到存储字符串类型的时候出现了无序,而且这个无序不是随机那种无序,它是有一定存 ...

  7. Mac下关于->您不能拷贝项目“”,因为它的名称太长或包括的字符在目的宗卷上无效。<-的删除

    打开 Terminal 应用程序. 键入: sudo rm -rf注意:在"-rf"后键入一个空格.没有空格该命令将不能执行.在步骤 6 之前请不要按下 Return 键. 打开您 ...

  8. PHP解决中文乱码问题

    初学PHP,在汉字页面间传输和转换的时候,遇到了中文乱码问题. 究其原因乱码无外乎以下几种情况: 1.html页本身的乱码问题, 解决方法:纯html页使用<meta http-equiv=&q ...

  9. 推荐一本不错的书《Sencha Ext JS 5 Bootcamp in a Book》

    原文:https://www.createspace.com/5425618 看了一下该书目录,感觉不错,Ext JS 5的重点内容都提及了,确实是一本学习Ext JS 5的好书,唯一遗憾的地方就是太 ...

  10. 数据结构-自平衡二叉查找树(AVL)详解

    介绍: 在计算机科学中,AVL树是最先发明的自平衡二叉查找树. 在AVL树中任何节点的两个子树的高度最大差别为一,所以它也被称为高度平衡树. 查找.插入和删除在平均和最坏情况下都是O(log n).增 ...