C++读取文件夹下的子目录路径,以及读取文件夹下的图片并合并成视频

//读取当前目录下的一级目录,并存放到容器中

#include <iostream>
#include <vector>
#include <string>
#include <windows.h>

void getDirectoryNames(const std::string& folderPath, std::vector<std::string>& directoryNames)
{
    std::string searchPattern = folderPath + "\\*";

    WIN32_FIND_DATAA findData;
    HANDLE hFind = FindFirstFileA(searchPattern.c_str(), &findData);

    if (hFind != INVALID_HANDLE_VALUE)
    {
        do
        {
            std::string entryName = findData.cFileName;

            if ((findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && entryName != "." && entryName != "..")
            {
                directoryNames.push_back(folderPath + "/" + entryName);
            }
        } while (FindNextFileA(hFind, &findData));

        FindClose(hFind);
    }
}

int main()
{
    std::vector<std::string> directoryNames;
   

    std::string folderPath = "./1";

    getDirectoryNames(folderPath, directoryNames);
    
    // 打印目录名称
    for (const auto& directoryName : directoryNames)
    {
        std::cout << directoryName << std::endl;
    }

    return 0;
}
#include <iostream>
#include <opencv2/opencv.hpp>

void processImage(const std::string& imagePath, cv::VideoWriter& videoWriter, const cv::Size& targetSize) {
    cv::Mat image = cv::imread(imagePath);

    // 调整图片大小
    cv::resize(image, image, targetSize);

    videoWriter.write(image);
}

void processFolder(const std::string& folderPath, cv::VideoWriter& videoWriter, const cv::Size& targetSize) {
    cv::String pattern = folderPath + "/*.bmp"; // 匹配 BMP 格式的图片
    std::vector<cv::String> imagePaths;
    cv::glob(pattern, imagePaths);

    for (const auto& imagePath : imagePaths) {
        processImage(imagePath, videoWriter, targetSize);
    }
}

int main() {
    // 设置输入文件夹路径和输出视频文件名
    std::string inputFolder = "path/to/input/folder/";
    std::string outputVideo = "output_video.avi";

    // 设置目标图片大小
    cv::Size targetSize(640, 480);

    // 创建输出视频编写器
    cv::VideoWriter videoWriter(outputVideo, cv::VideoWriter::fourcc('M', 'J', 'P', 'G'), 10, targetSize);

    // 检查是否成功创建输出视频编写器
    if (!videoWriter.isOpened()) {
        std::cout << "无法创建输出视频编写器!" << std::endl;
        return 1;
    }

    // 处理文件夹中的图片
    processFolder(inputFolder, videoWriter, targetSize);

    // 释放资源
    videoWriter.release();

    // 使用VideoCapture打开输出视频文件
    cv::VideoCapture videoCapture(outputVideo);

    // 检查是否成功打开视频文件
    if (!videoCapture.isOpened()) {
        std::cout << "无法打开视频文件!" << std::endl;
        return 1;
    }

    // 逐帧读取视频并显示
    cv::Mat frame;
    while (videoCapture.read(frame)) {
        cv::imshow("Video", frame);
        cv::waitKey(1);  // 1毫秒的延迟,用于显示视频
    }
    //while (true) {
            //    cap.read(frame); // 读取视频帧
            //    if (frame.empty()) {
            //        // 处理视频帧读取完毕的情况
            //        break;
            //    }

            //    writer.write(frame); // 写入视频帧

            //    cv::imshow("Video", frame); // 显示视频帧
            //    if (cv::waitKey(30) == 27) { // 按下Esc键退出循环
            //        break;
            //    }
            //}

    // 释放资源
    videoCapture.release();

    std::cout << "视频播放完成!" << std::endl;

    return 0;
}







读取给定路径下的二级目录路径

#include <iostream>
#include <vector>
#include <string>
#include <windows.h>

void traverseDirectories( const std::string& directoryPath, std::vector<std::string>& paths) {
    std::string searchPattern = directoryPath + "\\*";

    WIN32_FIND_DATAA findData;
    HANDLE hFind = FindFirstFileA(searchPattern.c_str(), &findData);
 
    if (hFind != INVALID_HANDLE_VALUE) {
        do {
            std::string entryName = findData.cFileName;
            if ((findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && entryName != "." && entryName != "..") {
                std::string subDirectoryPath = directoryPath + "\\" + entryName;
                                                           
                //traverseDirectories(subDirectoryPath, paths);  // 递归遍历子目录
                
                paths.push_back(subDirectoryPath); 
                                           
            }
        } while (FindNextFileA(hFind, &findData));

        FindClose(hFind);
    }
}

int main() {
    std::string rootDirectory = ".\\1";
    std::vector<std::string> oneDirectoryPaths;
    std::vector<std::string> finalDirectoryPaths;


    traverseDirectories(rootDirectory, oneDirectoryPaths);


    // 打印所有子目录路径
    for (const auto& path : oneDirectoryPaths) {
        traverseDirectories(path, finalDirectoryPaths);
    }
    for (const auto& path : finalDirectoryPaths) {
        std::cout << path << std::endl;
    }

    return 0;
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值