//读取当前目录下的一级目录,并存放到容器中
#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;
}