0

I have converted an HDR image in RGB color space to CIE-XYZ color space with "cvtColor" function, and then written the image to a Radiance HDR file using "imwrite" function. However, the header of the Radiance HDR file still says "FORMAT=32-bit_rle_rgbe". What should I pass to "imwrite" to make sure it specifies the color space correctly?

I am following the code as in this example: https://docs.opencv.org/3.4/d3/db7/tutorial_hdr_imaging.html (except the tone mapping part, which is irrelevant to my problem). I need the relative luminance information, so I have tried to convert the HDR image, which is in BGR space by default, to the CIE-XYZ space using the code shown below.

/* Codes to read in images and construct the camera response function 
can be found from the link above, though I am happy to post the whole 
if needed.*/
//! [Make HDR image]
std::cout << "Now make HDR file" << endl;
Mat hdr;
Ptr<MergeDebevec> merge_debevec = createMergeDebevec();
merge_debevec->process(images, hdr, times, response);

/* The hdr is in BGR color space by default. 
Therefore, convert it to XYZ space by the following two lines. */
Mat hdr_xyz;
cv::cvtColor(hdr, hdr_xyz, CV_BGR2XYZ);

//! [Write results]
std::cout << "Now write to files" << endl;
cv::imwrite("hdr.hdr", hdr);
cv::imwrite("hdr_xyz.hdr", hdr_xyz);

Most of the code seems to be working, and the resulting HDR image seems to have been converted correctly (judging from the numbers in matrix "hdr_xyz").

However, the resulting HDR file "hdr_xyz.hdr" still has the following header: "#?RGBE FORMAT=32-bit_rle_rgbe" As a result, the image can't be shown correctly with "imshow" or programs like Photoshop. I have also tried the program "hdrgen" by Greg Ward (http://www.anyhere.com/). The HDR file it created has the header "FORMAT=32-bit_rle_xyze" and is correctly shown in Photoshop. I guess it is an easy fix (but I am new to OpenCV). Any suggestions are greatly appreciated.

  • "As a result, the image can't be shown correctly with "imshow"" -- No, that's not related. For `imshow`, any 3 channel input is treated as BGR, it has no concept of other colour spaces. Situation is quite similar for `imwrite`, and specifically for HDR, [`FORMAT=32-bit_rle_rgbe` is the only format it supports](https://github.com/opencv/opencv/blob/master/modules/imgcodecs/src/rgbe.cpp#L163). – Dan Mašek Oct 20 '19 at 10:09
  • Thank you @DanMašek for pointing me to the right source code. It is much clearer now. Is it true that OpenCV only has functions to write to RGB color space and I have to implement my own functions to write to other color space? – Jiaweikodomo Oct 20 '19 at 21:03
  • That seems to be the case, yes (but you'd have to go through all the `imwrite` codecs in that source code directory to be sure, but looking at the documentation I don't see any flags to configure such functionality). – Dan Mašek Oct 20 '19 at 21:12

0 Answers0