// ---------------------------------- - OpenCV学习15-------------------------------------
// 程序摘要:视频读取及自定义视频类处理
/* 读取视频序列;
自定义视频处理类VideoProcessor:可以处理视频和独立的图像序列,前者用以前的帧处理函数(回调函数process),
后者用自定义的帧处理类(FrameProcessor类接口即对象->process方法);其中用到了canny函数,其中涉及把每帧图像彩色转灰度和用Canny和
threshold函数*/
// 写入视频帧类VideoWriter,在VideoProcessor的方法中应用写入类实例的成员函数,也分为处理视频和独立的图像序列即用到成员函数重载
// 以上描述为:程序后半段用自定义视频处理类封装视频捕捉类、帧处理类和视频帧写入类
// 2016年10月 Created by孙立波(Visual Studio 2013+OpenCV2.4.9)
#include <opencv2\opencv.hpp>
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
using namespace cv;
using namespace std;
#include "videoprocessor.h"
void draw(const cv::Mat& img, cv::Mat& out) {
img.copyTo(out);
cv::circle(out, cv::Point(100, 100), 5, cv::Scalar(255, 0, 0), 2);
}
// processing function此函数定义用于真正的图像处理,为回调函数在main中注册后每次执行的函数处理,注意canny是封装了Canny函数
void canny(cv::Mat& img, cv::Mat& out) {
// Convert to gray
if (img.channels() == 3)
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()
{
// 打开视频文件
cv::VideoCapture capture("D:\\workplace\\opencv_training\\bike.avi");
//cv::VideoCapture capture("http://www.laganiere.name/bike.avi");
// 核实视频文件是否却是打开
if (!capture.isOpened())
return 1;
// Get the frame rate
double rate = capture.get(CV_CAP_PROP_FPS);
std::cout << "Frame rate: " << rate << "fps" << std::endl;
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;//视频播放按原始帧率,可设置使视频快进或倒退
long long i = 0;
std::string b = "bike";
std::string ext = ".bmp";
// for all frames in video抽取视频帧保存到同一工程目录下!!!!!!!!!
while (!stop)
{
// read next frame if any
if (!capture.read(frame))
break;
cv::imshow("Extracted Frame", frame);
std::string name(b);
// note: some MinGW compilers generate an error for this line
// this is a compiler bug
// try: std::ostringstream ss; ss << i; name+= ss.rdbuf(); i++;
// name+=std::to_string(i++);
std::ostringstream ss; ss << i; name += ss.str(); i++;
name += ext;
std::cout << name << std::endl;
cv::imwrite(name, frame);
// introduce a delay
// or press key to stop
if (cv::waitKey(delay) >= 0)//当超过指定时间,没有按键盘时,返回值-1
stop = true;
}
// Close the video file
capture.release();
cv::waitKey();
//###########################################################################################
// Now using the VideoProcessor class用自定义视频处理类封装视频捕捉类、帧处理类和视频帧写入类
// Create instance
VideoProcessor processor;
// Open video file
processor.setInput("D:\\workplace\\opencv_training\\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*****设置类的实例的回调函数方法为main前声明的canny()函数!!!
processor.setFrameProcessor(canny);
// output a video
//传入值-1代表不采用与输入一致的编解码方式,而是提供MFC供选择,15代表帧的速率
processor.setOutput("D:\\workplace\\opencv_training\\bike1.avi", -1, 15);
// stop the process at this frame,只处理前51帧并保存到内存
processor.stopAtFrameNo(51);
// Start the process
processor.run();
cv::waitKey();
return 0;
}
opencv学习系列:视频读取及自定义视频类处理
最新推荐文章于 2022-08-02 17:43:36 发布