Java 在 OpenCV 中将值初始化为 Mat 对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43180053/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 00:44:51  来源:igfitidea点击:

Initialize the values into Mat object in OpenCV

javaopencvmat

提问by AciD IoN

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

我需要将这些数组值直接初始化为一个Mat对象。我尝试使用,obj.put(i,j,data)但这不起作用并且Mat对象仍然是空的。我在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 Matobject where I directly insert the array data?

可以向我解释如何初始化new Mat我直接插入数组数据的对象吗?

采纳答案by Jurjen

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

您的问题对我来说并不完全清楚,但我假设您正在尝试将 a 加载float arrayMat单个行中的 OpenCV对象中。首先,请务必查看有关构建Matin的文档C++。由于您有一个一维数组并且(我假设)您知道rows并且columns您想提供您的Mat,您应该使用这个构造函数

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, 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 intinstead of float). You can ignore the AUTO_STEPparameter, 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 rowsand colsarguments in the constructor:

当然,您可以根据需要更改数据类型(例如使用int而不是float)。您可以忽略该AUTO_STEP参数,但如果要使用它,请务必查看有关用法的文档。此外,如果您想更改您的结构Mat(例如将数组拆分为多行),您可以通过更改构造函数中的rowscols参数来实现此目的:

float data[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 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 Matobject into two rows of 5 columns, instead of 1 row of 10 columns.

您现在已将Mat对象拆分为两行 5 列,而不是 1 行 10 列。

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:

在 Java 的情况下:如果您想在 Java 中执行此操作,那么您已经在正确的轨道上。但是,您可能忘记指定行、列和通道/深度。像以前一样,根据适合您数据的任何内容更改行、列和 CvType。您可以执行以下操作:

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 Matas well!

请务必查看Mat 上Java 文档

回答by Quang Hoang

Next, you can do:

接下来,您可以执行以下操作:

cv::Mat newMat(i,j,CV_32F,data);

回答by Micka

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

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.

如果您想使用(可能是动态的)数组作为输入,请使用数据数组作为源,如其他人的答案所示。