Skip to content

Add Operator overload for multi-channel Mat with literal constant. - #13586

Merged
alalek merged 4 commits into
opencv:3.4from
eightco:Core_bugfix3
Jan 17, 2019
Merged

alalek merged 4 commits into
opencv:3.4from
eightco:Core_bugfix3

Conversation

@eightco

@eightco eightco commented Jan 6, 2019 •

Copy link
Copy Markdown
Contributor

Add Operator override for multi-channel Mat with literal constant.

When performing *= or /= operation on a multi-channel Mat with a literal constant, it is performed normally on all channels.

However, in the case of += -=, the operation result is applied to only one channel.
The reason is that literal constants are converted to Scalar datatypes, then they only apply to one channel.

So I added operator overrides for literal constants.

Simple test

    Mat mat1(2, 2, CV_8UC3, Scalar::all(1));
    mat1 += 3;
    mat1 -= 1;
    EXPECT_EQ(3, mat1.ptr<uchar>(0)[0]);
    EXPECT_EQ(3, mat1.ptr<uchar>(0)[1]);
    EXPECT_EQ(3, mat1.ptr<uchar>(0)[2]);
    mat1 &= 5;
    mat1 |= 12;
    mat1 ^= 6;
    EXPECT_EQ(11, mat1.ptr<uchar>(1)[0]);
    EXPECT_EQ(11, mat1.ptr<uchar>(1)[1]);
    EXPECT_EQ(11, mat1.ptr<uchar>(1)[2]);

eightco added a commit to eightco/opencv that referenced this pull request Jan 6, 2019
eightco added a commit to eightco/opencv that referenced this pull request Jan 6, 2019
@eightco eightco changed the title Add Operator override for multi-channel Mat with literal constant. Add Operator overload for multi-channel Mat with literal constant. Jan 9, 2019
@alalek

alalek commented Jan 10, 2019

Copy link
Copy Markdown
Member

I'm prefer to forbid that cases at all - detect them and emit the error for multi-channel Mats with single constant.

For example, CV_32FC2 is widely used for operation with complex numbers. So instead of adding 1 this patch adds 1 + 1i.

@eightco

eightco commented Jan 10, 2019

Copy link
Copy Markdown
Contributor Author

@alalek
Okay. I understand. you are right.

If so, should we prohibit all operator overloading when it is a literal constant with a multi-channel Mat?
But forbidding implicit casting to Scalar is also not easy.

I think it would be nice to add operator overloading for a literal constant and assert it if it is not for 1 channel.
Or just leave this PR close.

@alalek

alalek commented Jan 10, 2019

Copy link
Copy Markdown
Member

I think it would be nice to add operator overloading for a literal constant and assert it if it is not for 1 channel.

Right, adding overloads for "float" / "double" / "int" arguments should help avoiding of implicit Scalar constructors.

In tests you can use this:
EXPECT_ANY_THROW(mat1 += 3);

@eightco
eightco force-pushed the Core_bugfix3 branch 2 times, most recently from be71286 to 3661760 Compare January 11, 2019 01:06
eightco added a commit to eightco/opencv that referenced this pull request Jan 12, 2019
Comment thread modules/dnn/test/test_tf_importer.cpp Outdated
Mat input;
resize(sample, input, Size(224, 224));
input -= 128; // mean sub
input -= Scalar(128); // mean sub

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dkurt, Scalar::all(128) ?

BTW, Test doesn't test inference result, only loading is tested here.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I'm not sure about mean values for this model.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Scalar(128) is similar to Scalar(128, 0, 0, 0)
Scalar::all(128) is Scalar(128, 128, 128, 128)

@dkurt dkurt Jan 14, 2019 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, I just not sure if we really need to pass [128, 128, 128] or [0, 0, 0] (from accuracy test below it). From the origin repo, they use different value:
https://github.com/petewarden/tf_ios_makefile_example/blob/880c92d2a15d65b32319d43cca5987481069b574/RunModelViewController.mm#L175L209.
So I think we can replace it even to Scalar::all(117).

eightco added a commit to eightco/opencv that referenced this pull request Jan 15, 2019

@alalek alalek left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@alalek
alalek merged commit 3721c8b into opencv:3.4 Jan 17, 2019
@alalek

alalek commented Jan 18, 2019

Copy link
Copy Markdown
Member

Operations += and -= are not consistent with their base "+" / "-" operations

Also this patch breaks compatibility, so I'm going to revert it.

SavyaSanchi-Sharma pushed a commit to SavyaSanchi-Sharma/opencv that referenced this pull request Jul 30, 2026
* Add Operator override for multi-channel Mat with literal constant.

* simple test

* Operator overloading channel constraint for primitive types

* fix some test for opencv#13586
SavyaSanchi-Sharma pushed a commit to SavyaSanchi-Sharma/opencv that referenced this pull request Jul 30, 2026
This reverts commit f2f6c0f
except changes in modules/dnn/test/test_tf_importer.cpp
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.

3 participants