Pro_gram_mer
Pro_gram_mer

Reputation: 817

OpenCV Scalar multiplication gives weird result

In OpenCV, multiplying two scalars returns unexpected result, for example :

cv::Scalar s1(2, 3, 4);
cv::Scalar s2(2, 3, 4);
cv::Scalar s3 = s1 * s2; 

where I expect s3 should be [4,9,16,0] ,however the result is [-21, 12, 16, 0], I would like to know the logic behind it. Thanks!

Upvotes: 2

Views: 263

Answers (1)

Pro_gram_mer
Pro_gram_mer

Reputation: 817

From the comments above I would like to summarize that the source code of operator* is defined in modules/core/include/opencv2/core/types.hpp (see below code) and the logic behind it is quaternion product.

template<typename _Tp> static inline
Scalar_<_Tp> operator * (const Scalar_<_Tp>& a, const Scalar_<_Tp>& b)
{
    return Scalar_<_Tp>(saturate_cast<_Tp>(a[0]*b[0] - a[1]*b[1] - a[2]*b[2] - a[3]*b[3]),
                        saturate_cast<_Tp>(a[0]*b[1] + a[1]*b[0] + a[2]*b[3] - a[3]*b[2]),
                        saturate_cast<_Tp>(a[0]*b[2] - a[1]*b[3] + a[2]*b[0] + a[3]*b[1]),
                        saturate_cast<_Tp>(a[0]*b[3] + a[1]*b[2] - a[2]*b[1] + a[3]*b[0]));
}

Upvotes: 1

Related Questions