You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Passing any of the IMREAD_REDUCED_ flags, e.g. IMREAD_REDUCED_GRAYSCALE_2, to the flags parameter of imdecode() has no effect. I notice the imread_() function includes this code:
int scale_denom = 1;
if( flags > IMREAD_LOAD_GDAL )
{
if( flags & IMREAD_REDUCED_GRAYSCALE_2 )
scale_denom = 2;
else if( flags & IMREAD_REDUCED_GRAYSCALE_4 )
scale_denom = 4;
else if( flags & IMREAD_REDUCED_GRAYSCALE_8 )
scale_denom = 8;
}
/// set the scale_denom in the driver
decoder->setScale( scale_denom );
This code is absent in imdecode() (and imdecode_).
For those that aren't familiar with that flag: The expected effect is that the image is resized by the stated factor if it is a JPEG. This happens for "free" due to the way the JPEG encoding works, so it is much faster than opening the image at full resolution and then doing a separate resize.
Mat img1 = imread("d:/input.jpg", IMREAD_REDUCED_GRAYSCALE_8);
Mat img2 = imread("d:/input.jpg", IMREAD_REDUCED_GRAYSCALE_4);
Mat img3 = imread("d:/input.jpg", IMREAD_REDUCED_GRAYSCALE_2);
Mat img4 = imread("d:/input.jpg");
cout << endl << img1.size;
cout << endl << img2.size;
cout << endl << img3.size;
cout << endl << img4.size;
produces:
435 x 290
869 x 579
1737 x 1157
3474 x 2314
Are you using a Jpeg2k file by any chance? I don't sense OpenCV handles rescaling in that format, at least I don't see it handled in grfmt_jpeg2000.cpp.
Many thanks to both of you for providing fixes. I must admit I'm a little surprised that both of are suggesting replacing the sequence of if statements (quoted in my original comment) with:
scale_denom = flags >> 3;
Although this is certainly shorter, I don't think it's simpler. It's not as obvious that it's correct as the original code was, and it's fragile in the face of enum values changing in future (admittedly unlikely). I think there's a chance it won't work with other flags simultaneously e.g. what happens if the user provides IMREAD_REDUCED_GRAYSCALE_4 | IMREAD_IGNORE_ORIENTATION? It also makes it harder to search - I came across the snippet by searching the codebase for "IMREAD_REDUCED_GRAYSCALE_2", which after this change would provide no hits.
If you want to do a bit of refactoring while fixing this issue, a more productive route would probably be to merge imread_() and imdecode_() into one function. After all, that duplicated code is how this issue arose in the first place. But I'm not an OpenCV contributor so I don't know for sure that would be possible or a good idea.
Sure @alalek, infact I'll fix imdecode only and write tests for now, raise a new PR for imread. My main concern is that neither imread nor imdecode is taking care of flags like IMREAD_REDUCED_COLOR_2, regarding resize.
Hi @alalek / @sturkmen72 / @JamesOldfield, there is a small problem with the enum flags, they cannot be used with & operators, for example: IMREAD_REDUCED_COLOR_2 is 17 meaning binary: 10001 IMREAD_REDUCED_COLOR_4 is 33 meaning binary: 100001
This means anding them will give true which is wrong. It makes sense for greyscale because they are 16, 32 and 64.
Here is my proposed solution, please let me know if any changes needed
switch(flags)
{
case IMREAD_REDUCED_GRAYSCALE_2:
case IMREAD_REDUCED_COLOR_2:
scale_denom = 2;
break;
case IMREAD_REDUCED_GRAYSCALE_4:
case IMREAD_REDUCED_COLOR_4:
scale_denom = 4;
break;
case IMREAD_REDUCED_GRAYSCALE_8:
case IMREAD_REDUCED_COLOR_8:
scale_denom = 8;
break;
}
@sturkmen72, fair point, but if we add some flags in between, it might break something. But yeah, I think if no flags will be added, I'll go ahead with the current method and do the bitwise and, let me know which is best.
@ganesh-k13 I think you might have misunderstood how IMREAD_ flags are allocated, and the ones related to resizing especially. It is not integer values that are assigned meaning, but instead the individual bits are assigned meaning. So there is no possibility that someone would "add some flags in between". Let me show you what bits correspond to what flags, with an example value that I will explain below:
In the above example value of 161, the flags are set to ignore JPEG orientation, reduce size by factor 4, and return as a color image, because those three flags are set.
I think you are confused by the fact that there are both IMREAD_REDUCED_GRAYSCALE_2 and IMREAD_REDUCED_COLOR_2 flags (and similarly for other sizes). But only the IMREAD_REDUCED_GRAYSCALE_2 actually defines the bit for size reduction; IMREAD_REDUCED_COLOR_2 is just that flag ORed with IMREAD_COLOR for convenience. For example, the above value could be obtained in these two ways:
int flags1 = cv::IMREAD_COLOR | cv::IMREAD_REDUCED_GRAYSCALE_4 | cv::IMREAD_IGNORE_ORIENTATION); // = 161
int flags2 = cv::IMREAD_REDUCED_COLOR_4 | cv::IMREAD_IGNORE_ORIENTATION); // = 161
This explains why the existing code with repeated if statements works and is robust to other flags being set even though it only checks the IMREAD_REDUCED_GRAYSCALE_ flags: because those are the values that actually define the bits for the reduction flags.
In retrospect maybe it would have been better to simply call those flags IMREAD_REDUCED_2 etc. and document that the user must OR them with IMREAD_GRAY (which is 0) or IMREAD_COLOR depending on what they want. This would also make it clear that you can combine them with IMREAD_ANYCOLOR, which I believe is currently possible but from the flag names you'd be hard pressed to guess that.
(In retrospect I also think it was a waste to give IMREAD_REDUCED_GRAYSCALE_8 its own bit. It could have just been set to IMREAD_REDUCED_GRAYSCALE_2 | IMREAD_REDUCED_GRAYSCALE_4, given that those flags are otherwise mutally exclusive.)
Reacted by Alexander Alekhin and Ganesh Kathiresan
Activity
saskatchewancatch commentedon Dec 21, 2019
For me: in Version 3.4.9, Win32, running this:
produces:
Are you using a Jpeg2k file by any chance? I don't sense OpenCV handles rescaling in that format, at least I don't see it handled in
grfmt_jpeg2000.cpp.alalek commentedon Dec 24, 2019
I believe this report is about
imdecode()call (notimread()).sturkmen72 commentedon Dec 24, 2019
let me work on this ( i am the person who add IMREAD_REDUCED_* flags)
ganesh-k13 commentedon Jan 8, 2020
Hi @sturkmen72 / @alalek, I'm new to opencv, can I look into this? I think if we add the flag checks in this line, issue will be solved.
sturkmen72 commentedon Jan 8, 2020
see my initial work sturkmen72@fbc0fcd i did not test it well and need resize except jpeg format
ganesh-k13 commentedon Jan 8, 2020
Hi, @sturkmen72 thanks for the response, can I build on this? Or you'll only commit to this issue?
sturkmen72 commentedon Jan 8, 2020
feel free to create a PR i am a bit busy these days
ganesh-k13 commentedon Jan 8, 2020
Thanks, @sturkmen72, will look into it.
Added flag handlers for imread and imdecode | Issue opencv#16203
JamesOldfield commentedon Jan 10, 2020
Many thanks to both of you for providing fixes. I must admit I'm a little surprised that both of are suggesting replacing the sequence of
ifstatements (quoted in my original comment) with:Although this is certainly shorter, I don't think it's simpler. It's not as obvious that it's correct as the original code was, and it's fragile in the face of enum values changing in future (admittedly unlikely). I think there's a chance it won't work with other flags simultaneously e.g. what happens if the user provides
IMREAD_REDUCED_GRAYSCALE_4 | IMREAD_IGNORE_ORIENTATION? It also makes it harder to search - I came across the snippet by searching the codebase for "IMREAD_REDUCED_GRAYSCALE_2", which after this change would provide no hits.If you want to do a bit of refactoring while fixing this issue, a more productive route would probably be to merge
imread_()andimdecode_()into one function. After all, that duplicated code is how this issue arose in the first place. But I'm not an OpenCV contributor so I don't know for sure that would be possible or a good idea.ganesh-k13 commentedon Jan 10, 2020
Sure @alalek, I'll add them. Also like @JamesOldfield suggested, do I merge the common parts of imread and imdecode?
alalek commentedon Jan 10, 2020
Lets solve problems by one and provide small and clear patches. Added duplicated code is not huge for now.
ganesh-k13 commentedon Jan 10, 2020
Sure @alalek, infact I'll fix imdecode only and write tests for now, raise a new PR for imread. My main concern is that neither imread nor imdecode is taking care of flags like
IMREAD_REDUCED_COLOR_2, regarding resize.sturkmen72 commentedon Jan 10, 2020
@JamesOldfield you are right
scale_denom = flags >> 3;is wrongalalek commentedon Jan 11, 2020
You may use this image for tests.
Add tests for existed
imread()first (this feature has been landed without any tests...):and similar cases for
imdecode(). You may take a look on code for reading file content from here.ganesh-k13 commentedon Jan 11, 2020
Thanks @alalek, will do.
ganesh-k13 commentedon Jan 11, 2020
Hi @alalek / @sturkmen72 / @JamesOldfield, there is a small problem with the enum flags, they cannot be used with
&operators, for example:IMREAD_REDUCED_COLOR_2is 17 meaning binary:10001IMREAD_REDUCED_COLOR_4is 33 meaning binary:100001This means anding them will give true which is wrong. It makes sense for greyscale because they are 16, 32 and 64.
Here is my proposed solution, please let me know if any changes needed
sturkmen72 commentedon Jan 11, 2020
@ganesh-k13 why do you think there is a problem on actual code? did you test it. i believe all
IMREAD_REDUCED_*flags withimread()works fineganesh-k13 commentedon Jan 11, 2020
@sturkmen72, IMREAD_REDUCED_COLOR_* is not used anywhere in the code. So this issue did not rise.
sturkmen72 commentedon Jan 11, 2020
i am not an expert but i think there is a bitwise
andso no need IMREAD_REDUCED_COLOR_* in the code
ganesh-k13 commentedon Jan 11, 2020
@sturkmen72, fair point, but if we add some flags in between, it might break something. But yeah, I think if no flags will be added, I'll go ahead with the current method and do the bitwise
and, let me know which is best.JamesOldfield commentedon Jan 13, 2020
@ganesh-k13 I think you might have misunderstood how
IMREAD_flags are allocated, and the ones related to resizing especially. It is not integer values that are assigned meaning, but instead the individual bits are assigned meaning. So there is no possibility that someone would "add some flags in between". Let me show you what bits correspond to what flags, with an example value that I will explain below:In the above example value of 161, the flags are set to ignore JPEG orientation, reduce size by factor 4, and return as a color image, because those three flags are set.
I think you are confused by the fact that there are both
IMREAD_REDUCED_GRAYSCALE_2andIMREAD_REDUCED_COLOR_2flags (and similarly for other sizes). But only theIMREAD_REDUCED_GRAYSCALE_2actually defines the bit for size reduction;IMREAD_REDUCED_COLOR_2is just that flag ORed withIMREAD_COLORfor convenience. For example, the above value could be obtained in these two ways:This explains why the existing code with repeated
ifstatements works and is robust to other flags being set even though it only checks theIMREAD_REDUCED_GRAYSCALE_flags: because those are the values that actually define the bits for the reduction flags.In retrospect maybe it would have been better to simply call those flags
IMREAD_REDUCED_2etc. and document that the user must OR them withIMREAD_GRAY(which is 0) orIMREAD_COLORdepending on what they want. This would also make it clear that you can combine them withIMREAD_ANYCOLOR, which I believe is currently possible but from the flag names you'd be hard pressed to guess that.(In retrospect I also think it was a waste to give
IMREAD_REDUCED_GRAYSCALE_8its own bit. It could have just been set toIMREAD_REDUCED_GRAYSCALE_2 | IMREAD_REDUCED_GRAYSCALE_4, given that those flags are otherwise mutally exclusive.)ganesh-k13 commentedon Jan 13, 2020
@JamesOldfield, thanks for the clarification, it is very clear now :)
asmorkalov commentedon Jan 14, 2020
#16309
Added flag handlers for imread and imdecode | Issue opencv#16203