nawara
nawara

Reputation: 1167

The opencv Mat object

I'am new to opencv I want to understand the opencv Mat class

For the get method

I try the first one int get(int row, int col, byte[] data) with this example

  Mat mat = Mat.eye( 3, 3, CvType.CV_8UC1 );


  System.out.println(mat.dump());


  byte[] data = new byte[mat.cols() * mat.rows() * (int)mat.elemSize()];

  System.out.println(data.length);-->9

  System.out.println( mat.get(0, 0, data)); -->9

but I can't understand

1) the role of the third argument byte[] data

2) and the result

Upvotes: 0

Views: 1112

Answers (1)

kiranpradeep
kiranpradeep

Reputation: 11221

//create image as 3 * 3 identity matrix - imagine a black square with a white diagonal
//CV_8UC1 => each image pixel to be stored in a single unsigned char ( 1 byte )
//implies each pixel could take value between 0 and 255
Mat img = Mat.eye( 3, 3, CvType.CV_8UC1 );
//allocate memory to read entire img as an array
//image.elemSize() => number of bytes per image pixel
byte[] imgValues = new byte[img.cols() * img.rows() * (int)img.elemSize()];
//print number of elements in array which is 3 * 3 * 1 = 9
System.out.println(imgValues.length);
//entire content in img from offset(0,0) is read into imgValues and 
//returned is number of bytes read which is 9
int numBytesRead = img.get(0, 0, imgValues);
System.out.println(numBytesRead);

Suggestion: Read on single channel( grayscale ) and multi channel images. Then try using CV_8UC3 instead of CV_8UC1 and see the change.

Upvotes: 3

Related Questions