【问题标题】:OpenCV image descriptors to JSONOpenCV 图像描述符到 JSON
【发布时间】:2016-03-04 21:09:45
【问题描述】:

我想在服务器上存储一些图像描述符,以便在 Android 手机上进行图像匹配时,我可以获取预先计算的图像描述符,而不是即时进行。我已经成功创建了一个可以获取输入图像并输出最佳匹配的应用程序,但是在将图像描述符矩阵放入 JSON 文件时遇到了一些问题。

我在下面放置了一些我试图调整以执行我想要的功能的代码,但是我遇到了这些行的错误:

mat.get(0, 0, data);

它给出的错误是:

Mat 数据类型不兼容:5

描述符矩阵的类型是 CV_32FC1,但它会将其视为 CV_8SC1。完整的代码在下面,想法是我将描述符矩阵传递给 matToJson,然后将输出存储在服务器上,然后使用 matFromJson 检索 JSON 文件的内容。我也无法解析 Base64.DEFAULT,因为它显示错误。任何帮助将不胜感激。

    public static String matToJson(Mat mat){        
    JsonObject obj = new JsonObject();

    if(mat.isContinuous()){
        int cols = mat.cols();
        int rows = mat.rows();
        int elemSize = (int) mat.elemSize();    

        byte[] data = new byte[cols * rows * elemSize];

        mat.get(0, 0, data);

        obj.addProperty("rows", mat.rows()); 
        obj.addProperty("cols", mat.cols()); 
        obj.addProperty("type", mat.type());

        // We cannot set binary data to a json object, so:
        // Encoding data byte array to Base64.
        String dataString = new String(Base64.encode(data, Base64.DEFAULT)); //Error here as well .default does not exist

        obj.addProperty("data", dataString);            

        Gson gson = new Gson();
        String json = gson.toJson(obj);

        return json;
    } else {
        System.out.println("Mat not continuous.");
    }
    return "{}";
}

public static Mat matFromJson(String json){
    JsonParser parser = new JsonParser();
    JsonObject JsonObject = parser.parse(json).getAsJsonObject();

    int rows = JsonObject.get("rows").getAsInt();
    int cols = JsonObject.get("cols").getAsInt();
    int type = JsonObject.get("type").getAsInt();

    String dataString = JsonObject.get("data").getAsString();       
    byte[] data = Base64.decode(dataString.getBytes(), Base64.DEFAULT); 

    Mat mat = new Mat(rows, cols, type);
    mat.put(0, 0, data);

    return mat;
}

【问题讨论】:

    标签: java json opencv mat opencv-features2d


    【解决方案1】:

    找到问题here的解决方案,该问题是由于在数组中使用了不正确的数据类型引起的。而不是使用字节它应该是浮动的。但是我上面链接的解决方案要好得多,因为它会在编码数据之前检查数据类型。

    【讨论】:

      猜你喜欢
      • 2016-02-11
      • 2010-10-27
      • 1970-01-01
      • 2016-02-10
      • 2018-06-28
      • 1970-01-01
      • 2013-11-07
      • 1970-01-01
      • 2017-03-21
      相关资源
      最近更新 更多