在 OpenCV java 中声明 Mat

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27338864/
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-11-02 11:39:05  来源:igfitidea点击:

declare Mat in OpenCV java

javaopencv

提问by Kevin S. Miller

How can I create and assign a Mat with Java OpenCV? The C++ version from this pageis

如何使用 Java OpenCV 创建和分配 Mat?此页面的 C++ 版本是

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

What would be the equivalent in Java OpenCV? It seems that the documentation for Java OpenCV is lacking. What does exist often contains C++ code that doesn't work in Java.

Java OpenCV 中的等价物是什么?似乎缺少 Java OpenCV 的文档。存在的内容通常包含在 Java 中不起作用的 C++ 代码。

回答by Kiran

Yes. The documentation is minimal or non existing. An equivalent would be

是的。文档很少或不存在。一个等价物是

Mat img = new Mat( 3, 3, CvType.CV_64FC1 );
int row = 0, col = 0;
img.put(row ,col, 0, -1, 0, -1, 5, -1, 0, -1, 0 );

In opencv java doc(1) for Matclass, see the overloaded putmethod

在 opencv java doc( 1) for Matclass 中,查看重载put方法

public int put(int row, int col, double... data )
public int put(int row, int col, float[] data )
public int put(int row, int col, int[] data )
public int put(int row, int col, short[] data )
public int put(int row, int col, byte[] data )

We can see that for data types other than double, the last parameter is an array and not variable argument type. So if choosing to create Matof different type, we will need to use arrays as below

我们可以看到,对于除 之外的数据类型double,最后一个参数是一个数组,而不是可变参数类型。所以如果选择创建Mat不同的类型,我们将需要使用如下数组

int row = 0, col = 0;
int data[] = {  0, -1, 0, -1, 5, -1, 0, -1, 0 };
//allocate Mat before calling put
Mat img = new Mat( 3, 3, CvType.CV_32S );
img.put( row, col, data );