Counterintuitive/buggy Mat operations with scalars (Bug #2222)
Description
When you write something like:
cv::Mat img(10, 10, CV_8UC3);
img = 10; // all the channels are set to 0, as of OpenCV 2.3.1
img = img - 5; // only the first channel is affected, because the operation is translated to
// img = img - Scalar(5) which again is translated into
// img - img - Scalar(5, 0, 0)
This behaviour is at best counterintuitive. The expected result would be to subtract from all channels, not only from first one.
I would say it's a bug, but I do not know how to fix it without breaking a lot other codes which rely on the default behaviour of
Scalar( param );
to be equivalent to
Scalar( param, 0, 0 );
Of course, I suppose this becomes ugly when you have more than 4 channels.
History
Updated by vasile v over 12 years ago
To make it even more inconsistent, if you write something like
img = img * 2;
All the channels will be updated.
Updated by Vadim Pisarevsky over 12 years ago
we considered different options when we introduced such operations. the current variant may look ugly, but we do not see a better alternative. In any case, as you said, any modifications in semantics will break a lot of code, so let's leave it as is.
btw, to modify an image of more than 4 channels, use Vec:
cv::subtract(image, Vec<uchar, 6>(1, 2, 3, 4, 5, 6), image);
- Status changed from Open to Cancelled