Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

declare Mat in OpenCV java

Tags:

java

opencv

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.

like image 651
Kevin S. Miller Avatar asked Dec 07 '14 02:12

Kevin S. Miller


1 Answers

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 );
like image 66
kiranpradeep Avatar answered Oct 07 '22 00:10

kiranpradeep