zoukankan      html  css  js  c++  java
  • 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;
    }


  • 相关阅读:
    EasyRMS录播管理服务器项目实战:windows上开机自启动NodeJS服务
    EasyPlayer RTSP Windows(with ActiveX/OCX插件)播放器支持H.265播放与抓图功能
    EasyPlayer RTSP Windows(with ActiveX/OCX插件)播放器支持H.265播放与抓图功能
    将EasyRTMP_RTSP移植到Android平台实现的RTSP拉流转推RTMP直播流功能
    将EasyRTMP_RTSP移植到Android平台实现的RTSP拉流转推RTMP直播流功能
    EasyDarwin如何支持点播和RTMP/HLS直播?EasyDSS!
    EasyDarwin如何支持点播和RTMP/HLS直播?EasyDSS!
    EasyCMS在幼儿园视频直播项目实战中以redis操作池的方式应对高并发的redis操作问题
    EasyCMS在幼儿园视频直播项目实战中以redis操作池的方式应对高并发的redis操作问题
    EasyDarwin开源流媒体服务器中一种实现对作用域内new对象自动释放的方法(值得借鉴)
  • 原文地址:https://www.cnblogs.com/rainsoul/p/8042938.html
Copyright © 2011-2022 走看看