RM新时代网站

C++ 存儲示例代碼-Save To Disk

# 存儲示例-SaveToDisk

 

功能描述:連接設備開(kāi)流 , 獲取彩色和深度圖并存儲為png格式。

> 本示例基于C++ High Level API進(jìn)行演示

 

創(chuàng )建兩個(gè)函數來(lái)用于將獲取的圖片保存到文件中

//保存深度圖為png格式
void saveDepth( std::shared_ptr< ob::DepthFrame > depthFrame ) {
    std::vector< int > compression_params;
    compression_params.push_back( cv::IMWRITE_PNG_COMPRESSION );
    compression_params.push_back( 0 );
    compression_params.push_back( cv::IMWRITE_PNG_STRATEGY );
    compression_params.push_back( cv::IMWRITE_PNG_STRATEGY_DEFAULT );
    std::string depthName = "Depth_" + std::to_string( depthFrame->timeStamp() ) + ".png";
    cv::Mat     depthMat( depthFrame->height(), depthFrame->width(), CV_16UC1, depthFrame->data() );
    cv::imwrite( depthName, depthMat, compression_params );
    std::cout << "Depth saved:" << depthName << std::endl;
}

//保存彩色圖為png格式
void saveColor( std::shared_ptr< ob::ColorFrame > colorFrame ) {
    std::vector< int > compression_params;
    compression_params.push_back( cv::IMWRITE_PNG_COMPRESSION );
    compression_params.push_back( 0 );
    compression_params.push_back( cv::IMWRITE_PNG_STRATEGY );
    compression_params.push_back( cv::IMWRITE_PNG_STRATEGY_DEFAULT );
    std::string colorName = "Color_" + std::to_string( colorFrame->timeStamp() ) + ".png";
    cv::Mat     colorRawMat( 1, colorFrame->dataSize(), CV_8UC1, colorFrame->data() );
    cv::Mat     colorMat = cv::imdecode( colorRawMat, 1 );
    cv::imwrite( colorName, colorMat, compression_params );
    std::cout << "Color saved:" << colorName << std::endl;
}

創(chuàng )建一個(gè)Pipeline,通過(guò)Pipeline可以很容易的打開(kāi)和關(guān)閉多種類(lèi)型的流并獲取一組幀數據

ob::Pipeline pipeline;

 

然后可以通過(guò)Pipeline來(lái)獲取彩色流和深度流的所有配置, 包括流的分辨率 ,幀率 ,以及流的格式,配置所需要彩色和深度流

try {
    // Get all stream profiles of the color camera, including stream resolution, frame rate, and frame format
    auto                                    colorProfiles = pipeline.getStreamProfileList(OB_SENSOR_COLOR);
    std::shared_ptr<ob::VideoStreamProfile> colorProfile  = nullptr;
    if(colorProfiles) {
        colorProfile = std::const_pointer_cast<ob::StreamProfile>(colorProfiles->getProfile(OB_PROFILE_DEFAULT))->as<ob::VideoStreamProfile>();
    }
    config->enableStream(colorProfile);
}
catch(ob::Error &e) {
    // no Color Sensor
    colorCount = -1;
    std::cerr << "Current device is not support color sensor!" << std::endl;
}

// Get all stream profiles of the depth camera, including stream resolution, frame rate, and frame format
auto                                    depthProfiles = pipeline.getStreamProfileList(OB_SENSOR_DEPTH);
std::shared_ptr<ob::VideoStreamProfile> depthProfile  = nullptr;
if(depthProfiles) {
    depthProfile = std::const_pointer_cast<ob::StreamProfile>(depthProfiles->getProfile(OB_PROFILE_DEFAULT))->as<ob::VideoStreamProfile>();
}
config->enableStream(depthProfile);

啟動(dòng)流

pipeline.start( config );

通過(guò)等待的方式來(lái)獲取幀的數據

auto frameset = pipeline.waitForFrames( 100 )

獲取單種類(lèi)型的幀的數據

auto colorFrame = frameset->colorFrame();
auto depthFrame = frameset->depthFrame();

 

創(chuàng )建格式轉換Filter對彩色圖像進(jìn)行轉換成RGB格式后保存

//創(chuàng )建格式轉換Filter
ob::FormatConvertFilter formatConverFilter;

formatConverFilter.setFormatConvertType(FORMAT_MJPEG_TO_RGB888);
colorFrame = formatConverFilter.process(colorFrame)->as<ob::ColorFrame>();
formatConverFilter.setFormatConvertType(FORMAT_RGB_TO_BGR);
colorFrame = formatConverFilter.process(colorFrame)->as<ob::ColorFrame>();

通過(guò)開(kāi)頭的編輯的存儲函數來(lái)存儲獲得的數據

saveColor( colorFrame );
saveDepth( depthFrame );

 

停止Pipeline

pipeline.stop();

 

程序正常退出后會(huì )釋放資源


RM新时代网站