如何加载用Java文件存储保存的OpenCV矩阵? C++中,OpenCV有一个很好的类,它使得保存和加载 Mat < /代码>微不足道。p>

如何加载用Java文件存储保存的OpenCV矩阵? C++中,OpenCV有一个很好的类,它使得保存和加载 Mat < /代码>微不足道。p>,java,yaml,opencv3.0,Java,Yaml,Opencv3.0,这很容易 //To save FileStorage fs(outputFile, FileStorage::WRITE); fs << "variable_name" << variable; //To load FileStorage fs(outputFile, FileStorage::READ); fs["variable_name"] >> variable; //保存 文件存储fs(outputFile,FileStorage::WRITE)

这很容易

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

//To load
FileStorage fs(outputFile, FileStorage::READ);
fs["variable_name"] >> variable;
//保存
文件存储fs(outputFile,FileStorage::WRITE);
fs变量;
文件格式为YAML


<>我想用一个C++程序在java中创建一个<代码> Mat <代码>,理想的是,从保存的YAML文件中加载它。但是,我在Java绑定中找不到与
FileStorage
等效的类。有人存在吗?如果没有,我还有什么选择?

一个可能的解决方案是使用Java库(如或)编写YAML解析器

我选择使用yamlbeans是因为默认的文件存储编码是YAML 1.0,而snakeyaml需要1.1

<我的C++代码< /p>
FileStorage fs(path, FileStorage::WRITE);
fs << "M" << variable;
删除标题“%YAML:1.0”后,我可以使用

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;
    }
}
导入java.io.FileReader;
导入java.io.FileNotFoundException;
导入java.util.List;
导入java.util.Map;
导入java.util.Scanner;
导入org.opencv.core.CvType;
导入org.opencv.core.Mat;
导入net.sourceforge.yamlbeans.YamlException;
导入net.sourceforge.yamlbeans.YamlReader;
公共类YamlMatLoader{
//此嵌套类指定文件中的预期变量
//Mat不能直接使用,因为它缺少行和列变量
受保护静态类存储{
公共int行;
公共int cols;
公共字符串dt;
公开名单数据;
//YamlReader需要空构造函数
公众席{
}
public double[]getData(){
double[]dataOut=新的double[data.size()];
for(int i=0;i
这很好,但在
Map=(Map)reader.read()上速度非常慢@Cecilia我知道你的答案是5岁,但我遇到了类似的问题。我把你的建议(+1)作为起点。我想知道在这期间你是否遇到了一个不同的解决方案?你可以看到我的扭曲
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;
    }
}