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