Ask Your Question
1

Reading mat files in java

asked 2013-12-18 00:59:37 -0600

bginer gravatar image

Hi, I can read an image in java using Highgui.imread(filename), but how to load an Mat file that has been saved on the file system ??

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2013-12-18 03:06:38 -0600

itay gravatar image

updated 2013-12-18 03:16:19 -0600

don't save it as mat file, you can save the matrix as dat file by doing this this:

in matlab:

fid=fopen('test.dat','w+');
fprintf(fid,'%d\n',currFrame_Cropped_Gray);
fclose(fid);

This will save your matrix as 1 column in a dat file, so after it you can read in java line by line.

now in java:

private Mat ReadDemoFromFile()
{
    Mat tempmat =  new Mat(new Size(701, 701), CvType.CV_8U);
    InputStream in;
        try {
            in = new FileInputStream(Environment.getExternalStorageDirectory() + "/DCIM/Frames/test.dat");
            InputStreamReader is = new InputStreamReader(in);

            BufferedReader br = new BufferedReader(is);
            String read;
            for(int i = 0 ; i< 701; i++)
                for(int j = 0 ; j < 701 ; j++)
                {
                    try {
                        read = br.readLine();

                        tempmat.put(j, i, Integer.parseInt(read));
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }

            return tempmat;
        } catch (FileNotFoundException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }



    return tempmat;


}

Here I saved the data file in my device, if you want you can save the file in the RAW folder inside your project. This is an example of what I did, so change all the parameters and sizes as you wish.

edit flag offensive delete link more

Comments

But what if you dont the size of the mat ? suppose reading from a raw file ?

bginer gravatar imagebginer ( 2014-01-23 03:53:38 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2013-12-18 00:59:37 -0600

Seen: 2,266 times

Last updated: Dec 18 '13