Ask Your Question
0

How to copy a mat object to a integer 2D matrix in opencv java ?

asked 2019-11-11 14:27:09 -0600

S.P gravatar image

updated 2019-11-12 02:39:52 -0600

berak gravatar image

Here is my code below:

System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
    Imgcodecs imageCodecs = new Imgcodecs();
    String file ="File path"; 
    Mat matrix = imageCodecs.imread(file, Imgcodecs.IMREAD_GRAYSCALE);  
     System.out.println("Image Loaded");

Now I want to see the pixel values of mat object "matrix" as an 2D array. I want it to convert or copy as int[][] format. How to do this please suggest.

edit retag flag offensive close merge delete

Comments

I want it to convert or copy as int[][] format

can you explain, why this would be nessecary ? (and what for ?)

berak gravatar imageberak ( 2019-11-12 02:38:26 -0600 )edit

Now I want to see the pixel values of mat object

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

berak gravatar imageberak ( 2019-11-12 02:38:58 -0600 )edit

I have to convert a grayscale matrix to a 2D integer array( or matrix) so that I can do some Operation on that matrix (which I got from image) and get some different images back again from that output matrix. Previously I was doing with a random large sized matrix but now have to do with the matrix from grayscale image. That is the reason I want to convert it to a 2D matrix with row col.

S.P gravatar imageS.P ( 2019-11-14 07:17:31 -0600 )edit

Previously I was working with random 128*!28 matrix. Bu now I have to work with the matrix i got from that grayscale image. I want to pass this matrix from image as argument but unable to do so. Please suggest.

public static void main(String[] args) { //int row,col=128; //int[][] mat = new int[row][col];

    System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
    Imgcodecs imageCodecs = new Imgcodecs();
    String file ="Image file path"; 
    Mat image = imageCodecs.imread(file, Imgcodecs.IMREAD_GRAYSCALE);  
    System.out.println("Image Loaded");  
    System.out.println(image.dump());

    int row=image.rows();
    int col=image.cols();

     for (int i = 1; i < row-1; i++) { 
      for (int j = 1; j < col-1; j++) {
             //subMatrix(i, j, mat);
              submatrix(i,j,image);}
S.P gravatar imageS.P ( 2019-11-14 07:31:16 -0600 )edit

Posting the remaining code below :

public class SampleImage {

 //public static void subMatrix(int startRow, int startCol, int[][] mat) {
public static void subMatrix(int startRow, int startCol,  mat image) {

           int[][] sub = new int[3][3];
    System.out.print("Submatrix for : ");
    System.out.println(startRow + ", " + startCol + " is : ");     
    for (int i = 0; i < 3; i++) {
     for (int j = 0; j < 3; j++) {

         //sub[i][j] = mat[i+(startRow-1)][j+(startCol-1)]; 
         sub[i][j] = image[i+(startRow-1)][j+(startCol-1)]; //Traversing a 3*3 matrix in large matrix 

                     }
                 }

             MultMatrix(startRow,startCol,sub);
            BitMatrix(startRow,startCol,sub);
S.P gravatar imageS.P ( 2019-11-14 07:36:59 -0600 )edit

so that I can do some Operation

what exactly ? why not use opencv for this, too ?

berak gravatar imageberak ( 2019-11-14 08:01:29 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2019-11-14 07:58:08 -0600

berak gravatar image

updated 2019-11-14 08:14:34 -0600

you can access the pixels from java like:

Mat image = ...
byte [] data = new byte[mat.total() * mat.channels()];
image.get(0,0, data);

note, that this is an 1d array, and it's elements are bytes, not integers. you would treat it like:

byte value = data[ (y * mat.cols() + x) * mat.channels() ]

(ofc. mat.channels() == 1 for a grayscale image)

edit flag offensive delete link more

Comments

while i am writing this code getting one error like : "Type mismatch: cannot convert from long to int". at line :-
byte [] data = new byte[image.total() * image.channels()];

And can you please explain which value denotes x and y in expression byte value = data[ (y * mat.cols() + x) * mat.channels() ];

If it is 1D array then how to get pixel value as (i,j) where i is row and j is column of matrix.

and please suggest me how to access the pixel value like cell (0,0),(0,1),(0,2)......(0,i-1) (1,0),(1,1),(1,2).......(i-1),(j-1) like a 2D matrix.

S.P gravatar imageS.P ( 2019-11-14 14:11:57 -0600 )edit

Here I neet to traverse along the large matrix which i got from image and extract 3*3 submatrix within it. code:

  System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
        Imgcodecs imageCodecs = new Imgcodecs();
        String file ="C:\\Users\\Abhishek\\Desktop\\download.jpg"; 
        Mat image = imageCodecs.imread(file, Imgcodecs.IMREAD_GRAYSCALE);  
        System.out.println("Image Loaded");  
         int row=image.rows();
         int col=image.cols();

             int[][] sub = new int[3][3];

            for (int i = 1; i < row-1; i++) { 
                  for (int j = 1; j < col-1; j++) {
                      for ( int p = 0; p < 3; p++) {
                       for ( int q = 0; q < 3; q++) {
                        sub[p][q] = image[p+(i-1)][q+(j-1)];  //I know it is wrong as image is an object 
                           }
                          }

//next step print sub matrix sub[p][q

S.P gravatar imageS.P ( 2019-11-14 14:17:55 -0600 )edit

Please suggest me what to write in line instead of image object in line: sub[p][q] = image[p+(i-1)][q+(j-1)];

Previously I used matrix mat like sub[p][q] = mat[p+(i-1)][q+(j-1)]; But now I can not use image as i don't have any matrix in 2D format from image. Please suggest how to traverse then?

S.P gravatar imageS.P ( 2019-11-14 14:21:48 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2019-11-11 14:27:09 -0600

Seen: 1,606 times

Last updated: Nov 14 '19