Skip to content

division with MatExpr #15760

Closed
Closed

Description

@berak
System information (version)
  • OpenCV => 4.1.2-dev
  • Operating System / Platform => win7 32bit
  • Compiler => mingw64 8.1

assuming X a valid Mat and Y an empty/invalid one, the expression X / Y does not throw an exception, but calculates 1 / X instead.

because here (e.b.data is invalid in our case):

else if( e.flags == '/' && e.b.data )
cv::divide(e.a, e.b, dst, e.alpha);
else if( e.flags == '/' && !e.b.data )
cv::divide(e.alpha, e.a, dst );

why does it have e.a and e.alpha in reverse ?
shouldn't it have thrown Sizes of input arguments do not match , like it does in the Y / X case ?

Steps to reproduce
Mat x(3,3,CV_32FC3,Scalar(3,2,1));
Mat y; // invalid
Mat r = x / y;
cout << r << endl;

[0.33333334, 0.5, 1, 0.33333334, 0.5, 1, 0.33333334, 0.5, 1;
0.33333334, 0.5, 1, 0.33333334, 0.5, 1, 0.33333334, 0.5, 1;
0.33333334, 0.5, 1, 0.33333334, 0.5, 1, 0.33333334, 0.5, 1]

Activity

  1. alalek commented on Oct 22, 2019

    @alalek
    Member

    I believe we need check much early than mentioned code (MatOp_Bin::assign()) - somewhere in operator / ()

  2. saskatchewancatch commented on Oct 24, 2019

    @saskatchewancatch
    Contributor

    So in several of the operator / () overloads, in the right places, CV_ERROR will have to be thrown if one of the operands to the expression is invalid.

  3. berak commented on Oct 24, 2019

    @berak
    ContributorAuthor

    btw, similar problem for | & ^ operators here

  4. saskatchewancatch commented on Oct 25, 2019

    @saskatchewancatch
    Contributor

    @berak and @alalek , I think more operator overloads may be suffering from the same issue. Here is an example of the boolean compare operator not doing an expected operation.

    int main()
    {
            Mat a = Mat::ones(2, 2, CV_8U);
            Mat b;
    
            cout << (a == b) << endl;
            return 0;
    }
    

    Produces (in 3.4 dev on Ubuntu)

    [255, 255;
     255, 255]
    

    which doesn't make sense, unless I've totally overlooked something. Element-wise boolean with "nothing" should not return true values.

  5. saskatchewancatch commented on Nov 20, 2019

    @saskatchewancatch
    Contributor

    A list of more operations that are affected, in addition to the ones above:

        Mat ones = Mat::ones(2, 2, CV_8U);
        Mat zeros = Mat::zeros(2, 2, CV_8U);
        Mat empty;
        Mat result;
        int s = 9;
        int alpha = 10;
    
        ...
        // In each case below, result seems to get an empty matrix
        result = ones > empty;
        result = min(empty, s);
        result = min(s, empty);
        result = s + empty;
        result = empty + s;
        result = s - empty;
        result = empty - s;
        result = empty * alpha;
        result = alpha * empty;
        result = -empty;
        result = abs(empty);
        result = empty.t();
    

    In other cases where an exception is actually thrown somewhere, it is not graceful and consistent behaviour between the various MatExpr schemes.

  6. saskatchewancatch commented on Nov 20, 2019

    @saskatchewancatch
    Contributor

    @alalek As an example, here is what I propose.... in MatExpr min for example, to error out gracefully.

    MatExpr min(const Mat& a, const Mat& b)
    {
        CV_INSTRUMENT_REGION();
    
        //Check for emptiness of operands and throw exception if a Mat input is empty.
        if (a.empty() || b.empty())
        {
            CV_Error(Error::StsBadArg, "One or more matrix operands are empty\n");
        }
    
        MatExpr e;
        MatOp_Bin::makeExpr(e, 'm', a, b);
        return e;
    }
    

    Similarly for other expression handlings. The emptiness checks can be done as a separate function.

    I haven't tried this out but I suspect this should work.

    I can have a PR for this.

  7. alalek commented on Nov 20, 2019

    @alalek
    Member

    Looks good to me.

    Please remove trailing "\n" from the error description.
    Also add simple test (in test_operations.cpp) with these checks:

    TEST(Core_MatExpr, empty_check_15760)
    {
        EXPECT_THROW(Mat c = min(Mat(), Mat(), cv::Exception);
        ...
    }
    
  8. saskatchewancatch commented on Nov 21, 2019

    @saskatchewancatch
    Contributor

    #15966 created to start work on this. More checks and tests coming.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions