Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

opencv

kinect

I'm working with kinect, and I need to save RAW depth image. This means that I shouldn't save it with a conversion to 8 bit (that is what imwrite is doing!) but save it as 16 bit, without have any bit-depth reducing. I hope that this question will not be too trivial, but I'm 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.

like image 832
Andrea Avatar asked Nov 10 '14 16:11

Andrea


People also ask

How do we save an image from OpenCV?

When working with OpenCV Python, images are stored in numpy ndarray. To save an image to the local file system, use cv2. imwrite() function of opencv python library.

How do I save an image in C++ OpenCV?

OpenCV provides imwrite() function to save an image to a specified file. The file extension represents the image format. Here, "Destination" is where we want to save the image. In this program, we save the image as "Lakshmi.

What format is the image read in OpenCV?

So, OpenCV can always read JPEGs, PNGs, and TIFFs. On MacOSX, there is also an option to use native MacOSX image readers.

How do I move pixels in OpenCV?

You can simply use affine transformation translation matrix (which is for shifting points basically). cv::warpAffine() with proper transformation matrix will do the trick. where: tx is shift in the image x axis, ty is shift in the image y axis, Every single pixel in the image will be shifted like that.


2 Answers

The problem wasn't in the way the image was saved, that was all right (if someone will have the same problem, be sure to save in PNG/TIFF format and specify CV_16UC1 when reading). It wasn't saved as 16bit because of VideoCapture; in fact I 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 silly problem.
Thanks to all the people who tried to help me.

like image 107
Andrea Avatar answered Sep 29 '22 18:09

Andrea


My imwrite in opencv does not seem to support 16-bit image storage. So, I used the OpenCV FileStorage Class.

Next is the relevant code snippet. WRITING:

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;
like image 26
mkuse Avatar answered Sep 29 '22 17:09

mkuse