Open cocositu opened 3 weeks ago
~Maybe duplicated https://github.com/opencv/opencv/issues/25165~
Umm, I read again. This behavious has been changed after 4.9.0, but I think OpenCV 4.9.0 behavious is correct
( More precisely, the case where src2 is Scalar is not described in the multiply function... )
- When using an integer as the multiplier, versions 4.5 to 4.8 only the blue channel is correctly multiplied by 2.0, while the green and red channels' pixel values become zero.
- When using a one-dimensional NumPy array (np.array([2.0])) as the multiplier, versions 4.5 to 4.8 correctly multiply all channels (BGR) of the image by the given value. However, in OpenCV 4.9, only the blue channel is correctly multiplied, while the green and red channels' pixel values are set to zero.
Please could you see atirhmertric function behavious at https://docs.opencv.org/4.9.0/d2/de8/group__core__array.html#ga10ac1bfb180e2cfda1701d06c24fdbd6 ?
[50, 50, 50]
2
then dst = [50x2, 50x2, 50x2]
= [100, 100, 100]
[2,3,4]
then dst = [50x2, 50x3, 50x4]
= [100, 150, 200]
[2]
then dst = [50x2, 50x0, 50x0]
= [100, 0, 0]
From https://docs.opencv.org/4.9.0/d2/de8/group__core__array.html#ga979d898a58d7f61c53003e162e7ad89f
Note Saturation is not applied when the output array has the depth CV_32S. You may even get result of an incorrect sign in the case of overflow. (Python) Be careful to difference behaviour between src1/src2 are single number and they are tuple/array. multiply(src,X) means multiply(src,(X,X,X,X)). multiply(src,(X,)) means multiply(src,(X,0,0,0)).
And This behavious is same as C++
#include <iostream>
#include <opencv2/core.hpp>
int main(void)
{
cv::Mat gray_img(400,400,CV_8UC3, cv::Scalar::all(50));
cv::Mat tmp_img_int;
cv::multiply(gray_img, 2, tmp_img_int);
std::cout << "INT = " << cv::format(tmp_img_int(cv::Rect(0,0,2,2)), cv::Formatter::FMT_NUMPY) << std::endl;
cv::Mat tmp_img_scalar;
cv::multiply(gray_img, cv::Scalar(2), tmp_img_scalar);
std::cout << "SCALAR = " << cv::format(tmp_img_scalar(cv::Rect(0,0,2,2)), cv::Formatter::FMT_NUMPY) << std::endl;
return 0;
}
Result
$ make && ./a.out
g++ main.cpp -o a.out \
-I/usr/local/include/opencv4 \
-lopencv_core
INT = array([[[100, 100, 100], [100, 100, 100]],
[[100, 100, 100], [100, 100, 100]]], dtype='uint8')
SCALAR = array([[[100, 0, 0], [100, 0, 0]],
[[100, 0, 0], [100, 0, 0]]], dtype='uint8')
@cocositu If OK, please could you close this issue ?
System Information
OpenCV python version: 4.5.3 and 4.8.1 and 4.9.0 Operating System / Platform: Ubuntu 20.04 and windows 10 Python version: 3.9
Detailed description
The cv.multiply() function exhibits inconsistent and unexpected behavior when processing color images in OpenCV versions 4.5 to 4.8 compared to version 4.9. Specifically:
These anomalies deviate from users' reasonable expectations for the cv.multiply() function, potentially leading to incorrect results or difficult-to-debug issues in applications.
Steps to reproduce
Sample Code
Expected Result
1 For both integer and one-dimensional NumPy array multipliers, the cv.multiply() function should correctly multiply all channels (BGR) of the color image by the specified value, regardless of the OpenCV version.
Actual Result
OpenCV versions 4.5 to 4.8:
OpenCV version 4.9:
Reproduction Steps
Issue submission checklist