OpenCV Saves CV_32FC1 Images

The program I use reads some bitmaps and expects 32FC1 images.

I'm trying to create these images

cv::Mat M1(255, 255, CV_32FC1, cv::Scalar(0,0,0)); cv::imwrite( "my_bitmap.bmp", M1 ); 

but when I check the depth it is always CV_8U

How can I create files so that they contain the correct information?

Update : it doesn't matter if I use another file extension - for example. tif or png

I read it - using the code already implemented - with cvLoadImage .

I'm trying to CREATE files that existing code can use - which checks the type of image.

I cannot convert files to existing code. Existing code does not attempt to read a random type of image and convert it to the desired type, but checks that the files are of the correct type.

I found out - thanks for the answers that cv :: imwrite only writes images of integer type.

Is there any other way - either using OpenCV, or something else - to record images so that in the end I get CV_32F?

Refresh again: Code for reading image ... if in cv :: Mat:

 cv::Mat x = cv::imread(x_files, CV_LOAD_IMAGE_ANYDEPTH|CV_LOAD_IMAGE_ANYCOLOR); 

Existing Code:

 IplImage *I = cvLoadImage(x_files.c_str(), CV_LOAD_IMAGE_ANYDEPTH|CV_LOAD_IMAGE_ANYCOLOR); 
+4
source share
2 answers

cv :: imwrite () .bmp encoder assumes 8-bit channels.

If you only need to write .bmp files using OpenCV, you can convert the 32FC1 image to 8UC4 and then use cv::imwrite() to write it, and you get a 32-bit pixel file .bmp. I assume that your program that reads the file will interpret 32-bit pixels as 32FC1. The .bmp format does not have an explicit channel structure, just a few bits per pixel. Therefore, you should be able to write 32-bit pixels as 4 channels of 8 bits in OpenCV and read them as single-channel 32-bit pixels in another program - if you do, you need to know about the reader's assumptions. A match like the following should work:

 cv::Mat m1(rows, cols, CV_32FC1); ... // fill m1 cv::Mat m2(rows, cols, CV_8UC4, m1.data); // provide different view of m1 data // depending on endianess of reader, you may need to swap byte order of m2 pixels cv::imwrite("my_bitmap.bmp", m2); 

You cannot read files created in OpenCV correctly, because the .bmp decoder in OpenCV assumes that the file is 1 or 3 channel of 8-bit data (that is, it cannot read 32-bit pixels).


EDIT

Probably a much better option would be to use the OpenEXR format , for which OpenCV has a codec. I assume that you just need to save your files with the .exr extension.

+3
source

Your problem is that bitmaps store data internally since integers do not float. If your problem comes down to an error while saving, you need to either use a different file format or scale your data before saving, and then return after saving. If you just want to convert the matrix that you get after reading the file into a float, you can use cv :: convertto

+2
source

Source: https://habr.com/ru/post/1492194/


All Articles