2

In C++, OpenCV has a nice FileStorage class that makes saving and loading Mat a breeze.

It's as easy as

//To save
FileStorage fs(outputFile, FileStorage::WRITE);
fs << "variable_name" << variable;

//To load
FileStorage fs(outputFile, FileStorage::READ);
fs["variable_name"] >> variable;

The file format is YAML.

I want to use a Mat that I create with a C++ program in Java, ideally, loading it from the saved YAML file. However, I cannot find an equivalent class to FileStorage in the Java bindings. Does one exist? If not, what alternatives do I have?

Cecilia
  • 4,512
  • 3
  • 32
  • 75

1 Answers1

3

One possible solution is to write a YAML parser using a Java library such as yamlbeans or snakeyaml.

I choose to use yamlbeans because the default FileStorage encoding is YAML 1.0, and snakeyaml requires 1.1.

My C++ code

FileStorage fs(path, FileStorage::WRITE);
fs << "M" << variable;

Saves the following example YAML file

%YAML:1.0
codebook: !!opencv-matrix
   rows: 1
   cols: 3
   dt: f
   data: [ 1.03692314e+02, 1.82692322e+02, 8.46153831e+00 ]

After I remove the header, "%YAML:1.0", I can load it into Java using

import java.io.FileReader;
import java.io.FileNotFoundException;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

import org.opencv.core.CvType;
import org.opencv.core.Mat;

import net.sourceforge.yamlbeans.YamlException;
import net.sourceforge.yamlbeans.YamlReader;

public class YamlMatLoader {
    // This nested class specifies the expected variables in the file
    // Mat cannot be used directly because it lacks rows and cols variables
    protected static class MatStorage {
        public int rows;
        public int cols;
        public String dt;
        public List<String> data;

        // The empty constructor is required by YamlReader
        public MatStorage() {
        }

        public double[] getData() {
            double[] dataOut = new double[data.size()];
            for (int i = 0; i < dataOut.length; i++) {
                dataOut[i] = Double.parseDouble(data.get(i));
            }

            return dataOut;
        }
    }

    // Loading function
    private Mat getMatYml(String path) {
        try {  
            YamlReader reader = new YamlReader(new FileReader(path));

            // Set the tag "opencv-matrix" to process as MatStorage
            // I'm not sure why the tag is parsed as
            // "tag:yaml.org,2002:opencv-matrix"
            // rather than "opencv-matrix", but I determined this value by
            // debugging
            reader.getConfig().setClassTag("tag:yaml.org,2002:opencv-matrix", MatStorage.class);

            // Read the string
            Map map = (Map) reader.read();

            // In file, the variable name for the Mat is "M"
            MatStorage data = (MatStorage) map.get("M");

            // Create a new Mat to hold the extracted data
            Mat m = new Mat(data.rows, data.cols, CvType.CV_32FC1);
            m.put(0, 0, data.getData());
            return m;
        } catch (FileNotFoundException | YamlException e) {
            e.printStackTrace();
        }
        return null;
    }
}
Cecilia
  • 4,512
  • 3
  • 32
  • 75
  • This work nice, but is very slow on `Map map = (Map) reader.read();` – Daniel Argüelles Jun 17 '17 at 09:42
  • @Cecilia I realise your answer is 5 years old but I'm running into a similar issue. I've used your advice (+1) as a started point. I was wondering if in the meantime you came across a different solution ? You can see my twist on it [here](https://stackoverflow.com/questions/61312690/how-to-elegantly-serialize-and-deserialize-opencv-yaml-calibration-data-in-java) – George Profenza Apr 27 '20 at 21:25