简体   繁体   English

如何加载用FileStorage用Java保存的OpenCV矩阵?

[英]How to load OpenCV Matrices saved with FileStorage in Java?

In C++, OpenCV has a nice FileStorage class that makes saving and loading Mat a breeze. 在C ++中,OpenCV有一个不错的FileStorage类,使保存和加载Mat变得轻而易举。

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. 文件格式为YAML。

I want to use a Mat that I create with a C++ program in Java, ideally, loading it from the saved YAML file. 我想使用用Java中的C ++程序创建的Mat ,理想情况下,是从保存的YAML文件中加载它。 However, I cannot find an equivalent class to FileStorage in the Java bindings. 但是,我在Java绑定中找不到FileStorage的等效类。 Does one exist? 是否存在? If not, what alternatives do I have? 如果没有,我有什么选择?

One possible solution is to write a YAML parser using a Java library such as yamlbeans or snakeyaml . 一种可能的解决方案是使用Java库(例如yamlbeanssnakeyaml)编写YAML解析器。

I choose to use yamlbeans because the default FileStorage encoding is YAML 1.0, and snakeyaml requires 1.1. 我选择使用yamlbeans,因为默认的FileStorage编码为YAML 1.0,snakeyaml需要1.1。

My C++ code 我的C ++代码

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

Saves the following example YAML file 保存以下示例YAML文件

%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 删除标头“%YAML:1.0”后,可以使用将其加载到Java中

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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM