OpenCV CV_32FC1 File I/O

During my work with the Kinect 2 and libfreenect2 I wanted to save the captured depth images (CV_32FC1) to use it for postprocessing in a next step. Everytime I loaded the data back with cv::imread I had the problem that the data was not in that format that it should be.

Here is my solution to solve that task:

cv::Mat img(width, height, CV_32FC1, cv::Scalar(0)); // fill image with data img.at<float>(col, row) = … std::vector<int> compression_params; compression_params.push_back(CV_IMWRITE_PNG_COMPRESSION); compression_params.push_back(0);

cv::Mat m1(img.rows, img.cols, CV_8UC4, img.data); cv::imwrite(“D:\\img.png”, m1, compression_params); cv::Mat depth = cv::imread(“D:\\img.png”, -1); cv::Mat m2(depth.rows, depth.cols, CV_32FC1, depth.data); // check output cv::imshow(“Image”, img); cv::imshow(“Depth”, m2); cv::waitKey(0);

Here is my test file: DepthImageTest

Kinect v2 Free API: libfreenect2 (Windows)

If you are going to work with Microsoft’s Kinect v2 in the visual area you don’t need to use the official SDK and driver. Instead of that you should work with libfreenect2 to get platform and IDE independency. There is only one thing you have to do before you can use it: you have to build it (not difficult but time consuming – in my case). To avoid the same problems that I had, follow the guide below.

Dependencies
Download and install all applications. Extract the source files to your favorite projects’ directory.

Applications:
cmake
nasm
opencl

Sources:
libfreenect2
glfw
libjpeg-turbo
libusbx
opencv

Building

Start building of all sources by using cmake, but the last project you should build is libfreenect2. Follow the rules on the libfreenect2 homepage.