From 5009832b3a0eb6046626d64b7f5ac16a73624eff Mon Sep 17 00:00:00 2001 From: tangchao0503 <735056338@qq.com> Date: Thu, 9 Apr 2026 10:28:40 +0800 Subject: [PATCH] =?UTF-8?q?add=201=E3=80=81=E9=87=87=E9=9B=86=E6=B7=B1?= =?UTF-8?q?=E5=BA=A6=E3=80=81=E5=BD=A9=E8=89=B2=E3=80=81rgb=E7=82=B9?= =?UTF-8?q?=E4=BA=91=EF=BC=8C=E5=B9=B6=E5=AD=98=E5=82=A8=E5=88=B0=E6=96=87?= =?UTF-8?q?=E4=BB=B6=EF=BC=9B=202=E3=80=81=E7=95=8C=E9=9D=A2=E6=93=8D?= =?UTF-8?q?=E4=BD=9C=E9=80=BB=E8=BE=91=E6=8E=A7=E5=88=B6=EF=BC=8C=E9=98=B2?= =?UTF-8?q?=E6=AD=A2=E7=94=A8=E6=88=B7=E9=94=99=E8=AF=AF=E6=93=8D=E4=BD=9C?= =?UTF-8?q?=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HPPA/DepthCameraWindow.cpp | 215 +++++++++++++++++++++++++++++++++++- HPPA/DepthCameraWindow.h | 57 +++++++++- HPPA/HPPA.vcxproj | 6 +- HPPA/RgbCameraOperation.cpp | 4 +- HPPA/RgbCameraOperation.h | 3 +- HPPA/rgbCameraWindow.cpp | 2 +- 6 files changed, 275 insertions(+), 12 deletions(-) diff --git a/HPPA/DepthCameraWindow.cpp b/HPPA/DepthCameraWindow.cpp index 58fad2a..3b3ba34 100644 --- a/HPPA/DepthCameraWindow.cpp +++ b/HPPA/DepthCameraWindow.cpp @@ -1,24 +1,233 @@ -#include "DepthCameraWindow.h" +#include "DepthCameraWindow.h" DepthCameraWindow::DepthCameraWindow(QWidget* parent) : QDialog(parent) { ui.setupUi(this); - connect(ui.openDepthCamera_btn, &QPushButton::clicked, this, &DepthCameraWindow::openDepthCamera); - connect(ui.closeDepthCamera_btn, &QPushButton::clicked, this, &DepthCameraWindow::closeDepthCamera); + m_DepthCameraThread = new QThread(); + m_DepthCameraOperation = new DepthCameraOperation(); + m_DepthCameraOperation->moveToThread(m_DepthCameraThread); + m_DepthCameraThread->start(); + + connect(ui.openDepthCamera_btn, &QPushButton::clicked, this, &DepthCameraWindow::openDepthCamera); + connect(ui.closeDepthCamera_btn, &QPushButton::clicked, this, &DepthCameraWindow::closeDepthCamera); + + connect(this, &DepthCameraWindow::openDepthCameraSignal, m_DepthCameraOperation, &DepthCameraOperation::OpenDepthCamera); + + connect(m_DepthCameraOperation, &DepthCameraOperation::CamOpenedSignal, this, &DepthCameraWindow::onCamOpened); + connect(m_DepthCameraOperation, &DepthCameraOperation::CamClosedSignal, this, &DepthCameraWindow::onCamClosed); } DepthCameraWindow::~DepthCameraWindow() { + m_DepthCameraThread->quit(); + m_DepthCameraThread->wait(); + delete m_DepthCameraOperation; + m_DepthCameraOperation = nullptr; } void DepthCameraWindow::openDepthCamera() { + if (!m_DepthCameraOperation->getRecordStatus()) + { + emit openDepthCameraSignal(); + } +} +void DepthCameraWindow::onCamOpened() +{ + ui.openDepthCamera_btn->setEnabled(false); + ui.closeDepthCamera_btn->setEnabled(true); + + ui.openDepthCamera_btn->setText(QString::fromLocal8Bit("已打开")); } void DepthCameraWindow::closeDepthCamera() +{ + m_DepthCameraOperation->CloseDepthCamera(); +} + +void DepthCameraWindow::onCamClosed() +{ + ui.openDepthCamera_btn->setEnabled(true); + ui.closeDepthCamera_btn->setEnabled(false); + + ui.openDepthCamera_btn->setText(QString::fromLocal8Bit("打 开")); +} + +//------------------------------------------------------------------------------------------------------------------------------- +DepthCameraOperation::DepthCameraOperation() +{ + m_pipe = nullptr; + m_func = nullptr; + record = false; +} + +DepthCameraOperation::~DepthCameraOperation() +{ + if (m_pipe) + { + m_pipe->stop(); + + delete m_pipe; + m_pipe = nullptr; + } +} + +void DepthCameraOperation::OpenDepthCamera() +{ + if (m_pipe) + { + return; + } + m_pipe = new ob::Pipeline(); + + std::shared_ptr config = std::make_shared(); + + // Get device from pipeline. + auto device = m_pipe->getDevice(); + auto devInfo = device->getDeviceInfo(); + auto pid = devInfo->getPid(); + auto vid = devInfo->getVid(); + + //// Get sensorList from device. + //auto sensorList = device->getSensorList(); + + //for (uint32_t index = 0; index < sensorList->getCount(); index++) { + // // Query all supported infrared sensor type and enable the infrared stream. + // // For dual infrared device, enable the left and right infrared streams. + // // For single infrared device, enable the infrared stream. + // OBSensorType sensorType = sensorList->getSensorType(index); + // std::cout << "Supported Sensor type: " << sensorType << std::endl; + + // // Enable the stream for the sensor type. + // config->enableStream(sensorType); + //} + + config->enableVideoStream(OB_STREAM_DEPTH, 640, 480, 15, OB_FORMAT_Y16); + config->enableVideoStream(OB_STREAM_COLOR, 640, 480, 15, OB_FORMAT_YUYV); + config->setFrameAggregateOutputMode(OB_FRAME_AGGREGATE_OUTPUT_ALL_TYPE_FRAME_REQUIRE); + config->setAlignMode(ALIGN_D2C_HW_MODE); + + m_pipe->enableFrameSync(); + + // Create a format converter filter. + auto formatConverter = std::make_shared(); + + m_pipe->start(config); + + // Drop several frames + for (int i = 0; i < 15; ++i) { + auto lost = m_pipe->waitForFrameset(100); + } + + auto pointCloud = std::make_shared(); + + int frameIndex = 0; + record = true; + QString fileNamePrefix = AppSettings::instance().dataFolder() + QDir::separator() + AppSettings::instance().fileName(); + while (record) + { + if(frameIndex==0) + { + emit CamOpenedSignal(); + std::cout << "Start recording..." << std::endl; + } + + auto frameSet = m_pipe->waitForFrameset(100); + if (frameSet == nullptr) + { + std::cout << "No frames received in 100ms..." << std::endl; + continue; + } + + // Get the depth and color frames. + auto depthFrame = frameSet->getFrame(OB_FRAME_DEPTH)->as(); + auto colorFrame = frameSet->getFrame(OB_FRAME_COLOR)->as(); + + // Convert the color frame to RGB format. + if (colorFrame->format() != OB_FORMAT_RGB) { + if (colorFrame->format() == OB_FORMAT_MJPG) { + formatConverter->setFormatConvertType(FORMAT_MJPG_TO_RGB); + } + else if (colorFrame->format() == OB_FORMAT_UYVY) { + formatConverter->setFormatConvertType(FORMAT_UYVY_TO_RGB); + } + else if (colorFrame->format() == OB_FORMAT_YUYV) { + formatConverter->setFormatConvertType(FORMAT_YUYV_TO_RGB); + } + else { + std::cout << "Color format is not support!" << std::endl; + continue; + } + colorFrame = formatConverter->process(colorFrame)->as(); + } + // Processed the color frames to BGR format, use OpenCV to save to disk. + formatConverter->setFormatConvertType(FORMAT_RGB_TO_BGR); + colorFrame = formatConverter->process(colorFrame)->as(); + + saveDepthFrame(depthFrame, frameIndex, fileNamePrefix.toStdString()); + saveColorFrame(colorFrame, frameIndex, fileNamePrefix.toStdString()); + + pointCloud->setCreatePointFormat(OB_FORMAT_RGB_POINT); + std::shared_ptr frame = pointCloud->process(frameSet); + + QString plyPath = fileNamePrefix + "_"+ QString::number(frameIndex) + ".ply"; + ob::PointCloudHelper::savePointcloudToPly(plyPath.toStdString().c_str(), frame, false, false, 50); + + frameIndex++; + } + + m_pipe->stop(); + + delete m_pipe; + m_pipe = nullptr; +} + +void DepthCameraOperation::saveDepthFrame(const std::shared_ptr depthFrame, const uint32_t frameIndex, std::string fileNamePrefix_) +{ + std::vector params; + params.push_back(cv::IMWRITE_PNG_COMPRESSION); + params.push_back(0); + params.push_back(cv::IMWRITE_PNG_STRATEGY); + params.push_back(cv::IMWRITE_PNG_STRATEGY_DEFAULT); + std::string depthName = fileNamePrefix_ + "_Depth_" + std::to_string(depthFrame->width()) + "x" + std::to_string(depthFrame->height()) + "_" + std::to_string(frameIndex) + "_" + + std::to_string(depthFrame->timeStamp()) + "ms.png"; + cv::Mat depthMat(depthFrame->height(), depthFrame->width(), CV_16UC1, depthFrame->data()); + cv::imwrite(depthName, depthMat, params); + std::cout << "Depth saved:" << depthName << std::endl; +} + +void DepthCameraOperation::saveColorFrame(const std::shared_ptr colorFrame, const uint32_t frameIndex, std::string fileNamePrefix_) +{ + std::vector params; + params.push_back(cv::IMWRITE_PNG_COMPRESSION); + params.push_back(0); + params.push_back(cv::IMWRITE_PNG_STRATEGY); + params.push_back(cv::IMWRITE_PNG_STRATEGY_DEFAULT); + std::string colorName = fileNamePrefix_ + "_Color_" + std::to_string(colorFrame->width()) + "x" + std::to_string(colorFrame->height()) + "_" + std::to_string(frameIndex) + "_" + + std::to_string(colorFrame->timeStamp()) + "ms.png"; + cv::Mat depthMat(colorFrame->height(), colorFrame->width(), CV_8UC3, colorFrame->data()); + cv::imwrite(colorName, depthMat, params); + std::cout << "Color saved:" << colorName << std::endl; +} + +void DepthCameraOperation::OpenDepthCamera_callback() { } + +void DepthCameraOperation::setCallback(void(*func)()) +{ + m_func = func; +} + +void DepthCameraOperation::CloseDepthCamera() +{ + std::cout << "DepthCameraOperation::CloseDepthCamera,关闭深度相机" << std::endl; + + record = false; + + emit CamClosedSignal(); +} diff --git a/HPPA/DepthCameraWindow.h b/HPPA/DepthCameraWindow.h index 184b0cd..318f5e6 100644 --- a/HPPA/DepthCameraWindow.h +++ b/HPPA/DepthCameraWindow.h @@ -1,11 +1,56 @@ -#pragma once +#pragma once #include #include #include #include +#include +#include +#include +#include #include "ui_DepthCamera.h" +#include "AppSettings.h" + +#include +#include +#include "libobsensor/hpp/Utils.hpp" +typedef void(*func)(); + +class DepthCameraOperation :public QObject +{ + Q_OBJECT + +public: + DepthCameraOperation(); + ~DepthCameraOperation(); + + QImage m_qImage; + void setCallback(void(*func)()); + bool getRecordStatus() const { return record; } + +private: + ob::Pipeline* m_pipe; + cv::Mat frame; + + func m_func; + + void saveDepthFrame(const std::shared_ptr depthFrame, const uint32_t frameIndex, std::string fileNamePrefix_); + void saveColorFrame(const std::shared_ptr colorFrame, const uint32_t frameIndex, std::string fileNamePrefix_); + + bool record; + +public slots: + void OpenDepthCamera(); + void OpenDepthCamera_callback();//不使用信号而使用回调函数来通知界面刷新视频 + void CloseDepthCamera(); + +signals: + void PlotSignal(); + + void CamOpenedSignal(); + void CamClosedSignal(); +}; class DepthCameraWindow : public QDialog { @@ -15,13 +60,19 @@ public: DepthCameraWindow(QWidget* parent = nullptr); ~DepthCameraWindow(); + DepthCameraOperation* m_DepthCameraOperation; public Q_SLOTS: - void PlotRgbImageSignal(); - void CamClosedSignal(); + void openDepthCamera(); + void onCamOpened(); + void closeDepthCamera(); + void onCamClosed(); signals: + void openDepthCameraSignal(); private: Ui::DepthCameraClass ui; + QThread* m_DepthCameraThread; + }; diff --git a/HPPA/HPPA.vcxproj b/HPPA/HPPA.vcxproj index c3a9527..fb49336 100644 --- a/HPPA/HPPA.vcxproj +++ b/HPPA/HPPA.vcxproj @@ -55,8 +55,8 @@ - D:\cpp_library\gdal2.2.3_vs2017\include;C:\Program Files\ResononAPI\include;D:\cpp_library\opencv3.4.11\opencv\build\include;D:\cpp_library\opencv3.4.11\opencv\build\include\opencv;D:\cpp_library\opencv3.4.11\opencv\build\include\opencv2;D:\cpp_project_vs2022\AutoFocus_InspireLinearMotor_DLL\AutoFocus_InspireLinearMotor_DLL\SDKs\PCOMM\Include;D:\cpp_project_vs2022\AutoFocus_InspireLinearMotor_DLL\AutoFocus_InspireLinearMotor_DLL\SDKs\PortControl;D:\cpp_project_vs2022\AutoFocus_InspireLinearMotor_DLL\AutoFocus_InspireLinearMotor_DLL;D:\cpp_project_vs2022\HPPA\HPPA;D:\cpp_library\libconfig-1.7.3\lib;D:\cpp_project_vs2022\HPPA\vincecontrol;D:\cpp_library\vincecontrol_vs2017;C:\XIMEA\API\xiAPI;D:\cpp_project_vs2022\HPPA\IrisMultiMotorController\IrisMultiMotorController;D:\cpp_library\eigen-3.4-rc1;$(IncludePath) - D:\cpp_library\opencv3.4.11\opencv\build\x64\vc15\lib;D:\cpp_library\gdal2.2.3_vs2017\lib;C:\Program Files\ResononAPI\lib64;D:\cpp_project_vs2022\AutoFocus_InspireLinearMotor_DLL\x64\Debug;D:\cpp_library\libconfig-1.7.3\build\x64;D:\cpp_project_vs2022\HPPA\x64\Debug;C:\XIMEA\API\xiAPI;D:\cpp_project_vs2022\HPPA\IrisMultiMotorController\x64\Debug;$(LibraryPath) + D:\cpp_library\gdal2.2.3_vs2017\include;C:\Program Files\ResononAPI\include;D:\cpp_library\opencv3.4.11\opencv\build\include;D:\cpp_library\opencv3.4.11\opencv\build\include\opencv;D:\cpp_library\opencv3.4.11\opencv\build\include\opencv2;D:\cpp_project_vs2022\AutoFocus_InspireLinearMotor_DLL\AutoFocus_InspireLinearMotor_DLL\SDKs\PCOMM\Include;D:\cpp_project_vs2022\AutoFocus_InspireLinearMotor_DLL\AutoFocus_InspireLinearMotor_DLL\SDKs\PortControl;D:\cpp_project_vs2022\AutoFocus_InspireLinearMotor_DLL\AutoFocus_InspireLinearMotor_DLL;D:\cpp_project_vs2022\HPPA\HPPA;D:\cpp_library\libconfig-1.7.3\lib;D:\cpp_project_vs2022\HPPA\vincecontrol;D:\cpp_library\vincecontrol_vs2017;C:\XIMEA\API\xiAPI;D:\cpp_project_vs2022\HPPA\IrisMultiMotorController\IrisMultiMotorController;D:\cpp_library\eigen-3.4-rc1;C:\Program Files\OrbbecSDK 2.7.6\include;$(IncludePath) + D:\cpp_library\opencv3.4.11\opencv\build\x64\vc15\lib;D:\cpp_library\gdal2.2.3_vs2017\lib;C:\Program Files\ResononAPI\lib64;D:\cpp_project_vs2022\AutoFocus_InspireLinearMotor_DLL\x64\Debug;D:\cpp_library\libconfig-1.7.3\build\x64;D:\cpp_project_vs2022\HPPA\x64\Debug;C:\XIMEA\API\xiAPI;D:\cpp_project_vs2022\HPPA\IrisMultiMotorController\x64\Debug;C:\Program Files\OrbbecSDK 2.7.6\lib;$(LibraryPath) Spectral Insight @@ -66,7 +66,7 @@ - opencv_world3411.lib;opencv_world3411d.lib;gdal_i.lib;resonon-basler.lib;AutoFocus_InspireLinearMotor_DLL.lib;libconfig++d.lib;vincecontrol.lib;resonon-allied.lib;xiapi64.lib;IrisMultiMotorController.lib;%(AdditionalDependencies) + opencv_world3411.lib;opencv_world3411d.lib;gdal_i.lib;resonon-basler.lib;AutoFocus_InspireLinearMotor_DLL.lib;libconfig++d.lib;vincecontrol.lib;resonon-allied.lib;xiapi64.lib;IrisMultiMotorController.lib;OrbbecSDK.lib;%(AdditionalDependencies) D:\cpp_project_vs2022\HPPA\x64\Debug;%(AdditionalLibraryDirectories) diff --git a/HPPA/RgbCameraOperation.cpp b/HPPA/RgbCameraOperation.cpp index 5b08299..9c2cdbc 100644 --- a/HPPA/RgbCameraOperation.cpp +++ b/HPPA/RgbCameraOperation.cpp @@ -30,7 +30,7 @@ void RgbCameraOperation::OpenCamera() cam->release(); - emit CamClosed(); + emit CamOpenedSignal(); } @@ -65,4 +65,6 @@ void RgbCameraOperation::CloseCamera() std::cout << "关闭摄像头+++++++++++++++++++++++++++++++++++++++++++" << std::endl; record = false; + + emit CamClosedSignal(); } diff --git a/HPPA/RgbCameraOperation.h b/HPPA/RgbCameraOperation.h index 4b36edc..334172e 100644 --- a/HPPA/RgbCameraOperation.h +++ b/HPPA/RgbCameraOperation.h @@ -42,6 +42,7 @@ public slots: signals: void PlotSignal(); - void CamClosed(); + void CamOpenedSignal(); + void CamClosedSignal(); }; #endif // !RGBCAMERAOPERATION_H diff --git a/HPPA/rgbCameraWindow.cpp b/HPPA/rgbCameraWindow.cpp index c5f0c22..4314805 100644 --- a/HPPA/rgbCameraWindow.cpp +++ b/HPPA/rgbCameraWindow.cpp @@ -18,7 +18,7 @@ rgbCameraWindow::rgbCameraWindow(QWidget* parent) //connect(this->ui.open_rgb_camera_btn, SIGNAL(clicked()), m_RgbCamera, SLOT(OpenCamera_callback()));//ʹûصˢ̣߳uị߳ϵƵ ʧ connect(ui.close_rgb_camera_btn, SIGNAL(clicked()), this, SLOT(onCloseRgbCamera()));//ر - connect(m_RgbCamera, SIGNAL(CamClosed()), this, SIGNAL(CamClosedSignal())); + connect(m_RgbCamera, SIGNAL(CamClosedSignal()), this, SIGNAL(CamClosedSignal())); } rgbCameraWindow::~rgbCameraWindow()