OpenCV and reading image with floating point data
I use OpenCV to read the image. Then I use Matlab to load the same image. Then I display the images. For OpenCV loaded image, the image is has no picture inside and just gray plane. For Matlab loaded image, it has the image what I want. The image pixel values are very small floating point data like 0.0021. The code I used to load the image is shown as follow.
`Mat image(IMAGE_ROW, IMAGE_COL, CV_64FC3); Mat gray(IMAGE_ROW, IMAGE_COL, CV_64FC1); image = imread(filespath, CV_LOAD_IMAGE_COLOR );// Read the file cv::imshow("Image", image); cvtColor( image, gray, CV_BGR2GRAY, 1); cv::imshow("gray", gray);`
Why I can't have the same image as loaded by Matlab?
well you can't do it with imwrite()/imread() as stated before.
but you can save/load floating point Mats using the FileStorage, like this:
Mat fm = Mat::ones(3,3,CV_32FC3); // dummy data FileStorage fs("my.yml", FileStorage::WRITE ); fs << "mat1" << fm; //choose any key here, just be consistant with the one below
and read back in:
Mat fm; FileStorage fs("my.yml", FileStorage::READ ); fs["mat1"] >> fm;
You don't need to explicitly initialize a cv::Mat image before calling cv::imread, it will initialize the image properly according to the size and format of the image read. So it doesn't matter that you've initialized your image with (IMAGE_ROW, IMAGE_COL, CV_64FC3).
OpenCV has no capabilities for writing/reading floating point images. From cv::imwrite manual:
Only 8-bit (or 16-bit in the case of PNG, JPEG 2000 and TIFF) single-channel or 3-channel (with ‘BGR’ channel order) images can be saved using this function.
You can load float images with opencv Mat img= imread(filename, CV_LOAD_IMAGE_ANYDEPTH);
Tried @berak solution, but got a "Missing , between elements" exception. As stated in this bug report, you must release the FileStorage object after writing operation, otherwise it will not properly finalise the file writing and thus raising that exception. Then, the corrected version of the codelet should be:
Mat fm = Mat::ones(3,3,CV_32FC3); // dummy data FileStorage fs("my.yml", FileStorage::WRITE ); fs << "mat1" << fm; //choose any key here, just be consistant with the one below fs.release(); //Release the file and finish the writing.