# 存儲示例-SaveToDisk
功能描述:連接設備開流 , 獲取彩色和深度圖并存儲為png格式。
> 本示例基于C++ High Level API進行演示
創(chuàng)建兩個函數來用于將獲取的圖片保存到文件中
//保存深度圖為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)建一個Pipeline,通過Pipeline可以很容易的打開和關閉多種類型的流并獲取一組幀數據
ob::Pipeline pipeline;
然后可以通過Pipeline來獲取彩色流和深度流的所有配置, 包括流的分辨率 ,幀率 ,以及流的格式,配置所需要彩色和深度流
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);
啟動流
pipeline.start( config );
通過等待的方式來獲取幀的數據
auto frameset = pipeline.waitForFrames( 100 )
獲取單種類型的幀的數據
auto colorFrame = frameset->colorFrame();
auto depthFrame = frameset->depthFrame();
創(chuàng)建格式轉換Filter對彩色圖像進行轉換成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>();
通過開頭的編輯的存儲函數來存儲獲得的數據
saveColor( colorFrame );
saveDepth( depthFrame );
停止Pipeline
pipeline.stop();
程序正常退出后會釋放資源