1

I am trying to find the easiest way to add, subtract a scalar value with a opencv 2.0 cv::Mat class.

Most of the existing function allows only matrix-matrix and matrix-scalar operations.

I am looking for a scalar-matrix operations.

I am doing it currently by creating a temporary matrix with the same scalar value and doing required arithmetic operation. Example below..

Mat M(Size(100,100), CV_8U);
Mat temp = Mat::ones(100, 100, CV_8U)*255; 
M = temp-M;

But I think there should be better/easier ways to do it.

Any suggestions ?

4

2 回答 2

5

您不能从 int 或 double 初始化 Mat 表达式。解决方案是使用 cv::Scalar,即使对于单通道矩阵:

Mat M = Mat::ones(Size(100, 100), CV_8U);
M = Scalar::all(255) - M;

有关可能的 Mat 表达式列表,请参阅http://docs.opencv.org/modules/core/doc/basic_structures.html#matrixexpressions

于 2010-06-13T21:40:44.590 回答
0

也许这是 2.1 或 2.1 和当前主干版本之间的某个功能,但这对我来说很好:

Mat cc = channels[k];
    double fmin,fmax;
    cv::minMaxLoc( cc, &fmin, &fmax );
    if( fmax > 1.0 )
        fmax = 255.0 ;
else
fmax = 1.0;
cc = ( cc / (fmax + 1e-9) );

渠道来自:

channels = vector<Mat>(3);
cv::split( img, channels );

所以,确保只使用标量表达式,至少在 2.1 / 当前 SVN 分支中;如果您在 2.0 中尝试上述操作会发生什么?

于 2010-11-21T18:45:51.250 回答