Initialize the values into Mat object in OpenCV

AciD IoN picture AciD IoN · Apr 3, 2017 · Viewed 34.7k times · Source

I need to initialize these array values directly into a Mat object. I tried using obj.put(i,j,data) but this does not work and the Mat object is still empty. i need this in java

data [] = {103547.0, 2.0959531E7, 5.152769223E9, 1.415924406121E12, 2.0842905E7, 
           4.195143491E9, 1.025510364741E12, 5.000561607E9, 9.99289545049E11, 
           1.332451366923E12}

Can explain to me me how to initialize a new Mat object where I directly insert the array data?

Answer

Micka picture Micka · Apr 3, 2017

Try inline initialization if you want to hardcode those values.:

    // For small matrices you may use comma separated initializers:

    Mat C = (Mat_<double>(3,3) << 0, -1, 0, -1, 5, -1, 0, -1, 0);
    cout << "C = " << endl << " " << C << endl << endl;

stolen from

http://opencvexamples.blogspot.de/2013/09/creating-matrix-in-different-ways.html?m=1

use data array as source as shown in some else's answer if you want to use a (maybe dynamic) array as input.