OpenCV (c++): 写视频文件

目录

获取图片名称

图片名称排序

图片名称分隔

字符串转数

从小到大排序

写视频文件

报错

修改


使用图片如下:width: 640, height: 368

获取图片名称

使用 c++调系统命令-popen方法

这里我们写成函数形式,获取./images下面的图片名称

#include <iostream>
#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

vector<std::string> getFiles(std::string cmdStr)
{
    FILE * fp;
    char buffer[200];
    fp = popen(cmdStr.c_str(), "r");
    string line;
    vector<std::string> files;
    while (fgets(buffer, sizeof(buffer), fp))
    {
        line = buffer;
        files.push_back(line.substr(0,line.size()-1));
    }
    pclose(fp);
    return files;
}

int main()
{
    vector<std::string> files = getFiles("ls ./images");

    cout << files.size() << endl;
    for (int i=0; i<5; i++)
    { cout << files[i] << endl; }
    
}
335
1.jpg
10.jpg
100.jpg
101.jpg
102.jpg

图片名称排序

图片名称分隔

使用 实现字符串分割

#include <iostream>
#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

std::vector<std::string> getFiles(std::string cmdStr)
{
    FILE * fp;
    char buffer[200];
    fp = popen(cmdStr.c_str(), "r");
    string line;
    vector<std::string> files;
    while (fgets(buffer, sizeof(buffer), fp))
    {
        line = buffer;
        files.push_back(line.substr(0,line.size()-1));
    }
    pclose(fp);
    return files;
}
std::vector<std::string> split(std::string str, char s)
{
    std::vector<std::string> list;
    std::string temp="";
    for (int i=0; i<str.size(); i++)
    {
        if (str[i] != s)
        { temp = temp + str[i]; }
        else
        { list.push_back(temp);
          temp = ""; }

        if (i==str.size()-1)
        { list.push_back(temp); }
    }
    return list;
}
int main()
{
    std::vector<std::string> files = getFiles("ls ./images");

    cout << files.size() << endl;
    for (int i=0; i<5; i++)
    { cout << split(files[i],'.')[0] << endl; }
    
}
335
1
10
100
101
102
字符串转数

之前我们实现了:c++模块化-实现字符串转数

c++也有相应的函数,参考:https://blog.csdn.net/m0_37316917/article/details/82712017

这里我们用c++自带的转换函数

#include <iostream>
#include <opencv2/opencv.hpp>
#include <typeinfo>

using namespace cv;
using namespace std;

std::vector<std::string> getFiles(std::string cmdStr)
{
    FILE * fp;
    char buffer[200];
    fp = popen(cmdStr.c_str(), "r");
    string line;
    vector<std::string> files;
    while (fgets(buffer, sizeof(buffer), fp))
    {
        line = buffer;
        files.push_back(line.substr(0,line.size()-1));
    }
    pclose(fp);
    return files;
}
std::vector<std::string> split(std::string str, char s)
{
    std::vector<std::string> list;
    std::string temp="";
    for (int i=0; i<str.size(); i++)
    {
        if (str[i] != s)
        { temp = temp + str[i]; }
        else
        { list.push_back(temp);
          temp = ""; }

        if (i==str.size()-1)
        { list.push_back(temp); }
    }
    return list;
}
int main()
{
    std::vector<std::string> files = getFiles("ls ./images");

    cout << typeid(split(files[0],'.')[0]).name() << endl; 
    cout << typeid(std::stoi(split(files[0],'.')[0])).name() << endl;
}
NSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEEE
i
从小到大排序
#include <iostream>
#include <opencv2/opencv.hpp>
#include <typeinfo>

using namespace cv;
using namespace std;

std::vector<std::string> getFiles(std::string cmdStr)
{
    FILE * fp;
    char buffer[200];
    fp = popen(cmdStr.c_str(), "r");
    string line;
    vector<std::string> files;
    while (fgets(buffer, sizeof(buffer), fp))
    {
        line = buffer;
        files.push_back(line.substr(0,line.size()-1));
    }
    pclose(fp);
    return files;
}
std::vector<std::string> split(std::string str, char s)
{
    std::vector<std::string> list;
    std::string temp="";
    for (int i=0; i<str.size(); i++)
    {
        if (str[i] != s)
        { temp = temp + str[i]; }
        else
        { list.push_back(temp);
          temp = ""; }

        if (i==str.size()-1)
        { list.push_back(temp); }
    }
    return list;
}
int main()
{
    std::vector<std::string> files = getFiles("ls ./images");

    std::stable_sort(files.begin(),files.end(),
                        [](string left,string right)
                        { return stoi(split(left,'.')[0]) < stoi(split(right,'.')[0]); });
    for (int i=0; i<5; i++)
    { cout << files[i] << endl; }
}
1.jpg
2.jpg
3.jpg
4.jpg
5.jpg

写视频文件

width: 640, height: 368
fps: 24

下面的演示是在mac上进行的

#include <iostream>
#include <opencv2/opencv.hpp>
#include <typeinfo>

using namespace cv;
using namespace std;

std::vector<std::string> getFiles(std::string cmdStr)
{
    FILE * fp;
    char buffer[200];
    fp = popen(cmdStr.c_str(), "r");
    string line;
    vector<std::string> files;
    while (fgets(buffer, sizeof(buffer), fp))
    {
        line = buffer;
        files.push_back(line.substr(0,line.size()-1));
    }
    pclose(fp);
    return files;
}
std::vector<std::string> split(std::string str, char s)
{
    std::vector<std::string> list;
    std::string temp="";
    for (int i=0; i<str.size(); i++)
    {
        if (str[i] != s)
        { temp = temp + str[i]; }
        else
        { list.push_back(temp);
          temp = ""; }

        if (i==str.size()-1)
        { list.push_back(temp); }
    }
    return list;
}
int main()
{
    std::vector<std::string> files = getFiles("ls ./images");

    std::stable_sort(files.begin(),files.end(),
                        [](string left,string right)
                        { return stoi(split(left,'.')[0]) < stoi(split(right,'.')[0]); });
    
    VideoWriter writer("./xx_save.mp4", cv::CAP_OPENCV_MJPEG, 24, cv::Size(640,368),true);
    Mat frame;
    for (auto name:files)
    {
        frame = imread("./images/"+name);
        writer.write(frame);
    }
    writer.release();
}
OpenCV: FFMPEG: tag 0x00000898/'�???' is not found (format 'mp4 / MP4 (MPEG-4 Part 14)')'
报错

参考:https://blog.csdn.net/qq_41204464/article/details/122470298

#include <iostream>
#include <opencv2/opencv.hpp>
#include <typeinfo>

using namespace cv;
using namespace std;

std::vector<std::string> getFiles(std::string cmdStr)
{
    FILE * fp;
    char buffer[200];
    fp = popen(cmdStr.c_str(), "r");
    string line;
    vector<std::string> files;
    while (fgets(buffer, sizeof(buffer), fp))
    {
        line = buffer;
        files.push_back(line.substr(0,line.size()-1));
    }
    pclose(fp);
    return files;
}
std::vector<std::string> split(std::string str, char s)
{
    std::vector<std::string> list;
    std::string temp="";
    for (int i=0; i<str.size(); i++)
    {
        if (str[i] != s)
        { temp = temp + str[i]; }
        else
        { list.push_back(temp);
          temp = ""; }

        if (i==str.size()-1)
        { list.push_back(temp); }
    }
    return list;
}
int main()
{
    std::vector<std::string> files = getFiles("ls ./images");

    std::stable_sort(files.begin(),files.end(),
                        [](string left,string right)
                        { return stoi(split(left,'.')[0]) < stoi(split(right,'.')[0]); });
    
    VideoWriter writer("./xx_save.mp4", -1, 24, cv::Size(640,368),true);
}
OpenCV: FFMPEG: format mp4 / MP4 (MPEG-4 Part 14)
fourcc tag 0x7634706d/'mp4v' codec_id 000C
fourcc tag 0x31637661/'avc1' codec_id 001B
fourcc tag 0x33637661/'avc3' codec_id 001B
fourcc tag 0x31766568/'hev1' codec_id 00AD
fourcc tag 0x31637668/'hvc1' codec_id 00AD
fourcc tag 0x7634706d/'mp4v' codec_id 0002
fourcc tag 0x7634706d/'mp4v' codec_id 0001
fourcc tag 0x7634706d/'mp4v' codec_id 0007
fourcc tag 0x7634706d/'mp4v' codec_id 003D
fourcc tag 0x7634706d/'mp4v' codec_id 0058
fourcc tag 0x312d6376/'vc-1' codec_id 0046
fourcc tag 0x63617264/'drac' codec_id 0074
fourcc tag 0x7634706d/'mp4v' codec_id 00A3
fourcc tag 0x39307076/'vp09' codec_id 00A7
fourcc tag 0x31307661/'av01' codec_id 801D
fourcc tag 0x6134706d/'mp4a' codec_id 15002
fourcc tag 0x63616c61/'alac' codec_id 15010
fourcc tag 0x6134706d/'mp4a' codec_id 1502D
fourcc tag 0x6134706d/'mp4a' codec_id 15001
fourcc tag 0x6134706d/'mp4a' codec_id 15000
fourcc tag 0x332d6361/'ac-3' codec_id 15003
fourcc tag 0x332d6365/'ec-3' codec_id 15028
fourcc tag 0x6134706d/'mp4a' codec_id 15004
fourcc tag 0x61706c6d/'mlpa' codec_id 1502C
fourcc tag 0x43614c66/'fLaC' codec_id 1500C
fourcc tag 0x7375704f/'Opus' codec_id 1503C
fourcc tag 0x6134706d/'mp4a' codec_id 15005
fourcc tag 0x6134706d/'mp4a' codec_id 15018
fourcc tag 0x6134706d/'mp4a' codec_id 15803
fourcc tag 0x7334706d/'mp4s' codec_id 17000
fourcc tag 0x67337874/'tx3g' codec_id 17005
fourcc tag 0x646d7067/'gpmd' codec_id 18807
fourcc tag 0x316d686d/'mhm1' codec_id 15817

下面选了:0x7634706d/'mp4v'

修改
#include <iostream>
#include <opencv2/opencv.hpp>
#include <typeinfo>

using namespace cv;
using namespace std;

std::vector<std::string> getFiles(std::string cmdStr)
{
    FILE * fp;
    char buffer[200];
    fp = popen(cmdStr.c_str(), "r");
    string line;
    vector<std::string> files;
    while (fgets(buffer, sizeof(buffer), fp))
    {
        line = buffer;
        files.push_back(line.substr(0,line.size()-1));
    }
    pclose(fp);
    return files;
}
std::vector<std::string> split(std::string str, char s)
{
    std::vector<std::string> list;
    std::string temp="";
    for (int i=0; i<str.size(); i++)
    {
        if (str[i] != s)
        { temp = temp + str[i]; }
        else
        { list.push_back(temp);
          temp = ""; }

        if (i==str.size()-1)
        { list.push_back(temp); }
    }
    return list;
}
int main()
{
    std::vector<std::string> files = getFiles("ls ./images");

    std::stable_sort(files.begin(),files.end(),
                        [](string left,string right)
                        { return stoi(split(left,'.')[0]) < stoi(split(right,'.')[0]); });
    
    VideoWriter writer("./xx_save.mp4", 0x7634706d, 24, cv::Size(640,368),true);
    Mat frame;
    for (auto name:files)
    {
        frame = imread("./images/"+name);
        writer.write(frame);
    }
    writer.release();
}

  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值