通过此文章可快速了解C++opencv的关于视频读取、颜色风格、视频保存相关的知识点。
目录
1. 相关知识点
2. 代码
编写代码main.cpp:
编写CmakeLists.txt:
编译并执行:
3. 效果展示
1. 相关知识点
1.1 VideoCapture
在C++中,使用OpenCV库中的VideoCapture
类来捕获视频流;VideoCapture
构造函数接受一个参数,指定要打开的视频文件的路径或者设备的索引号。
类定义:
class CV_EXPORTS_W VideoCapture
{
public:
CV_WRAP VideoCapture();
CV_WRAP VideoCapture(const String& filename, int apiPreference = CAP_ANY);
CV_WRAP VideoCapture(int index, int apiPreference = CAP_ANY);
CV_WRAP virtual ~VideoCapture();
CV_WRAP virtual bool open(const String& filename, int apiPreference = CAP_ANY);
CV_WRAP virtual bool open(int index, int apiPreference = CAP_ANY);
CV_WRAP virtual void release();
CV_WRAP virtual bool isOpened() const;
CV_WRAP virtual bool grab();
CV_WRAP virtual bool retrieve(OutputArray image, int flag = 0);
CV_WRAP virtual bool read(OutputArray image);
CV_WRAP virtual bool set(int propId, double value);
CV_WRAP virtual double get(int propId) const;
CV_WRAP virtual bool set(const String& propName, double value);
CV_WRAP virtual double get(const String& propName) const;
};
1.2 applyColorMap
它是OpenCV中的一个函数,用于将彩色映射应用到输入图像上。它通过将图像的灰度级映射到不同的颜色上来增强图像的视觉效果。
函数定义:
void applyColorMap(InputArray src, OutputArray dst, int colormap)
参数说明:
-
src
:输入图像,可以是单通道灰度图像或三通道彩色图像。 -
dst
:输出图像,与输入图像具有相同的尺寸和深度。 -
colormap
:选择要应用的彩色映射,可以是以下之一:-
cv::COLORMAP_AUTUMN
:秋季调色板 -
cv::COLORMAP_BONE
:骨骼调色板 -
cv::COLORMAP_JET
:喷射器调色板 -
cv::COLORMAP_WINTER
:冬季调色板 -
cv::COLORMAP_RAINBOW
:彩虹调色板 -
cv::COLORMAP_OCEAN
:海洋调色板 -
cv::COLORMAP_SUMMER
:夏季调色板 -
cv::COLORMAP_SPRING
:春季调色板 -
cv::COLORMAP_COOL
:凉爽调色板 -
cv::COLORMAP_HSV
:HSV调色板 -
cv::COLORMAP_PINK
:粉红调色板 -
cv::COLORMAP_HOT
:热调色板
-
1.3 VideoWriter
VideoWriter
类用来创建和写入视频文件。VideoWriter
类提供了一组方法来设置输出视频的参数,如帧率、分辨率、编解码器等,并且可以通过write
方法将帧写入视频文件。
类定义如下:
class CV_EXPORTS_W VideoWriter
{
public:
CV_WRAP VideoWriter();
CV_WRAP VideoWriter(const String& filename, int fourcc, double fps, Size frameSize, bool isColor = true);
CV_WRAP virtual ~VideoWriter();
CV_WRAP virtual bool open(const String& filename, int fourcc, double fps, Size frameSize, bool isColor = true);
CV_WRAP virtual void release();
CV_WRAP virtual bool isOpened() const;
CV_WRAP virtual void write(InputArray image);
CV_WRAP virtual bool set(int propId, double value);
CV_WRAP virtual double get(int propId) const;
CV_WRAP virtual bool set(const String& propName, double value);
CV_WRAP virtual double get(const String& propName) const;
};
2. 代码
2.1 编写代码main.cpp
这里我们定义了一个函数videoRW,有两个参数:输入视频的地址,保存结果视频的地址;在main函数中,通过命令行参数来指定输入视频、输出视频的地址,即可运行、实时可视化,并保存结果到指定路径:
#include<iostream>
#include "opencv2/opencv.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
using namespace cv;
void videoRW(String video_path, String out_path) {
std::cout << video_path << std::endl;
std::cout << out_path << std::endl;
VideoCapture video(video_path);
if (!video.isOpened())
{
std::cout << "Read Video Error!" << std::endl;
return;
}
int width = cvRound(video.get(CAP_PROP_FRAME_WIDTH));
int higth = cvRound(video.get(CAP_PROP_FRAME_HEIGHT));
std::cout << "Weight: " << width << std::endl;
std::cout << "Height: " << higth << std::endl;
std::cout << "FPS: " << video.get(CAP_PROP_FPS) << std::endl;
std::cout << "Frame num: " << video.get(CAP_PROP_FRAME_COUNT) << std::endl;
VideoWriter writer(out_path, video.get(CAP_PROP_FOURCC), video.get(CAP_PROP_FPS),
Size(width*2, higth));
Mat frame, dst;
int index = 0;
while (true) {
video.read(frame);
if (frame.empty()){
break;
}
index++;
applyColorMap(frame, dst, cvRound(index/10) % 21);
Mat big(higth, width * 2, CV_8UC3);
frame.copyTo(big(Rect(0, 0, width, higth)));
dst.copyTo(big(Rect(width, 0, width, higth)));
String wname = "video";
namedWindow(wname, 0);
imshow(wname, big);
writer.write(big);
int key = waitKey(10);
if (key == 27) {
break;
}
}
video.release();
writer.release();
}
int main(int argc, char **argv)
{
std::cout << "arg count: " << argc << "\narg value: " << argv << std::endl;
std::cout << "video demo~" << std::endl;
const String video_path = argv[1];
String out_path = argv[2];
videoRW(video_path, out_path);
cv::destroyAllWindows();
std::cout<<"\n------Demo Over!------\n"<<std::endl;
return 0;
}
2.2 编写CmakeLists.txt
这里在CmakeLists.txt中指定了我们所支持的最小cmake版本VERSION 3.1, 调用的是cmake_minimum_required();也制定了工程名称、指定了opencv库、打印了一些信息、添加了源文件、链接形成应用程序:
# cmake needs this line
cmake_minimum_required(VERSION 3.1)
# Define project name
project(opencv_example_project)
# Find OpenCV, you may need to set OpenCV_DIR variable
# to the absolute path to the directory containing OpenCVConfig.cmake file
# via the command line or GUI
find_package(OpenCV REQUIRED)
# If the package has been found, several variables will
# be set, you can find the full list with descriptions
# in the OpenCVConfig.cmake file.
# Print some message showing some of them
message(STATUS "OpenCV library status:")
message(STATUS " config: ${OpenCV_DIR}")
message(STATUS " version: ${OpenCV_VERSION}")
message(STATUS " libraries: ${OpenCV_LIBS}")
message(STATUS " include path: ${OpenCV_INCLUDE_DIRS}")
aux_source_directory(. ALL_SRCS)
# Declare the executable target built from your sources
add_executable(demo ${ALL_SRCS})
# Link your application with OpenCV libraries
target_link_libraries(demo PRIVATE ${OpenCV_LIBS})
2.3 编译并执行
# 编译
mkdir build
cd build
cmake ..
make
# 运行:需指定输入视频的路径和视频保存的路径
./demo input.mp4 output.mp4
3. 效果展示
运行后,可以看到如下的效果:
视频如下: 文章来源:https://www.toymoban.com/news/detail-634272.html
视频风格变换与拼接文章来源地址https://www.toymoban.com/news/detail-634272.html
到了这里,关于C++ opencv:视频读取、变换颜色风格、保存的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!