C++ 调用 opencv 读取视频文件列表并处理

//g++ trans_video.cpp -o trans_video `pkg-config opencv --libs --cflags` -L/usr/lib/x86_64-linux-gnu/ -lcairo

//g++ trans_video.cpp `pkg-config --cflags --libs opencv` -I/usr/include/cairo/ -L/usr/lib/x86_64-linux-gnu/ -lboost_system -lglog  -lcairo

#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv/cxcore.h>
#include <cairo/cairo-svg.h>
//#include "CvxText.h"

using namespace cv;
using namespace std;

void putTextCairo(cv::Mat &targetImage, std::string const& text,
        cv::Point2d centerPoint, std::string const& fontFace,
        double fontSize, cv::Scalar textColor, bool fontItalic, bool fontBold)
{
    // Create Cairo
    cairo_surface_t* surface =
            cairo_image_surface_create(
                CAIRO_FORMAT_ARGB32,
                targetImage.cols,
                targetImage.rows);

cairo_t* cairo = cairo_create(surface);

// Wrap Cairo with a Mat
    cv::Mat cairoTarget(
                cairo_image_surface_get_height(surface),
                cairo_image_surface_get_width(surface),
                CV_8UC4,
                cairo_image_surface_get_data(surface),
                cairo_image_surface_get_stride(surface));

// Put image onto Cairo
    cv::cvtColor(targetImage, cairoTarget, cv::COLOR_BGR2BGRA);

// Set font and write text
    cairo_select_font_face (
                cairo,
                fontFace.c_str(),
                fontItalic ? CAIRO_FONT_SLANT_ITALIC : CAIRO_FONT_SLANT_NORMAL,
                fontBold ? CAIRO_FONT_WEIGHT_BOLD : CAIRO_FONT_WEIGHT_NORMAL);

cairo_set_font_size(cairo, fontSize);
    cairo_set_source_rgb(cairo, textColor[2], textColor[1], textColor[0]);

cairo_text_extents_t extents;
    cairo_text_extents(cairo, text.c_str(), &extents);

cairo_move_to(
                cairo,
                centerPoint.x - extents.width/2 - extents.x_bearing,
                centerPoint.y - extents.height/2- extents.y_bearing);
    cairo_show_text(cairo, text.c_str());

// Copy the data to the output image
    cv::cvtColor(cairoTarget, targetImage, cv::COLOR_BGRA2BGR);

cairo_destroy(cairo);
    cairo_surface_destroy(surface);
}

int main()
{
    
    CvFont font;
    float p = 2.0;
    std::string const msg = "水印文字";
    CvScalar scalar(15, 0.5, 0.5, 0);

ifstream in("./all.list");
    string filename;
    string line;
    if(in) // 有该文件
    {
        while (getline (in, line)) // line中不包括每行的换行符
        {
            cout << line << endl;
            cv::VideoCapture capture(line);
            if (!capture.isOpened())  
            {
                std::cout << "Read video Failed !" << std::endl;
                return -1;
            }

int nFrame = capture.get(cv::CAP_PROP_FRAME_COUNT);
            double rate=capture.get(CV_CAP_PROP_FPS);
            //double rate = 15.0;
            Size videoSize(capture.get(CV_CAP_PROP_FRAME_WIDTH), capture.get(CV_CAP_PROP_FRAME_HEIGHT));
            cout << nFrame<< " "<< rate << " "<< videoSize <<endl;
            VideoWriter writer( line + "add_tag.flv", CV_FOURCC('F','L','V','1'), rate, videoSize);
            Mat frame;
            while (true)
            {
                capture >> frame;
                if(frame.empty()) break;
                double size = (double)MIN(30, 30);
            
                putTextCairo(frame, msg, Point(300, 30), "/hard_disk2/Receive_files/lj/transfor_video/Font/msyh.ttf", size, Scalar(0,0,255), false, false);

writer << frame;
                imshow("video", frame);
                if (waitKey(20) == 27)//esc
                {
                    break;
                }
            
            }
        }
    }

return 1;
}

上一篇:Netty 中 LengthFieldBasedFrameDecoder 构造函数取值备忘


下一篇:Tornado框架配置使用Jinja2模板引擎