Skip to content

imgcodecs: OpenEXR multispectral read/write support #27485

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jul 14, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion modules/imgcodecs/include/opencv2/imgcodecs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ filename extension (see cv::imread for the list of extensions). In general, only
single-channel or 3-channel (with 'BGR' channel order) images
can be saved using this function, with these exceptions:

- With OpenEXR encoder, only 32-bit float (CV_32F) images can be saved.
- With OpenEXR encoder, only 32-bit float (CV_32F) images can be saved. More than 4 channels can be saved. (imread can load it then.)
- 8-bit unsigned (CV_8U) images are not supported.
- With Radiance HDR encoder, non 64-bit float (CV_64F) images can be saved.
- All images will be converted to 32-bit float (CV_32F).
Expand Down
61 changes: 52 additions & 9 deletions modules/imgcodecs/src/grfmt_exr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ ExrDecoder::ExrDecoder()
m_ischroma = false;
m_hasalpha = false;
m_native_depth = false;

m_multispectral = false;
m_channels = 0;
}


Expand All @@ -140,7 +141,7 @@ void ExrDecoder::close()

int ExrDecoder::type() const
{
return CV_MAKETYPE((m_isfloat ? CV_32F : CV_32S), ((m_iscolor && m_hasalpha) ? 4 : m_iscolor ? 3 : m_hasalpha ? 2 : 1));
return CV_MAKETYPE((m_isfloat ? CV_32F : CV_32S), (m_multispectral ? m_channels : (m_iscolor && m_hasalpha) ? 4 : m_iscolor ? 3 : m_hasalpha ? 2 : 1));
}


Expand Down Expand Up @@ -169,6 +170,7 @@ bool ExrDecoder::readHeader()
m_green = channels.findChannel( "G" );
m_blue = channels.findChannel( "B" );
m_alpha = channels.findChannel( "A" );
m_multispectral = channels.findChannel( "0" ) != nullptr;

if( m_alpha ) // alpha channel supported in RGB, Y, and YC scenarios
m_hasalpha = true;
Expand All @@ -179,6 +181,23 @@ bool ExrDecoder::readHeader()
m_ischroma = false;
result = true;
}
else if( m_multispectral )
{
m_channels = 0;
for( auto it = channels.begin(); it != channels.end(); it++ )
m_channels++;

m_iscolor = true; // ??? false
m_ischroma = false;
m_hasalpha = false;
result = m_channels <= CV_CN_MAX;

for ( int i = 1; result && i < m_channels; i++ ) // channel 0 was found previously
{
const Channel *ch = channels.findChannel( std::to_string(i) );
result = ch && ch->xSampling == 1 && ch->ySampling == 1; // subsampling is not supported
}
}
else
{
m_green = channels.findChannel( "Y" );
Expand Down Expand Up @@ -214,8 +233,9 @@ bool ExrDecoder::readHeader()
bool ExrDecoder::readData( Mat& img )
{
m_native_depth = CV_MAT_DEPTH(type()) == img.depth();
bool multispectral = img.channels() > 4;
bool color = img.channels() > 2; // output mat has 3+ channels; Y or YA are the 1 and 2 channel scenario
bool alphasupported = ( img.channels() % 2 == 0 ); // even number of channels indicates alpha
bool alphasupported = !multispectral && ( img.channels() % 2 == 0 ); // even number of channels indicates alpha
int channels = 0;
uchar* data = img.ptr();
size_t step = img.step;
Expand All @@ -231,10 +251,13 @@ bool ExrDecoder::readData( Mat& img )
const size_t floatsize = sizeof(float);
size_t xstep = m_native_depth ? floatsize : 1; // 4 bytes if native depth (FLOAT), otherwise converting to 1 byte U8 depth
size_t ystep = 0;
const int channelstoread = ( (m_iscolor && alphasupported) ? 4 :
const int channelstoread = ( multispectral ? img.channels() : (m_iscolor && alphasupported) ? 4 :
( (m_iscolor && !m_ischroma) || color) ? 3 : alphasupported ? 2 : 1 ); // number of channels to read may exceed channels in output img
size_t xStride = floatsize * channelstoread;

CV_Assert( m_multispectral == multispectral );
CV_Assert( justcopy || !multispectral ); // multispectral needs justcopy

// See https://github.com/opencv/opencv/issues/26705
// If ALGO_HINT_ACCURATE is set, read BGR and swap to RGB.
// If ALGO_HINT_APPROX is set, read RGB directly.
Expand Down Expand Up @@ -312,6 +335,15 @@ bool ExrDecoder::readData( Mat& img )
xsample[0] = m_green->xSampling;
}
}
else if( m_multispectral )
{
for ( int i = 0; i < m_channels; i++ )
{
frame.insert( std::to_string(i), Slice( m_type,
buffer - m_datawindow.min.x * xStride - m_datawindow.min.y * ystep + (floatsize * i),
xStride, ystep, 1, 1, 0.0 ));
}
}
else
{
if( m_blue )
Expand Down Expand Up @@ -382,7 +414,7 @@ bool ExrDecoder::readData( Mat& img )
{
m_file->readPixels( m_datawindow.min.y, m_datawindow.max.y );

if( m_iscolor )
if( m_iscolor && !m_multispectral )
{
if (doReadRGB)
{
Expand Down Expand Up @@ -804,13 +836,19 @@ bool ExrEncoder::write( const Mat& img, const std::vector<int>& params )
header.channels().insert( "B", Channel( type ) );
//printf("bunt\n");
}
else
else if( channels == 1 || channels == 2 )
{
header.channels().insert( "Y", Channel( type ) );
//printf("gray\n");
}
else if( channels > 4 )
{
for ( int i = 0; i < channels; i++ )
header.channels().insert( std::to_string(i), Channel( type ) );
//printf("multi-channel\n");
}

if( channels % 2 == 0 )
if( channels % 2 == 0 && channels <= 4)
{ // even number of channels indicates Alpha
header.channels().insert( "A", Channel( type ) );
}
Expand Down Expand Up @@ -843,10 +881,15 @@ bool ExrEncoder::write( const Mat& img, const std::vector<int>& params )
frame.insert( "G", Slice( type, buffer + size, size * channels, bufferstep ));
frame.insert( "R", Slice( type, buffer + size * 2, size * channels, bufferstep ));
}
else
else if( channels == 1 || channels == 2 )
frame.insert( "Y", Slice( type, buffer, size * channels, bufferstep ));
else if( channels > 4 )
{
for ( int i = 0; i < channels; i++ )
frame.insert( std::to_string(i), Slice( type, buffer + size * i, size * channels, bufferstep ));
}

if( channels % 2 == 0 )
if( channels % 2 == 0 && channels <= 4 )
{ // even channel count indicates Alpha channel
frame.insert( "A", Slice( type, buffer + size * (channels - 1), size * channels, bufferstep ));
}
Expand Down
2 changes: 2 additions & 0 deletions modules/imgcodecs/src/grfmt_exr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ class ExrDecoder CV_FINAL : public BaseImageDecoder
bool m_iscolor;
bool m_isfloat;
bool m_hasalpha;
bool m_multispectral;
int m_channels;

private:
ExrDecoder(const ExrDecoder &); // copy disabled
Expand Down
9 changes: 9 additions & 0 deletions modules/imgcodecs/src/loadsave.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,12 @@ static bool imwrite_( const String& filename, const std::vector<Mat>& img_vec,
Mat image = img_vec[page];
CV_Assert(!image.empty());

#ifdef HAVE_OPENEXR
CV_Assert( image.channels() == 1 || image.channels() == 3 || image.channels() == 4 || encoder.dynamicCast<ExrEncoder>() );
#else
CV_Assert( image.channels() == 1 || image.channels() == 3 || image.channels() == 4 );
#endif


Mat temp;
if( !encoder->isFormatSupported(image.depth()) )
Expand Down Expand Up @@ -1473,7 +1478,11 @@ bool imencode( const String& ext, InputArray _img,
CV_Assert(!image.empty());

const int channels = image.channels();
#ifdef HAVE_OPENEXR
CV_Assert( channels == 1 || channels == 3 || channels == 4 || encoder.dynamicCast<ExrEncoder>() );
#else
CV_Assert( channels == 1 || channels == 3 || channels == 4 );
#endif

Mat temp;
if( !encoder->isFormatSupported(image.depth()) )
Expand Down
26 changes: 26 additions & 0 deletions modules/imgcodecs/test/test_exr.impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,32 @@ TEST(Imgcodecs_EXR, readWrite_32FC3)
EXPECT_EQ(0, remove(filenameOutput.c_str()));
}

TEST(Imgcodecs_EXR, readWrite_32FC7)
{ // 0-6 channels (nultispectral)
const string root = cvtest::TS::ptr()->get_data_path();
const string filenameInput = root + "readwrite/test32FC7.exr";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

imho using a generated cv::Mat here instead of adding an extra test file to opencv_extra is better.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

    TEST(Imgcodecs_EXR, readWrite_32FC7)
    { // 0-6 channels (multispectral)
        const string filename = cv::tempfile(".exr");

        const Size sz(3, 5);
        Mat img(sz, CV_MAKETYPE(CV_32F, 7));
        img.at<cv::Vec<float, 7>>(0, 0)[0] = 101.125;
        img.at<cv::Vec<float, 7>>(2, 1)[3] = 203.500;
        img.at<cv::Vec<float, 7>>(4, 2)[6] = 305.875;

        ASSERT_TRUE(cv::imwrite(filename, img));
        const Mat img2 = cv::imread(filename, IMREAD_UNCHANGED);
        EXPECT_EQ(img2.type(), img.type());
        EXPECT_EQ(img2.size(), img.size());
        EXPECT_LE(cvtest::norm(img, img2, NORM_INF | NORM_RELATIVE), 1e-3);

        const Mat img3 = cv::imread(filename, IMREAD_GRAYSCALE);
        ASSERT_TRUE(img3.empty());
        const Mat img4 = cv::imread(filename, IMREAD_COLOR);
        ASSERT_TRUE(img4.empty());
        EXPECT_EQ(0, remove(filename.c_str()));
    }

output

[ RUN      ] Imgcodecs_EXR.readWrite_32FC7
[ERROR:0@3.966] global loadsave.cpp:594 cv::imread_ imread_('C:\Users\aidata\AppData\Local\Temp\ocvBA1A.tmp.exr'): can't read data: OpenCV(4.12.0-pre) C:\projects\opencv\modules\imgcodecs\src\grfmt_exr.cpp:260: error: (-2:Unspecified error) in function 'bool __cdecl cv::ExrDecoder::readData(class cv::Mat &)'
> OpenCV EXR decoder needs more number of channels for multispectral images. Use cv::IMREAD_UNCHANGED mode for imread. (expected: 'img.channels() == CV_MAT_CN(type())'), where
>     'img.channels()' is 1
> must be equal to
>     'CV_MAT_CN(type())' is 7

[ERROR:0@3.966] global loadsave.cpp:594 cv::imread_ imread_('C:\Users\aidata\AppData\Local\Temp\ocvBA1A.tmp.exr'): can't read data: OpenCV(4.12.0-pre) C:\projects\opencv\modules\imgcodecs\src\grfmt_exr.cpp:260: error: (-2:Unspecified error) in function 'bool __cdecl cv::ExrDecoder::readData(class cv::Mat &)'
> OpenCV EXR decoder needs more number of channels for multispectral images. Use cv::IMREAD_UNCHANGED mode for imread. (expected: 'img.channels() == CV_MAT_CN(type())'), where
>     'img.channels()' is 3
> must be equal to
>     'CV_MAT_CN(type())' is 7

[       OK ] Imgcodecs_EXR.readWrite_32FC7 (5 ms)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

        EXPECT_EQ(0, remove(filename.c_str()));
        const Mat img3 = cv::imread(filename, IMREAD_GRAYSCALE);
        ASSERT_TRUE(img3.empty());
        const Mat img4 = cv::imread(filename, IMREAD_COLOR);
        ASSERT_TRUE(img4.empty());
    }

Removing file is before testing grayscale and color in current branch. It prints error message as you can't load multispectral image as GRAYSCALE or COLOR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

        EXPECT_EQ(0, remove(filename.c_str()));
        const Mat img3 = cv::imread(filename, IMREAD_GRAYSCALE);
        ASSERT_TRUE(img3.empty());
        const Mat img4 = cv::imread(filename, IMREAD_COLOR);
        ASSERT_TRUE(img4.empty());
    }

Removing file is before testing grayscale and color in current branch. It prints error message as you can't load multispectral image as GRAYSCALE or COLOR.

i fixed it on my code

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i suggest using this

    TEST(Imgcodecs_EXR, readWrite_32FC7)
    { // 0-6 channels (multispectral)
        const string filename = cv::tempfile(".exr");

        const Size sz(3, 5);
        Mat img = Mat::zeros(sz, CV_MAKETYPE(CV_32F, 7));
        img.at<cv::Vec<float, 7>>(0, 0)[0] = 101.125;
        img.at<cv::Vec<float, 7>>(2, 1)[3] = 203.500;
        img.at<cv::Vec<float, 7>>(4, 2)[6] = 305.875;

        ASSERT_TRUE(cv::imwrite(filename, img));
        const Mat img2 = cv::imread(filename, IMREAD_UNCHANGED);
        EXPECT_EQ(img2.type(), img.type());
        EXPECT_EQ(img2.size(), img.size());
        EXPECT_LE(cvtest::norm(img, img2, NORM_INF | NORM_RELATIVE), 1e-3);

        const Mat img3 = cv::imread(filename, IMREAD_GRAYSCALE);
        ASSERT_TRUE(img3.empty());
        const Mat img4 = cv::imread(filename, IMREAD_COLOR);
        ASSERT_TRUE(img4.empty());
        EXPECT_EQ(0, remove(filename.c_str()));
    }

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

        const Size sz(3, 5);
        Mat img = Mat::zeros(sz, CV_MAKETYPE(CV_32F, 7));
        img.at<cv::Vec<float, 7>>(0, 0)[0] = 101.125;
        img.at<cv::Vec<float, 7>>(2, 1)[3] = 203.500;
        img.at<cv::Vec<float, 7>>(4, 2)[6] = 305.875;
        ASSERT_TRUE(cv::imwrite(filename, img));

generates the same file at opencv/opencv_extra#1262

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I use the same pattern as tests readWrite_32FC1, readWrite_32FC3, readWrite_32FC1_half, readWrite_32FC3_half. It's generate image file upon GENERATE_DATA define. I don't know when it was called with this define so I do the new test readWrite_32FC7 same way.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xaos-cz thank you for your contribution. i am also a common contributor ( a bit more experienced). I think this test is to verify that the change you made is working correctly and to show if a later change breaks your code. It doesn't matter if it's similar in form to the other tests.

const string filenameOutput = cv::tempfile(".exr");
#ifndef GENERATE_DATA
const Mat img = cv::imread(filenameInput, IMREAD_UNCHANGED);
#else
const Size sz(3, 5);
Mat img(sz, CV_32FC7);
img.at<cv::Vec<float, 7>>(0, 0)[0] = 101.125;
img.at<cv::Vec<float, 7>>(2, 1)[3] = 203.500;
img.at<cv::Vec<float, 7>>(4, 2)[6] = 305.875;
ASSERT_TRUE(cv::imwrite(filenameInput, img));
#endif
ASSERT_FALSE(img.empty());
ASSERT_EQ(CV_MAKETYPE(CV_32F, 7), img.type());

ASSERT_TRUE(cv::imwrite(filenameOutput, img));
const Mat img2 = cv::imread(filenameOutput, IMREAD_UNCHANGED);
ASSERT_EQ(img2.type(), img.type());
ASSERT_EQ(img2.size(), img.size());
Copy link
Contributor

@mshabunin mshabunin Jul 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Temporary file will not be removed if test fail on this or previous asserts. We need to use EXPECT.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am new to gtest so I don't know that EXPECT should be used. I copied the pattern which was in previous tests: readWrite_32FC1, readWrite_32FC3.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

    EXPECT_EQ(img2.type(), img.type());
    EXPECT_EQ(img2.size(), img.size());

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed that only in my test readWrite_32FC7, but the same is in several other tests.

EXPECT_LE(cvtest::norm(img, img2, NORM_INF | NORM_RELATIVE), 1e-3);
EXPECT_EQ(0, remove(filenameOutput.c_str()));
}


TEST(Imgcodecs_EXR, readWrite_32FC1_half)
{
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy