OpenCV (C ++): how to save a 16-bit image?
I am working with kinect and I need to save a RAW depth image. This means I shouldn't be saving it with conversion to 8 bits (that's what imwrite does!), But store it as 16 bits, without decreasing the depth. I hope this question won't be too trivial, but I am new to OpenCV programming. I tried the following but it doesn't work:
[...]
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;
Thanks in advance.
source to share
The problem is not how the image was saved, everything was fine (if anyone has the same problem, be sure to save as PNG / TIFF and include CV_16UC1 when reading). It was not saved as 16 bit due to VideoCapture; I actually did the following:
if ( capture.retrieve( imageDepth, CAP_OPENNI_DISPARITY_MAP ) ) {
imwrite( fileDepth, imageDepth );
}
But the correct way to do it is:
if ( capture.retrieve( imageDepth, CAP_OPENNI_DEPTH_MAP ) ) {
imwrite( fileDepth, imageDepth );
}
So it was a stupid problem.
Thanks to all the people who tried to help me.
source to share
My imwrite in opencv doesn't seem to support 16-bit image storage. So, I used the OpenCV FileStorage class.
The following is the corresponding code snippet. RECORD:
cv::FileStorage fp("depth_file.xml", cv::FileStorage::WRITE);
fp << "dframe" << dframe;
fp.release();
READING:
cv::FileStorage fs(dframeName, cv::FileStorage::READ);
if( fs.isOpened() == false)
{
cerr<< "No More....Quitting...!";
return 0;
}
fs["dframe"] >> dframe;
source to share