错误:“filesystem“ 不是 “std“ 的成员_std没有成员filesystem-优快云博客
实现在C:\A0fangfang\shiyan\9.2 路径下,分别创建"rgb1"、"rgb2"、"ir1"、"ir2"、"depth1"、"depth2"6个文件夹、用于保存相机获取的"rgb1"、"rgb2"、"ir1"、"ir2"、"depth1"、"depth2"图片,程序开始运行时,每5秒保存一次,保存50次后停止代码运行。请完整输出修改后的代码。
#include <iostream>
#include <stdio.h>
#include <iomanip>
#include <time.h>
#include <signal.h>
#include <opencv2/opencv.hpp>
#include <filesystem>
# include <libfreenect2/libfreenect2.hpp>
# include <libfreenect2/frame_listener_impl.h>
# include <libfreenect2/registration.h>
# include <libfreenect2/packet_pipeline.h>
# include <libfreenect2/logger.h>
namespace fs = std::filesystem;
using namespace std;
using namespace cv;
using namespace libfreenect2;
enum
{
Processor_cl,
Processor_gl,
Processor_cpu
};
bool protonect_shutdown = false; // Whether the running application should shut down.
int save_count = 0; // 计数器,记录保存次数
const int save_limit = 50; // 保存次数上限
const int save_interval = 5000; // 保存间隔,单位毫秒
void sigint_handler(int s)
{
protonect_shutdown = true;
}
int main()
{
// Define variables
std::cout << "Hello World!" << std::endl;
Freenect2 freenect2;
vector<Freenect2Device*> devices;
vector<PacketPipeline*> pipelines;
vector<SyncMultiFrameListener*> listeners;
vector<FrameMap> frames;
// Search and initialize sensors
int numDevices = freenect2.enumerateDevices();
if (numDevices < 2)
{
cerr << "Error: Not enough Kinect devices connected!" << endl;
return -1;
}
// Configure transfer format
int depthProcessor = Processor_cl;
for (int i = 0; i < 2; ++i)
{
string serial = freenect2.getDeviceSerialNumber(i);
cout << "SERIAL " << i + 1 << ": " << serial << endl;
PacketPipeline* pipeline = nullptr;
if (depthProcessor == Processor_cpu)
{
pipeline = new CpuPacketPipeline();
}
else if (depthProcessor == Processor_gl) // if support gl
{
# ifdef LIBFREENECT2_WITH_OPENGL_SUPPORT
pipeline = new OpenGLPacketPipeline();
# else
cout << "OpenGL pipeline is not supported!" << endl;
# endif
}
else if (depthProcessor == Processor_cl) // if support cl
{
# ifdef LIBFREENECT2_WITH_OPENCL_SUPPORT
pipeline = new OpenCLPacketPipeline();
# else
cout << "OpenCL pipeline is not supported!" << endl;
# endif
}
if (pipeline)
{
Freenect2Device* dev = freenect2.openDevice(serial, pipeline);
if (dev == nullptr)
{
cerr << "Failure opening device " << i + 1 << "!" << endl;
return -1;
}
devices.push_back(dev);
pipelines.push_back(pipeline);
SyncMultiFrameListener* listener = new SyncMultiFrameListener(
Frame::Color |
Frame::Depth |
Frame::Ir);
listeners.push_back(listener);
frames.push_back(FrameMap());
dev->setColorFrameListener(listener);
dev->setIrAndDepthFrameListener(listener);
dev->start();
}
}
signal(SIGINT, sigint_handler);
protonect_shutdown = false;
// Create windows
namedWindow("rgb1", WND_PROP_ASPECT_RATIO);
namedWindow("rgb2", WND_PROP_ASPECT_RATIO);
namedWindow("ir1", WND_PROP_ASPECT_RATIO);
namedWindow("ir2", WND_PROP_ASPECT_RATIO);
namedWindow("depth1", WND_PROP_ASPECT_RATIO);
namedWindow("depth2", WND_PROP_ASPECT_RATIO);
// Create directories for saving images
string base_path = "C:\\A0fangfang\\shiyan\\9.2\\";
vector<string> folder_names = {"rgb1", "rgb2", "ir1", "ir2", "depth1", "depth2"};
for (const auto& folder_name : folder_names)
{
fs::create_directory(base_path + folder_name);
}
// Loop to receive frames
while (!protonect_shutdown && save_count < save_limit)
{
for (size_t i = 0; i < devices.size(); ++i)
{
listeners[i]->waitForNewFrame(frames[i]);
Frame* rgb = frames[i][Frame::Color];
Frame* ir = frames[i][Frame::Ir];
Frame* depth = frames[i][Frame::Depth];
Mat rgbmat(rgb->height, rgb->width, CV_8UC4, rgb->data);
Mat irmat(ir->height, ir->width, CV_32FC1, ir->data);
Mat depthmat(depth->height, depth->width, CV_32FC1, depth->data);
imshow("rgb" + to_string(i + 1), rgbmat);
imshow("ir" + to_string(i + 1), irmat / 4500.0f);
imshow("depth" + to_string(i + 1), depthmat / 4500.0f);
// Save images every 5 seconds
static clock_t last_save_time = 0;
if ((clock() - last_save_time) / CLOCKS_PER_SEC >= save_interval / 1000)
{
last_save_time = clock();
string timestamp = to_string(time(nullptr));
imwrite(base_path + "rgb" + to_string(i + 1) + "\\" + timestamp + ".png", rgbmat);
imwrite(base_path + "ir" + to_string(i + 1) + "\\" + timestamp + ".png", irmat);
imwrite(base_path + "depth" + to_string(i + 1) + "\\" + timestamp + ".png", depthmat);
save_count++; // Increment save count
if (save_count >= save_limit)
{
protonect_shutdown = true; // Stop the loop after reaching the save limit
}
}
listeners[i]->release(frames[i]);
}
int key = waitKey(1);
protonect_shutdown = protonect_shutdown || (key > 0 && ((key & 0xFF) == 27)); // shutdown on escape
}
// Close devices
for (auto dev : devices)
{
dev->stop();
dev->close();
}
// Clean up resources
for (auto pipeline : pipelines)
{
delete pipeline;
}
for (auto listener : listeners)
{
delete listener;
}
std::cout << "Goodbye World!" << std::endl;
return 0;
}
vs2015版本里 没有namespace fs = std::filesystem;