OpenCV save CV_32FC1 images

前端 未结 3 595
借酒劲吻你
借酒劲吻你 2021-01-18 00:48

A program I am using is reading some bitmaps, and expects 32FC1 images.

I am trying to create these images

cv::Mat M1(255, 255, CV_32FC1, cv::Scalar(         


        
3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-18 01:23

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

    If you only need to write .bmp files with OpenCV , you can convert your 32FC1 image to 8UC4, then use cv::imwrite() to write it and you will get a 32bits per pixel .bmp file. I am guessing that your program that reads the file will interpret the 32 bit pixels as a 32FC1. The .bmp format doesn't have an explicit channel structure, just a number of 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 this you need to be aware of endianness assumptions by the reader. Someting 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 will not be able to read properly the files you created in OpenCV because the .bmp decoder in OpenCV assumes the file is 1 or 3 channel of 8 bit data (i.e. it can't 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 you just need to save your files with a .exr extension.

提交回复
热议问题