C++ opencv:视频读取、变换颜色风格、保存

1. 相关知识点

VideoCapture;

applyColorMap;

VideoWriter;

2. 代码

编写代码main.cpp:

#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;
}

编写CmakeLists.txt:

# 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})

编译并执行:

# 编译
mkdir build
cd build
cmake ..
make

# 运行:需指定输入视频的路径和视频保存的路径
./demo input.mp4 output.mp4

3. 效果展示

视频风格变换与拼接