Kevin S. Miller
Kevin S. Miller

Reputation: 974

declare Mat in OpenCV java

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

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.

Upvotes: 9

Views: 19792

Answers (1)

kiranpradeep
kiranpradeep

Reputation: 11221

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 Mat class, see the overloaded put method

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 Mat of different type, we will need to use arrays as below

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 );

Upvotes: 17

Related Questions