开发者

OpenCV multiply scalar and a Matrix

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 sugges开发者_运维技巧tions ?


You cannot initialize a Mat expression from an int or double. The solution is to use cv::Scalar, even for single channel Matrices:

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

See http://docs.opencv.org/modules/core/doc/basic_structures.html#matrixexpressions for a list of possible Mat expressions.


Maybe this is a feature of 2.1 or somewhere between 2.1 and current trunk version, but this works fine for me:

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 is coming from:

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

So, sure just use a scalar expression, at least in 2.1 / current SVN branch; what happens if you try the above in 2.0 ?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜