2

I've been given a .mat file (three dimensions, single precision floating point), which I'd like to read using VXL. VNL has a few classes/functions for doing this, but I've not found any examples demonstrating their use. I've pieced the following together from the header file, but no data seems to be read in, and when I ask for the dimensions, I get what appears to be uninitialized junk (though some data from the header is read).

#include <vcl_cstdlib.h>
#include <vcl_iostream.h>
#include <vcl_fstream.h>
#include <vnl/vnl_matlab_read.h>
#include <vnl/vnl_vector.h>

int main(int argc, char ** argv)
{

  if (2 != argc)
    {
    std::cerr << "Provide a file." << std::endl;
    return EXIT_FAILURE;
    }

  const char * fileName = argv[1];

  vcl_filebuf fileBuffer;
  if (!fileBuffer.open(fileName,vcl_ios::in))
    {
    std::cerr << "There was a problem opening the file." << std::endl;
    return EXIT_FAILURE;
    }

  vcl_istream stream(&fileBuffer);
  vnl_matlab_readhdr read(stream);

  vcl_cout << read.name() << vcl_endl; // Platform: PCWIN64, Created on: (date and time)
  vcl_cout << read.rows() << vcl_endl; // 1094852661
  vcl_cout << read.cols() << vcl_endl; // 774905933

  vnl_vector<float> data;
  vnl_matlab_read_or_die(stream, data, fileName);
  std::cout << data.size() << std::endl; // 0

  return EXIT_SUCCESS;

}

I can't tell if I'm using the class incorrectly, or if perhaps VNL can't deal with multidimensional arrays?

sudo make install
  • 5,629
  • 3
  • 36
  • 48
  • And just a note, [vxl] would probably have been a good tag for this question, but I don't have enough reputation to propose it. A quick search shows a fair number of other questions about `vxl`, but I don't know how popular a topic should be to justify a tag. Just a thought. – sudo make install Oct 26 '15 at 14:00

1 Answers1

1

Not all MAT-Files are using the same format, it has been changed several times. Reading junk might be an attempt to read a compressed MAT-File with a library which does not support compression.

As I am unable to find any reasonable documentation for the library you chose, I can only recommend an alternative. When saving the MAT-File in matlab, specify the format -v7.3 which is the most recent one. Doing so, Matlab will write a gzip compressed HDF5 file, which comes with an API for C++

In case you don't have access to Matlab, use Octave to convert your Mat-Files to the most recent format. Here the option for the save function is called -hdf5 and not -7.3

Daniel
  • 36,610
  • 3
  • 36
  • 69
  • 1
    Thanks very much for the answer, @Daniel. I ended up using a workaround in R. I read the images in using `R.matlab`, then saved them as a volume using `oro.nifti`. I could then read them into ITK. This is pretty cumbersome, though--I'll give your method a try next time I get a batch of images. +1 for the C++ API documentation! – sudo make install Dec 02 '15 at 07:58