opencv Mat对象

时间:2015-03-12 21:05:06

标签: java opencv mat

我是opencv的新手 我想了解opencv Mat类

对于get method

我尝试第一个 int get(int row, int col, byte[] data)使用此示例

  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

但我无法理解

1)第三个参数byte[] data

的作用

2)和结果

1 个答案:

答案 0 :(得分:3)

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

建议:读取单通道(灰度)和多通道图像。然后尝试使用CV_8UC3代替CV_8UC1并查看更改。