《OpenCV2 计算机视觉编程手册》视频处理一

2024-04-07 06:38

本文主要是介绍《OpenCV2 计算机视觉编程手册》视频处理一,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

本文主要结合《OpenCV2 计算机视觉编程手册》第10章的内容,学习OpenCV 处理视频图像的一般方法,包括读入,处理,写出。

1.头文件

#ifndef HEAD_H_
#define HEAD_H_#include <iostream>
#include <iomanip>// 控制输出格式
#include <sstream>// 文件流控制
#include <string>
#include <vector>#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>#endif // HEAD_H_


2. VideoProcessor头文件


#ifndef VPROCESSOR_H_
#define VPROCESSOR_H_#include "head.h"void canny(cv::Mat& img, cv::Mat& out)
{cv::cvtColor(img, out, CV_BGR2GRAY);                      // 灰度转换cv::Canny(out, out, 100, 200);                            // Canny边缘检测cv::threshold(out, out, 128, 255, cv::THRESH_BINARY_INV); // 二值图像反转, 小于128设置为255, 否则为0;即边缘为黑色
}// 抽象类FrameProcessor中纯虚函数process必须在子类(其继承类)中重新定义。
// http://blog.csdn.net/hackbuteer1/article/details/7558868 
// 帧处理器接口
class FrameProcessor 
{public:// 处理方法,定义为纯虚函数, 让其子类实现具体的接口virtual void process(cv:: Mat &input, cv:: Mat &output)= 0;
};class VideoProcessor 
{private:cv::VideoCapture capture;           // OpenCV视频采集对象(object)void (*process)(cv::Mat&, cv::Mat&);// 每帧处理的回调函数, 函数指针调用FrameProcessor *frameProcessor;     // 基类(含纯虚函数的抽象类)FrameProcessor接口指针, 指向子类的实现bool callIt;                        // 启动回调函数与否的bool判断, true:调用, false:不调用std::string windowNameInput;        // 输入显示窗口名字std::string windowNameOutput;       // 输出显示窗口名字int delay;                          // 帧间处理延迟long fnumber;                       // 已处理帧总数long frameToStop;                   // 在该帧停止	  bool stop;                          // 停止处理标志位!std::vector<std::string> images;               // 输入的图像集或者图像向量(vector容器)std::vector<std::string>::const_iterator itImg;// 图像集的迭代器cv::VideoWriter writer;             // OpenCV视频写出对象std::string outputFile;             // 输出视频文件名字int currentIndex;                   // 输出图像集的当前索引int digits;                         // 输出图像文件名字的数字 std::string extension;              // 输出图像集的扩展名// Getting the next frame which could be: video file; camera; vector of imagesbool 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;}else{return false;}}}// Writing the output frame which could be: video file or imagesvoid writeNextFrame(cv::Mat& frame) {if (extension.length()) { // 输出图像文件std::stringstream ss;ss << outputFile << std::setfill('0') << std::setw(digits) << currentIndex++ << extension;cv::imwrite(ss.str(),frame);} else { // 输出视频文件writer.write(frame);}}public:// 构造函数VideoProcessor() : callIt(false), delay(-1), fnumber(0), stop(false), digits(0), frameToStop(-1), process(0), frameProcessor(0) {}// 设置视频文件的名字bool setInput(std::string filename) {fnumber= 0;// In case a resource was already // associated with the VideoCapture instancecapture.release();            // 释放之前打开过的资源images.clear();               // 释放之前打开过的资源return capture.open(filename);// 打开视频文件}// 设置相机IDbool setInput(int id) {fnumber= 0;// In case a resource was already // associated with the VideoCapture instancecapture.release();images.clear();// 打开视频文件return capture.open(id);}// 设置输入的图像集bool setInput(const std::vector<std::string>& imgs) {fnumber= 0;// In case a resource was already // associated with the VideoCapture instancecapture.release();//释放之前打开过的资源// 输入的是图像集images= imgs;itImg= images.begin();return true;}// 设置输出视频文件, 默认参数和输入的一样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(); // 与输入相同char c[4];                     // 使用和输入相同的编码格式if (codec==0) { codec= getCodec(c);}// 打开输出视频return writer.open(outputFile, // 文件名codec,                     // 使用的解码格式 framerate,                 // 帧率getFrameSize(),            // 帧大小isColor);                  // 是否为彩色视频}// 设置输出是图像集, 后缀必须是".jpg", ".bmp" ...bool setOutput(const std::string &filename, // 文件名前缀const std::string &ext,                 // 图像文件后缀 int numberOfDigits=3,                   // 数字位数int startIndex=0)                       // 开始索引000{     if (numberOfDigits<0)                   // 数字位数必须是正数return false;outputFile = filename;                  // 文件名extension  = ext;                       // 公共后缀名digits       = numberOfDigits;          // 文件名中的数字位数 currentIndex = startIndex;              // 开始索引return true;}// 设置每一帧的回调函数void setFrameProcessor(void (*frameProcessingCallback)(cv::Mat&, cv::Mat&)) {// invalidate frame processor class instance 使FrameProcessor实例无效化frameProcessor = 0;process = frameProcessingCallback;callProcess();}// 设置FrameProcessor接口实例void setFrameProcessor(FrameProcessor* frameProcessorPtr) {// invalidate callback function 使回调函数无效化process = 0;frameProcessor= frameProcessorPtr;callProcess();}// 在frame帧停止void stopAtFrameNo(long frame) {frameToStop= frame;}// 处理回调函数void callProcess() {callIt= true;}// 不调用回调函数void dontCallProcess() {callIt= false;}// 显示输入的图像帧void displayInput(std::string wn) {windowNameInput= wn;cv::namedWindow(windowNameInput);}// 显示处理的图像帧void displayOutput(std::string wn) {windowNameOutput= wn;cv::namedWindow(windowNameOutput);}// 不显示处理的图像帧void dontDisplay() {cv::destroyWindow(windowNameInput);cv::destroyWindow(windowNameOutput);windowNameInput.clear();windowNameOutput.clear();}// 设置帧间延迟时间// 0 means wait at each frame// negative means no delayvoid setDelay(int d) {delay= d;}// 处理帧的总数long getNumberOfProcessedFrames() {return fnumber;}// 返回视频帧的大小cv::Size getFrameSize() {if (images.size()==0) {// get size of from the capture deviceint 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 imagescv::Mat tmp= cv::imread(images[0]);if (!tmp.data) return cv::Size(0,0);else return tmp.size();}}// 返回下一帧的帧数long getFrameNumber() {if (images.size()==0) {// get info of from the capture devicelong f= static_cast<long>(capture.get(CV_CAP_PROP_POS_FRAMES));return f; } else { // if input is vector of imagesreturn static_cast<long>(itImg-images.begin());}}// return the position in msdouble getPositionMS() {// undefined for vector of imagesif (images.size()!=0) return 0.0;double t= capture.get(CV_CAP_PROP_POS_MSEC);return t; }// 返回帧率double getFrameRate() {// undefined for vector of imagesif (images.size()!=0) return 0;double r= capture.get(CV_CAP_PROP_FPS);return r; }// 返回视频中图像的总数long getTotalFrameCount(){// for vector of imagesif (images.size()!=0) return images.size();long t= capture.get(CV_CAP_PROP_FRAME_COUNT);return t; }// 获取输入视频的编解码器int getCodec(char codec[4]) {// 未制定的图像集if (images.size()!=0) return -1;union {// 4-char编码的数据结果int value;char code[4]; } returned;// 获取编码returned.value= static_cast<int>(capture.get(CV_CAP_PROP_FOURCC));// 获得4字符codec[0]= returned.code[0];codec[1]= returned.code[1];codec[2]= returned.code[2];codec[3]= returned.code[3];// 返回对应的整数return returned.value;}// 设置帧位置bool setFrameNumber(long pos) {// for vector of imagesif (images.size()!=0) {// move to position in vectoritImg= images.begin() + pos;// is it a valid position?if (pos < images.size())return true;elsereturn false;} else { // if input is a capture devicereturn capture.set(CV_CAP_PROP_POS_FRAMES, pos);}}// go to this positionbool setPositionMS(double pos) {// not defined in vector of imagesif (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 lengthbool setRelativePosition(double pos) {// for vector of imagesif (images.size()!=0) {// move to position in vectorlong posI= static_cast<long>(pos*images.size()+0.5);itImg= images.begin() + posI;// is it a valid position?if (posI < images.size())return true;elsereturn false;} else { // if input is a capture devicereturn capture.set(CV_CAP_PROP_POS_AVI_RATIO, pos);}}// 停止运行void stopIt() {stop= true;}// 是否已停止运行?bool isStopped() {return stop;}// 判断是否是视频捕获设备或图像集bool isOpened() {return capture.isOpened() || !images.empty();}// 获取并处理图像void run() {cv::Mat frame;  // 当前帧cv::Mat output; // 输出帧// if no capture device has been setif (!isOpened())return;stop= false;while (!isStopped()) {// 读取下一帧if (!readNextFrame(frame))break;// 显示输出帧if (windowNameInput.length()!=0) cv::imshow(windowNameInput,frame);// 调用帧处理回调函数或FrameProcessor实例if (callIt) {  // 处理当前帧if (process)             // 如果是回调函数process(frame, output);else if (frameProcessor) //如果是FrameProcessor实例frameProcessor->process(frame,output);// 增加帧数fnumber++;}else{output= frame;}// 写出输出图像序列if (outputFile.length()!=0)writeNextFrame(output);// 显示输出帧if (windowNameOutput.length()!=0) cv::imshow(windowNameOutput,output);// 引入帧间延迟if (delay>=0 && cv::waitKey(delay)>=0)stopIt();// 检查是否需要停止运行if (frameToStop>=0 && getFrameNumber()==frameToStop)stopIt();}}
};#endif // VPROCESSOR_H_

3. 主函数


#include "head.h"
#include "videoprocessor.h"int main()
{//----Zero Test----cv::VideoCapture capture("../bike.avi"); // 打开视频/摄像头0if (!capture.isOpened())return 1;double rate= capture.get(CV_CAP_PROP_FPS);// 获取帧率bool stop(false);cv::Mat frame;                            // 当前帧cv::namedWindow("Extracted Frame");int delay= 1000/rate;                     // 延迟的毫秒//int delay = 1000;// 处理视频所有帧while (!stop) {// read next frame if anyif (!capture.read(frame))break;cv::imshow("Extracted Frame",frame);if (cv::waitKey(delay)>=0)            // 延迟等待直到cv::waitKey(delay)<0stop= true;}capture.release();                       // 因为capture自动调用析构函数,所以capture.release不是必须的!cv::waitKey();//----First Test----VideoProcessor processor;                           // 创建VideoProcessor类实例 processorprocessor.setInput("../bike.avi");                  // 打开视频文件bike.aviprocessor.displayInput("Input Video");              // 声明输入视频显示窗口processor.displayOutput("Output Video");            // 声明输出视频显示窗口processor.setDelay(1000./processor.getFrameRate()); // 设置播放视频为原始输入视频帧率processor.setFrameProcessor(canny);                 // 设置帧处理器的回调函数--cannyprocessor.run();                                    // 开始处理视频文件cv::waitKey();                                      // 等待按键响应//----Second test----processor.setInput("../bike.avi");                            // 重新设置打开视频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;          // 视频帧的位置(ms)processor.dontCallProcess();                                   // 不处理打开视频文件// 输出.jpg视频图像到output文件夹, 图像名字为bikeOut000.jpg~bikeOut118.jpgprocessor.setOutput("../output/bikeOut",".jpg");               processor.run(); cv::waitKey();// 输出bike.avi视频到output文件夹,编解码器为:XVID, 基于MPEG-4视频标准的开源解码库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;processor.run();cv::waitKey();//----Three test----processor.setInput("../bike.avi");processor.displayInput("Input Video");              // 声明输入视频显示窗口processor.displayOutput("Output Video");            // 声明输出视频显示窗口processor.setFrameNumber(80);                       // 设置帧的位置processor.stopAtFrameNo(120);                       // 停止的帧位置processor.setDelay(1000./processor.getFrameRate());processor.run();cv::waitKey();return 0;
}

Canny边缘检测



视频写出结果(包含文件和视频)



制定开始帧和结束帧位置








这篇关于《OpenCV2 计算机视觉编程手册》视频处理一的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/881889

相关文章

python web 开发之Flask中间件与请求处理钩子的最佳实践

《pythonweb开发之Flask中间件与请求处理钩子的最佳实践》Flask作为轻量级Web框架,提供了灵活的请求处理机制,中间件和请求钩子允许开发者在请求处理的不同阶段插入自定义逻辑,实现诸如... 目录Flask中间件与请求处理钩子完全指南1. 引言2. 请求处理生命周期概述3. 请求钩子详解3.1

Python处理大量Excel文件的十个技巧分享

《Python处理大量Excel文件的十个技巧分享》每天被大量Excel文件折磨的你看过来!这是一份Python程序员整理的实用技巧,不说废话,直接上干货,文章通过代码示例讲解的非常详细,需要的朋友可... 目录一、批量读取多个Excel文件二、选择性读取工作表和列三、自动调整格式和样式四、智能数据清洗五、

SpringBoot如何对密码等敏感信息进行脱敏处理

《SpringBoot如何对密码等敏感信息进行脱敏处理》这篇文章主要为大家详细介绍了SpringBoot对密码等敏感信息进行脱敏处理的几个常用方法,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录​1. 配置文件敏感信息脱敏​​2. 日志脱敏​​3. API响应脱敏​​4. 其他注意事项​​总结

Python使用python-docx实现自动化处理Word文档

《Python使用python-docx实现自动化处理Word文档》这篇文章主要为大家展示了Python如何通过代码实现段落样式复制,HTML表格转Word表格以及动态生成可定制化模板的功能,感兴趣的... 目录一、引言二、核心功能模块解析1. 段落样式与图片复制2. html表格转Word表格3. 模板生

无法启动此程序因为计算机丢失api-ms-win-core-path-l1-1-0.dll修复方案

《无法启动此程序因为计算机丢失api-ms-win-core-path-l1-1-0.dll修复方案》:本文主要介绍了无法启动此程序,详细内容请阅读本文,希望能对你有所帮助... 在计算机使用过程中,我们经常会遇到一些错误提示,其中之一就是"api-ms-win-core-path-l1-1-0.dll丢失

Python Pandas高效处理Excel数据完整指南

《PythonPandas高效处理Excel数据完整指南》在数据驱动的时代,Excel仍是大量企业存储核心数据的工具,Python的Pandas库凭借其向量化计算、内存优化和丰富的数据处理接口,成为... 目录一、环境搭建与数据读取1.1 基础环境配置1.2 数据高效载入技巧二、数据清洗核心战术2.1 缺失

SpringBoot项目中Redis存储Session对象序列化处理

《SpringBoot项目中Redis存储Session对象序列化处理》在SpringBoot项目中使用Redis存储Session时,对象的序列化和反序列化是关键步骤,下面我们就来讲讲如何在Spri... 目录一、为什么需要序列化处理二、Spring Boot 集成 Redis 存储 Session2.1

Python处理超大规模数据的4大方法详解

《Python处理超大规模数据的4大方法详解》在数据的奇妙世界里,数据量就像滚雪球一样,越变越大,从最初的GB级别的小数据堆,逐渐演变成TB级别的数据大山,所以本文我们就来看看Python处理... 目录1. Mars:数据处理界的 “变形金刚”2. Dask:分布式计算的 “指挥家”3. CuPy:GPU

Python中CSV文件处理全攻略

《Python中CSV文件处理全攻略》在数据处理和存储领域,CSV格式凭借其简单高效的特性,成为了电子表格和数据库中常用的文件格式,Python的csv模块为操作CSV文件提供了强大的支持,本文将深入... 目录一、CSV 格式简介二、csv模块核心内容(一)模块函数(二)模块类(三)模块常量(四)模块异常

详解如何在SpringBoot控制器中处理用户数据

《详解如何在SpringBoot控制器中处理用户数据》在SpringBoot应用开发中,控制器(Controller)扮演着至关重要的角色,它负责接收用户请求、处理数据并返回响应,本文将深入浅出地讲解... 目录一、获取请求参数1.1 获取查询参数1.2 获取路径参数二、处理表单提交2.1 处理表单数据三、