Opencv将目录下的图片存储为视频

2023-05-16

文章目录

  • 源码
  • 编译
  • 运行

源码

/*************************************************************************
    > File Name: main.cpp
    > Author: 
    > Mail: 1@163.com 
    > Created Time: 2022年06月17日 星期五 14时00分37秒
    > g++ main.cpp -o main `pkg-config --cflags --libs opencv`
 ************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <unistd.h>
#include <iostream>
#include <vector>
#include <libgen.h>

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

using namespace cv;  
using namespace std;

vector <string> relativePath;
vector <string> fileName;


/******************************************************************************
 *
 * 功能:
 *		获得 (文件名.后缀) 和 (文件名) 和 (文件后缀),Windows和Linux通用!
 *
 * 参数:
 *		_filePaht:			C语言字符指针,文件路径字符串
 *		_fullFileName:		C++字符串引用,获得[(文件名.后缀) 或 (文件名)]返回
 *		_fileName:			C++字符串引用,获得(文件名.后缀)返回
 *		_fileSuffix:		C++字符串引用,获得(后缀)返回
 *		_dot:				true,后缀带点(.txt);false,后缀不带点(txt);默认值为true
 *
 * 返回值:
 *		有后缀返回"."的索引;没有后缀则返回-1
 *
 ******************************************************************************/
int get_fileName_fileSuffix(const char *_filePaht, std::string &_fullFileName, std::string &_fileName, std::string &_fileSuffix, bool _dot = true) {

	// 合法性检查
	if (!_filePaht) return -1;
	std::string str = _filePaht;
	if (str.empty()) return -1;

	// .txt    的情况
	if (str.at(0) == '.') return -1;
	// file.   的情况
	if (str.at(str.size() - 1) == '.') return -1;



	char c = '\0';

	// 区分此函数是在Windows环境调用还是Linux环境调用
#if defined (_WIN64) || defined (WIN32) || defined (_WIN32)
	//printf("---Windows---\n");
	c = '\\';
#else
	//printf("---Linux---\n");
	c = '\/';
#endif

	// 去除字符串中的路径,剩下文件名
	std::string filename = strrchr(_filePaht, c) == NULL ? "" : strrchr(_filePaht, c) + 1;
	if (filename == "") {
		/* 说明字符串是file.txt或者file形式 */
		filename = _filePaht;

	} else {
		/* 说名字符串是C:\\abc\\file.txt或者C:\\abc\\file形式 */
	}

	// 找到 . 的位置
	size_t _size = filename.rfind(".");
	// 获得文件名,不包含后缀
	_fileName = filename.substr(0, _size);

	// 获得文件后缀
	std::string strsuffix = "";
	if (_size != -1) {	// 不等于-1说明有后缀 file.txt形式

		// true后缀带点,false后缀不带点
		if (_dot == true) {
			strsuffix = filename.substr(_size);     // 文件后缀 .txt

		} else {
			strsuffix = filename.substr(_size + 1);     // 文件后缀 txt
		}
	}
	// 后缀
	_fileSuffix = strsuffix;

	// 获得文件名.后缀(file.txt | file)
	_fullFileName = filename;

	return _size;
}

int readFileList(char *basePath)
{
    DIR *dir;
    struct dirent *ptr;
    char base[1000];

    if ((dir=opendir(basePath)) == NULL)
    {
        perror("Open dir error...");
        exit(1);
    }

    while ((ptr=readdir(dir)) != NULL)
    {
        if(strcmp(ptr->d_name,".")==0 || strcmp(ptr->d_name,"..")==0)    ///current dir OR parrent dir
            continue;
        else if(ptr->d_type == 8){    ///file
            printf("d_name:%s/%s\n",basePath,ptr->d_name);
            // 文件名
            string s1 = ptr->d_name;
            fileName.push_back(s1);
            // 全路径
            string s = basePath;
            s = s + "/";
            s = s + +ptr->d_name;
            relativePath.push_back(s);
            
            std::string fullFilename, filename, fileSuffix;
	          int r = get_fileName_fileSuffix(s1.c_str(), fullFilename, filename, fileSuffix, true);
	         
        }else if(ptr->d_type == 10){    ///link file
            printf("d_name:%s/%s\n",basePath,ptr->d_name);
        }else if(ptr->d_type == 4)    ///dir
        {
            memset(base,'\0',sizeof(base));
            strcpy(base,basePath);
            strcat(base,"/");
            strcat(base,ptr->d_name);
            readFileList(base);
        }
    }
    closedir(dir);
    return 1;
}

int main(int argc, const char *argv[])
{
    DIR *dir;
    #if 0
    char basePath[1000];
    memset(basePath,'\0',sizeof(basePath));
    // 得到当前的绝对路径
    getcwd(basePath, 999);
    #else
    char *basePath = (char *)argv[1];
    #endif
    
    // 获取目录下的全部文件
    //readFileList(basePath);
    
    
  VideoWriter w1;
  int encode_type = VideoWriter::fourcc('M', 'J', 'P', 'G'); 
 	bool isColor = true; 
	double fps = 25.0;
	string filename = "test_save.avi";
	w1.open(filename, encode_type, fps, cv::Size(1280, 720), isColor);  
 
	if (!w1.isOpened()) {     
		cout << "视频打开失败" << endl;
	}


    int id = 0;
    int maxNum = 300;
    for (int i = 0; i<maxNum; i++)
    {
      string filePathName = argv[1] ;
      filePathName = filePathName+ "/" + to_string(i) + ".png";

      if (! access(filePathName.c_str() ,0) )
      {
          Mat img;
          img = imread(filePathName.c_str());  
          if (img.empty())  //判断图像文件是否存在
          {
            cout << "读取失败" << endl;
            if (w1.isOpened())
              w1.release();
            return -1;
          }
          w1.write(img);
          imshow("test", img);  //显示图像
          waitKey(0); 
      }
    
    }
    return 0;
}

编译

 g++ img2video.cpp -o img2video `pkg-config --cflags --libs opencv`

运行

./img2video  ../results
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Opencv将目录下的图片存储为视频 的相关文章

随机推荐

  • VS2008中Unicode编码转UTF-8编码

    今天在pc客户端传数据 Json格式 给服务端时 在存储到数据库 Mysql 时总是出现乱码现象 xff0c 使用的是http协议 xff0c 以及json的解码 后一篇说明 这里我们先说下转码 在vs2008中编码方式有Unicode编码
  • 串口显示乱码的原因总结

    分享一下我老师大神的人工智能教程 xff01 零基础 xff0c 通俗易懂 xff01 http blog csdn net jiangjunshow 也欢迎大家转载本篇文章 分享知识 xff0c 造福人民 xff0c 实现我们中华民族伟大
  • Linux出现Input/output error

    分享一下我老师大神的人工智能教程 xff01 零基础 xff0c 通俗易懂 xff01 http blog csdn net jiangjunshow 也欢迎大家转载本篇文章 分享知识 xff0c 造福人民 xff0c 实现我们中华民族伟大
  • 错误 expected '}' before ' ' token

    分享一下我老师大神的人工智能教程 xff01 零基础 xff0c 通俗易懂 xff01 http blog csdn net jiangjunshow 也欢迎大家转载本篇文章 分享知识 xff0c 造福人民 xff0c 实现我们中华民族伟大
  • 浅谈C语言中的布尔(bool)类型

    分享一下我老师大神的人工智能教程 xff01 零基础 xff0c 通俗易懂 xff01 http blog csdn net jiangjunshow 也欢迎大家转载本篇文章 分享知识 xff0c 造福人民 xff0c 实现我们中华民族伟大
  • C++类成员空间分配和虚函数表

    C 43 43 类成员空间分配和虚函数表 xff0c 非常详细的一篇文章 xff0c 受益匪浅 https www cnblogs com secondtonone1 p 7205108 html utm source 61 itdadao
  • 外部时钟与内部时钟区别 作用 使用条件

    分享一下我老师大神的人工智能教程 xff01 零基础 xff0c 通俗易懂 xff01 http blog csdn net jiangjunshow 也欢迎大家转载本篇文章 分享知识 xff0c 造福人民 xff0c 实现我们中华民族伟大
  • Ghostscript的介绍和移植

    关于ghostscript 以下简称gs Gs是一个地下工作者 xff0c 一般用户不熟悉它 xff0c 因为它上不和用户直接打交道 xff0c 下不直接接触打印机 但是在打印工作中它却扮演了极为重要的解色 一般从用户常见文件如图片或者wo
  • PPPoE on Android

    分享一下我老师大神的人工智能教程 零基础 xff01 通俗易懂 xff01 风趣幽默 xff01 还带黄段子 xff01 希望你也加入到我们人工智能的队伍中来 xff01 https blog csdn net jiangjunshow P
  • 文件编程 创建目录mkdir 函数

    分享一下我老师大神的人工智能教程 零基础 xff01 通俗易懂 xff01 风趣幽默 xff01 还带黄段子 xff01 希望你也加入到我们人工智能的队伍中来 xff01 https blog csdn net jiangjunshow L
  • VFY unable to resolve virtual method Landroid/support/v4/

    分享一下我老师大神的人工智能教程 零基础 xff01 通俗易懂 xff01 风趣幽默 xff01 还带黄段子 xff01 希望你也加入到我们人工智能的队伍中来 xff01 https blog csdn net jiangjunshow 使
  • CUPS移植记录

    分享一下我老师大神的人工智能教程 零基础 xff01 通俗易懂 xff01 风趣幽默 xff01 还带黄段子 xff01 希望你也加入到我们人工智能的队伍中来 xff01 https blog csdn net jiangjunshow C
  • gazebo多机器人仿真和move_base,diff_drive等gazebo包配置

    gazebo中的多机器人仿真指的是在gazebo中加入多个urdf xff0c 并控制机器人运动 下面是我总结的一些名称的命名规律 1 joint名称 在spawn model中 xff0c 通过 model model name添加参数
  • Golang语言移植-ARM开发环境搭建

    开发环境介绍 主机操作系统 xff1a Ubuntu14 04 64位目标平台 xff1a IMX 6Q ARM Cortex A9内核交叉工具链 xff1a arm poky linux gnueabi xff0c gcc4 9 1Go版
  • #每周一篇论文4#[感知]毫米波雷达与摄像头联合标定

    文章目录 源代码下载系列其他资源一 坐标系说明1 1 摄像头相关坐标系1 2 毫米波雷达相关坐标系1 3 GPS相关坐标系1 4 车体相关坐标系1 5 其他坐标系1 5 1 左右手坐标系 二 毫米波和摄像机联合标定2 1 毫米波 O X r
  • 单目测距 视觉测距

    文章目录 单目测距在kitti数据集中的测试结果C 43 43 工程原理代码注释 其他视觉测距算法 基于相似三角形的单目测距算法原理代码 参考资料 单目测距 在kitti数据集中的测试结果 C 43 43 工程 C 43 43 工程代码下载
  • python json 解析

    coding utf 8 import sys os re class JsonBaseType single type 61 0 object type 61 1 array type 61 2 class ParseException
  • proto_cmake_test

    proto cmake test Proto与CMAKE结合编译源代码 工程编译 span class token function cd span build cmake span class token punctuation span
  • Opencv获取指定时间内的视频片段以及帧

    文章目录 源码编译运行 源码 span class token comment gt File Name ddd cpp gt Author gt Mail 1 64 163 com gt Created Time 2022年06月17日
  • Opencv将目录下的图片存储为视频

    文章目录 源码编译运行 源码 span class token comment gt File Name main cpp gt Author gt Mail 1 64 163 com gt Created Time 2022年06月17日