OpenCV(C++):如何保存16位图像

OpenCV (C++): how to save a 16bit image?

本文关键字:16位 保存 图像 C++ OpenCV 何保存      更新时间:2023-10-16

我正在使用kinect,我需要保存RAW深度图像。这意味着我不应该将它转换为8位(这就是imwrite正在做的!),而是将它保存为16位,而不会减少任何位深度。我希望这个问题不会太琐碎,但我是OpenCV编程的新手。我尝试了以下操作,但不起作用:

[...]
Mat imageDepth ( 480, 640, CV_16UC1 );
Mat imageRGB;
// Video stream settings
VideoCapture capture;
capture.open( CAP_OPENNI );
if ( !capture.isOpened() ) {
  cerr << "Cannot get video stream!" << endl;
  exit ( EXIT_WITH_ERROR );
}
if ( !capture.grab() ) {
  cerr << "Cannot grab images!" << endl;
  exit ( EXIT_WITH_ERROR );
}
// Getting frames
if ( capture.retrieve( imageDepth, CAP_OPENNI_DISPARITY_MAP ) ) {
  imwrite( fileDepth, imageDepth );
}
if( capture.retrieve( imageRGB, CAP_OPENNI_BGR_IMAGE ) ) {
  imwrite( fileRGB, imageRGB );
}
return EXIT_WITH_SUCCESS;

提前谢谢。

问题不在于图像的保存方式,没关系(如果有人遇到同样的问题,请确保以PNG/TIFF格式保存,并在读取时指定CV_16UC1)。由于VideoCapture,它没有保存为16位;事实上,我做了以下事情:

if ( capture.retrieve( imageDepth, CAP_OPENNI_DISPARITY_MAP ) ) {
   imwrite( fileDepth, imageDepth );
}

但正确的方法是:

if ( capture.retrieve( imageDepth, CAP_OPENNI_DEPTH_MAP ) ) {
  imwrite( fileDepth, imageDepth );
}

所以这是一个愚蠢的问题
感谢所有试图帮助我的人。

我在opencv中的imwrite似乎不支持16位图像存储。因此,我使用了OpenCV FileStorage类。

接下来是相关的代码片段。写作:

cv::FileStorage fp("depth_file.xml", cv::FileStorage::WRITE);
fp << "dframe" << dframe;
fp.release();

阅读:

cv::FileStorage fs(dframeName, cv::FileStorage::READ);
if( fs.isOpened() == false)
{
    cerr<< "No More....Quitting...!";
    return 0;
}
fs["dframe"] >> dframe;