# How to convert mat to two-dimensional array (int) in android

i want to quantify pixels into 8 gray levels, and i have to change the mat into 2 dimensional array, can any one help me? thank you

Bitmap bitmap = grayscale.copy(Bitmap.Config.ARGB_8888,true);
Utils.bitmapToMat(bitmap,mat);
mat.convertTo(mat, CvType.CV_8U);
int channels = mat.channels();
int row  = mat.rows();
int col = mat.cols() * channels;

double[][] matrix = new double[row][col];


this is the code I use

edit retag close merge delete

Sort by ยป oldest newest most voted

you don't need any conversion or 2d array for this. assuming, your image is 8U, just do:

Core.divide(mat, new Scalar(32), mat);   // 256/32 = 8 (bins)
Core.multiply(mat, new Scalar(32), mat); // 8*32 = 256 (back to orig. value range)


(it's a simple integer division, which does the trick.)

more

thanks for the answer before, but i want to make GLCM matrix from mat, and GLCM method in opencv does not exist yet, so i must change mat to two-dimensional array

( 2017-09-21 07:48:31 -0500 )edit

you can use 2d arrays for your own data, but opencv's memory is consecutive, you'll have to use a 1d array, to access the pixels, like:

byte [] bytes = new byte[mat.total() * mat.channels()];
mat.get(0,0, bytes); // all of them.
byte pixel_at_x_y = bytes[ channels * (y * mat.cols() + x) ];

( 2017-09-21 08:15:34 -0500 )edit

Official site

GitHub

Wiki

Documentation

## Stats

Asked: 2017-09-20 09:20:55 -0500

Seen: 1,266 times

Last updated: Sep 20 '17