Skip to content

imdecode ignores IMREAD_REDUCED_* flags #16203

Closed

Description

@JamesOldfield

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.

Activity

  1. saskatchewancatch commented on Dec 21, 2019

    @saskatchewancatch
    Contributor

    For me: in Version 3.4.9, Win32, running this:

    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.

  2. alalek commented on Dec 24, 2019

    @alalek
    Member

    I believe this report is about imdecode() call (not imread()).

  3. sturkmen72 commented on Dec 24, 2019

    @sturkmen72
    Contributor

    let me work on this ( i am the person who add IMREAD_REDUCED_* flags)

  4. ganesh-k13 commented on Jan 8, 2020

    @ganesh-k13
    Contributor

    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.

  5. sturkmen72 commented on Jan 8, 2020

    @sturkmen72
    Contributor

    see my initial work sturkmen72@fbc0fcd i did not test it well and need resize except jpeg format

  6. ganesh-k13 commented on Jan 8, 2020

    @ganesh-k13
    Contributor

    Hi, @sturkmen72 thanks for the response, can I build on this? Or you'll only commit to this issue?

  7. sturkmen72 commented on Jan 8, 2020

    @sturkmen72
    Contributor

    feel free to create a PR i am a bit busy these days

  8. ganesh-k13 commented on Jan 8, 2020

    @ganesh-k13
    Contributor

    Thanks, @sturkmen72, will look into it.

  9. JamesOldfield commented on Jan 10, 2020

    @JamesOldfield
    Author

    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.

  10. ganesh-k13 commented on Jan 10, 2020

    @ganesh-k13
    Contributor

    Sure @alalek, I'll add them. Also like @JamesOldfield suggested, do I merge the common parts of imread and imdecode?

  11. alalek commented on Jan 10, 2020

    @alalek
    Member

    do I merge the common parts

    Lets solve problems by one and provide small and clear patches. Added duplicated code is not huge for now.

  12. ganesh-k13 commented on Jan 10, 2020

    @ganesh-k13
    Contributor

    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.

  13. sturkmen72 commented on Jan 10, 2020

    @sturkmen72
    Contributor

    IMREAD_REDUCED_GRAYSCALE_4 | IMREAD_IGNORE_ORIENTATION

    @JamesOldfield you are right
    scale_denom = flags >> 3; is wrong

  14. alalek commented on Jan 11, 2020

    @alalek
    Member

    You may use this image for tests.

    Add tests for existed imread() first (this feature has been landed without any tests...):

    String path = findDataFile("../cv/imgproc/stuff.jpg");
    {  // no flags
        Mat m = imread(path);
        ASSERT_FALSE(m.empty());
        EXPECT_EQ(640, m.cols);
        EXPECT_EQ(480, m.rows);
    }
    {  // reduced x2 flag
        Mat m = imread(path, IMREAD_REDUCED_GRAYSCALE_2);
        ASSERT_FALSE(m.empty());
        EXPECT_EQ(320, m.cols);
        EXPECT_EQ(240, m.rows);
    }
    ... other flags
    

    and similar cases for imdecode(). You may take a look on code for reading file content from here.

  15. ganesh-k13 commented on Jan 11, 2020

    @ganesh-k13
    Contributor

    Thanks @alalek, will do.

  16. ganesh-k13 commented on Jan 11, 2020

    @ganesh-k13
    Contributor

    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;
     }
    
  17. sturkmen72 commented on Jan 11, 2020

    @sturkmen72
    Contributor

    @ganesh-k13 why do you think there is a problem on actual code? did you test it. i believe all IMREAD_REDUCED_* flags with imread() works fine

  18. ganesh-k13 commented on Jan 11, 2020

    @ganesh-k13
    Contributor

    @sturkmen72, IMREAD_REDUCED_COLOR_* is not used anywhere in the code. So this issue did not rise.

  19. sturkmen72 commented on Jan 11, 2020

    @sturkmen72
    Contributor

    i am not an expert but i think there is a bitwise and

    so no need IMREAD_REDUCED_COLOR_* in the code

  20. ganesh-k13 commented on Jan 11, 2020

    @ganesh-k13
    Contributor

    @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.

  21. JamesOldfield commented on Jan 13, 2020

    @JamesOldfield
    Author

    @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:

    7 6 5 4 3 2 1 0   Bit index
    
    1 0 1 0 0 0 0 1 = 161, an example value (see below)
    | | | | | | | |
    | | | | | | |  \
    | | | | | | |   - IMREAD_COLOR
    | | | | | |  \
    | | | | | |   --- IMREAD_ANYDEPTH
    | | | | |  \
    | | | | |   ----- IMREAD_ANYCOLOR
    | | | |  \
    | | | |   ------- IMREAD_LOAD_GDAL
    | | |  \
    | | |   --------- IMREAD_REDUCED_GRAYSCALE_2
    | |  \
    | |   ----------- IMREAD_REDUCED_GRAYSCALE_4
    |  \
    |   ------------- IMREAD_REDUCED_GRAYSCALE_8
     \
      --------------- IMREAD_IGNORE_ORIENTATION
    

    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.)

  22. ganesh-k13 commented on Jan 13, 2020

    @ganesh-k13
    Contributor

    @JamesOldfield, thanks for the clarification, it is very clear now :)

  23. asmorkalov commented on Jan 14, 2020

    @asmorkalov
    Contributor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugcategory: imgcodecseffort: few hours

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions