10000 imgcodecs: OpenEXR multispectral read/write support by xaos-cz · Pull Request #27485 · opencv/opencv · GitHub
[go: up one dir, main page]

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

Conversation

xaos-cz
Copy link
Contributor
@xaos-cz xaos-cz commented Jun 26, 2025

OpenCV Extra: opencv/opencv_extra#1262

Adds capability to read and write multispectral (>4 channels) images in OpenEXR format.

Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

  • I agree to contribute to the project under Apache 2 License.
  • To the best 10000 of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
  • The PR is proposed to the proper branch
  • There is a reference to the original bug report and related work
  • There is accuracy test, performance test and test data in opencv_extra repository, if applicable
    Patch to opencv_extra has the same branch name.
  • The feature is well documented and sample code can be built with the project CMake

xaos-cz added 2 commits June 26, 2025 15:33
Adds capability to read and write multispectral (>4 channels) images in OpenEXR format.
solving warning C4800
@vpisarev vpisarev self-requested a review June 27, 2025 07:07
@asmorkalov asmorkalov modified the milestones: 4.12.0, 4.13.0 Jun 28, 2025
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.

TEST(Imgcodecs_EXR, readWrite_32FC7)
{ // 0-6 channels (multispectral)
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.

@asmorkalov asmorkalov self-assigned this Jul 14, 2025
@asmorkalov asmorkalov merged commit 69b2024 into opencv:4.x Jul 14, 2025
27 of 28 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants
0