Ask Your Question

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

asked 2017-09-20 09:20:55 -0500

sriayu gravatar image

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);
                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 flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2017-09-20 11:19:22 -0500

berak gravatar image

updated 2017-09-20 13:04:47 -0500

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

edit flag offensive delete link 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

sriayu gravatar imagesriayu ( 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.channels()];
mat.get(0,0, bytes); // all of them.
byte pixel_at_x_y = bytes[ channels * (y * mat.cols() + x) ];
berak gravatar imageberak ( 2017-09-21 08:15:34 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


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

Seen: 755 times

Last updated: Sep 20 '17