14

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?

Matthias Güntert
  • 4,013
  • 6
  • 41
  • 89
AciD IoN
  • 159
  • 1
  • 1
  • 10

3 Answers3

30

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.

Micka
  • 19,585
  • 4
  • 56
  • 74
  • 2
    One can use `Mat1d` instead of `Mat_`. Also, do not forget the parenthesis around `Mat_` expression to cast it to `Mat`, otherwise it will always yield an error. – Burak May 29 '21 at 12:50
28

Your question is not entirely clear to me, but I'm going to assume you are trying to load a float array into a OpenCV Mat object in a single row. First of all, be sure to check the documentation on constructing a Mat in C++. Since you have a 1D array and (I assume) you know the rows and columns you want to give your Mat, you should use this constructor:

cv::Mat::Mat (int rows, int cols, int type, void * data, size_t step = AUTO_STEP)   

Here's a code example:

float data[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
cv::Mat your_matrix = cv::Mat(1, 10, CV_32F, data);

cout << your_matrix.at<float>(0,2) << endl;
cout << your_matrix << endl;

It will output:

3
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Of course you can change the datatype according to your needs (e.g. use int instead of float). You can ignore the AUTO_STEP parameter, but be sure to check the documentation on the usage if you want to use it. Also, if you want to change the structure of your Mat (e.g. split the array into multiple rows) you can achieve this by changing the rows and cols arguments in the constructor:

float data[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
cv::Mat your_matrix = cv::Mat(2, 5, CV_32F, data);

cout << your_matrix.at<float>(1,2) << endl;
cout << your_matrix << endl;

It will output:

8
[1, 2, 3, 4, 5; 
6, 7, 8, 9, 10]

You have now split your Mat object into two rows of 5 columns, instead of 1 row of 10 columns.

In case of Java: If you want to do this in Java, you were already on the right track. However, you probably forgot to specify the rows, columns and channels/depth. Change the rows, cols and CvType according to whatever suits your data as before. You can do the following:

float data[] = new float[]{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
Mat mat = new Mat(1, 10, CvType.CV_32F);
mat.put(0, 0, data);

Be sure to check the Java documentation on Mat as well!

rayryeng
  • 102,964
  • 22
  • 184
  • 193
Jurjen
  • 1,376
  • 12
  • 19
  • Sorry sir for misleading you i need it in java i saw this method but i am unable to do it in java the constructor is not taking the data as argument in java – AciD IoN Apr 03 '17 at 09:29
  • OK no problem, it's pretty similar (at least the logic is). I added some extra code to make it clear for you, hope this helps! – Jurjen Apr 03 '17 at 09:39
  • thankyou sir yes i have forgotten to add channels/depth Cvtype.CV_32F Working perfect Thankyou sir – AciD IoN Apr 03 '17 at 14:34
  • Sir i have another question i am struggling to perform glcm in opencv java searched a lot got no reference and i got come snippets in c++ could u plz take a look at once sir my problem link : http://stackoverflow.com/questions/43172339/opencv-glcm-c-to-java – AciD IoN Apr 03 '17 at 14:36
2

Next, you can do:

cv::Mat newMat(i,j,CV_32F,data);
Quang Hoang
  • 146,074
  • 10
  • 56
  • 74