Reading mat files in java
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 ??
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.
Asked: 2013-12-18 00:59:37 -0600
Seen: 2,231 times
Last updated: Dec 18 '13