从yaml文件访问opencv对象

时间:2014-01-17 09:37:13

标签: java opencv yaml javacv

以下代码用于读取java中的.yaml文件。实际上我想将文件值解析为地图,并且可以访问其值。

    public static void main(String[] args) {
    // The path of your YAML file.
    final String fileName = "test.yml";      
    ArrayList<String> key = new ArrayList<String>();
    ArrayList<String> value = new ArrayList<String>();
    ArrayList<Object> obj = new ArrayList<Object>();
    opencvmatrix ob = new opencvmatrix();
    Yaml yaml = new Yaml();

    try {
        InputStream ios = new FileInputStream(new File(fileName));
        // Parse the YAML file and return the output as a series of Maps and Lists
        Map< String, Object> result = (Map< String, Object>) yaml.load(ios);
        for (Object name : result.keySet()) {   
            key.add(name.toString());
            obj.add(result.get(name));   
        }     
    } catch (Exception e) {
        e.printStackTrace();
    }     
     System.out.println(key + " " + obj.get(0).getClass());

}

可以在此处找到该文件的语法:.yml。 obje.get(0).getClass()返回读取值类的类型,并返回opencvmatrix对象。我怎样才能访问该文件?当我println System.out.println(key +“”+ obj);我明白了:

[mean, eigenvalues, eigenvectors, projections, labels] [opencvmatrix@254927, 
opencvmatrix@1e5c67f, opencvmatrix@4ace42, [opencvmatrix@12b710e, opencvmatrix@10b6d81,
opencvmatrix@142691b,...

我怎样才能访问该地址?

1 个答案:

答案 0 :(得分:0)

我的问题通过创建一个opencvmatrix类并将其对象强制转换为在地图中返回对象来解决:

 public static void main(String[] args) {
    // The path of your YAML file.
    final String fileName = "test.yml";
    ArrayList<String> key = new ArrayList<String>();
    ArrayList<String> value = new ArrayList<String>();
    ArrayList<Object> obj = new ArrayList<Object>();
    ArrayList<Object> objected = new ArrayList<Object>();
    opencvmatrix obj1 = new opencvmatrix();
    opencvmatrix obj2 = new opencvmatrix();
    opencvmatrix obj3 = new opencvmatrix();
    opencvmatrix obj4 = new opencvmatrix();
    opencvmatrix obj5 = new opencvmatrix();
    ArrayList<opencvmatrix> objects = new ArrayList<opencvmatrix>();
    Yaml yaml = new Yaml();

    try {
        InputStream ios = new FileInputStream(new File(fileName));
        // Parse the YAML file and return the output as a series of Maps and Lists
        Map< String, Object> result = (Map< String, Object>) yaml.load(ios);
        for (Object name : result.keySet()) {
            key.add(name.toString());
            obj.add(result.get(name));                
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

        obj1 =(opencvmatrix)(obj.get(0));
        obj2 =(opencvmatrix)(obj.get(1));
        obj3 =(opencvmatrix)(obj.get(2));
        objected.add((obj.get(3)));
        obj4 =(opencvmatrix)(obj.get(4));

        for (int i = 0; i < 3600; i++) {
            for (int j = 0; j < 19; j++) {

                System.out.print(obj3.data.get(i*j));
            }
            System.out.println();

        }



}