Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

OpenCV's Java bindings Mat.get() gives wierd results

I have a 8UC1 Mat image. I'm trying to convert it to Java 2D byte array with [x][y] coordinates. Here's what I have so far:

byte[][] arr = new byte[mat.cols()][mat.rows()];
        for (int colIndex = 0; colIndex < mat.cols(); colIndex++) {
            mat.get(0, colIndex, arr[colIndex]);
        }

However, that gives me totally scrambled results. For example a mat with .dump() such as this:

[255, 255, 255, 255, 255, 255, 255, 255, 255, 255;
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0;
 255, 255, 255, 255, 255, 255, 255, 255, 255, 255;
 255, 255, 255, 255, 255, 255, 255, 255, 255, 255;
 255, 255, 255, 255, 255, 255, 255, 255, 255, 255]

gives me this (don't mind the -1, that's OK):

-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 
-1 -1 -1 -1 -1 -1 -1 -1 -1 0 
-1 -1 -1 -1 -1 -1 -1 -1 0 0 
-1 -1 -1 -1 -1 -1 -1 0 0 0 
-1 -1 -1 -1 -1 -1 0 0 0 0

OpenCV's Java bindings Mat.get() gives wierd weird results

I have a 8UC1 Mat image. I'm trying to convert it to Java 2D byte array with [x][y] coordinates. Here's what I have so far:

byte[][] arr = new byte[mat.cols()][mat.rows()];
        for (int colIndex = 0; colIndex < mat.cols(); colIndex++) {
            mat.get(0, colIndex, arr[colIndex]);
        }

However, that gives me totally scrambled results. For example a mat with .dump() such as this:

[255, 255, 255, 255, 255, 255, 255, 255, 255, 255;
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0;
 255, 255, 255, 255, 255, 255, 255, 255, 255, 255;
 255, 255, 255, 255, 255, 255, 255, 255, 255, 255;
 255, 255, 255, 255, 255, 255, 255, 255, 255, 255]

gives me this (don't mind the -1, that's OK):

-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 
-1 -1 -1 -1 -1 -1 -1 -1 -1 0 
-1 -1 -1 -1 -1 -1 -1 -1 0 0 
-1 -1 -1 -1 -1 -1 -1 0 0 0 
-1 -1 -1 -1 -1 -1 0 0 0 0

OpenCV's Java bindings Mat.get() gives weird results

I have a 8UC1 Mat image. I'm trying to convert it to Java 2D byte array with [x][y] coordinates. Here's what I have so far:

byte[][] arr = new byte[mat.cols()][mat.rows()];
 for (int colIndex = 0; colIndex < mat.cols(); colIndex++) {
      mat.get(0, colIndex, arr[colIndex]);
  }

However, that gives me totally scrambled results. For example a mat with .dump() such as this:

[255, 255, 255, 255, 255, 255, 255, 255, 255, 255;
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0;
 255, 255, 255, 255, 255, 255, 255, 255, 255, 255;
 255, 255, 255, 255, 255, 255, 255, 255, 255, 255;
 255, 255, 255, 255, 255, 255, 255, 255, 255, 255]

gives me this (don't mind the -1, that's OK):

-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 
-1 -1 -1 -1 -1 -1 -1 -1 -1 0 
-1 -1 -1 -1 -1 -1 -1 -1 0 0 
-1 -1 -1 -1 -1 -1 -1 0 0 0 
-1 -1 -1 -1 -1 -1 0 0 0 0