-
-
Notifications
You must be signed in to change notification settings - Fork 56.2k
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
Conversation
Adds capability to read and write multispectral (>4 channels) images in OpenEXR format.
solving warning C4800
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()); |
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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());
There was a problem hiding this comment.
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"; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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()));
}
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
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
Patch to opencv_extra has the same branch name.