Opencv multiply scalar and matrix

后端 未结 5 2241
北海茫月
北海茫月 2021-02-19 06:11

I have been trying to achieve something which should pretty trivial and is trivial in Matlab.

I want to simply achieve something such as:

cv::Ma         


        
相关标签:
5条回答
  • 2021-02-19 06:45

    something like this.

    Mat m = (Mat_<float>(3, 3)<<
                        1, 2, 3,
                        4, 5, 6,
                        7, 8, 9)*5;
    
    0 讨论(0)
  • 2021-02-19 06:51

    OpenCV does in fact support multiplication by a scalar value with overloaded operator*. You might need to initialize the matrix correctly, though.

    float data[] = {1 ,2, 3,
                    4, 5, 6,
                    7, 8, 9};
    cv::Mat m(3, 3, CV_32FC1, data);
    m = 3*m;  // This works just fine
    

    If you are mainly interested in mathematical operations, cv::Matx is a little easier to work with:

    cv::Matx33f mx(1,2,3,
                   4,5,6,
                   7,8,9);
    mx *= 4;  // This works too
    
    0 讨论(0)
  • 2021-02-19 06:52

    For java there is no operator overloading, but the Mat object provides the functionality with a convertTo method.

    Mat dst= new Mat(src.rows(),src.cols(),src.type());
    src.convertTo(dst,-1,scale,offset);
    

    Doc on this method is here

    0 讨论(0)
  • 2021-02-19 06:56

    For big Mats you should use forEach.

    If C++11 is available:

    m.forEach<double>([&](double& element, const int position[]) -> void
    {
    element *= 5;
    }
    );
    
    0 讨论(0)
  • 2021-02-19 07:03
    Mat A = //data;//source Matrix
    Mat B;//destination Matrix
    Scalar alpha = new Scalar(5)//factor
    Core.multiply(A,alpha,b);
    
    0 讨论(0)
提交回复
热议问题