java中使用jmatio读取三维矩阵

基础知识

有关.mat文件,是matlab中的基本文件格式。这个格式主要用来存放matrix,即图像中各个像素点的值。但是.mat文件不能直接通过FileReader进行读取,如果想要在matlab中引用.mat可以直接使用load来加载代码:

load(filepath\filename.mat)

图像处理中会涉及的mat文件格式最后会在由于笔者不能熟练掌握matlab的基本语法,因此使用java来进行.mat文件的读写。但是.mat文件格式不能直接用输入流,因此只能用特殊的jar包来进行读。介绍一下jmatio。

jmatio基本知识

jmatio用于java读取.mat文件的矩阵,通过jar包配置的方式来进行。笔者没有用过maven,因此直接引用了jar包。

《java中使用jmatio读取三维矩阵》 image

可以看到建立了lib文件,同时引入这两个jar包,直接去官网下载即可,不多做介绍:

《java中使用jmatio读取三维矩阵》 image

建立完成后,在Idea中配置ProjectStructure,左上角菜单file下拉就可以看到ProjectStructure了:

《java中使用jmatio读取三维矩阵》 image

配置完成,如果MatFileReader和MLArray能够正常导入,那么就说明配置正确了。建议下载ujmp-complete-0.3.0.jar版本,至少用到现在没有出错。主要使用的三个基本类:

import com.jmatio.io.MatFileReader;
import com.jmatio.types.MLArray;
import com.jmatio.types.MLDouble;

读取三维矩阵

很遗憾,由于jmatio用的人非常少,因此很难查到相关资料。网上几乎所有的例子都是二维矩阵,然而笔者做的项目是一个空间三维的建模。摸索之后大概清楚了应该怎样能正确读取三维矩阵。

读取.mat文件使用MatFileReader类,代码如下:

MatFileReader read = new MatFileReader("data/seg1.mat");//throw IOException
MLArray mlArray = read.getMLArray("I_Global");//配置文件读取出来后的变量名为I_Global

需要引入维度方法getDimensions()这个方法返回的是维度大小size()的向量。如果要查看导入的代码有几行,使用getNDimensions()执行。本例中的三维矩阵mlArray对应的矩阵维度是mlArray.getNDimensions() == 3,同理需要查看每一维的大小使用代码:

sizex = mlArray.getDimensions()[0];
sizey = mlArray.getDimensions()[1];
sizez = mlArray.getDimensions()[2];

由于jmatio只能支持二维矩阵的正常读取工作,因此mlArray.get(var1, var2)这个方法只能支持两个参数。因此实际的矩阵维度并不是sizex*sizey*sizez而是sizex*(sizey*sizez),如果需要拆分第二和第三维必须要知道实际的mlArray中矩阵第二维的组织形式。

这里有个大坑,第二维的组织方式并不是像一般情况下先组织第三维再组织第二维的,直观来说就是和我们的思路反了。一般来说,如果要把一个二维矩阵给转换为一维矩阵,那么坑爹的jmatio是这么做的:

《java中使用jmatio读取三维矩阵》 image

代码如下:

MLDouble d = (MLDouble)mlArray;//转换为MLDouble类型
for (int i = 0; i < d.getDimensions()[0]; i++)
    for (int j = 0; j < d.getDimensions()[1]; j++)
        for (int k = 0; k < d.getDimensions()[2]; k++) {
            //先考虑第二维,再考虑第三维
            matResult[i][j][k] = d.get(i, k * d.getDimensions()[1] + j);
        }

通过测试,结果正确。这样返回的matResult就是正确存储的三维数组了。

引用一下kaggle中大佬的评价:

I see that the MAT file format is not so easy to read from Java – and maybe from some other languages too, e.g. C++. The main issue is that the MEG measurements (“X”) are shaped into a 3D data matrix. For this reason I am preparing simple Python code to transform MAT files into more standard (but much bigger!) CSV files. So the idea would be to download the compact zip/mat files, and then to run this converter on your local machine to get the CSV files. I will post more information later, as soon as the code is ready. Of course this procedure will assume that you can run a simple Python script and have SciPy installed (necessary to have the function loadmat()).

My attempt is not much different from that of Triskelion which provided a converter to the Vowpal Wabbit format or that of robstr in another thread of this forum. But I think that CSV format is more standard and it is supported by any language. Moreover I think that having more options is better.

    原文作者:汐之语private
    原文地址: https://www.jianshu.com/p/7fb1d0169687
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞