2016-03-04 149 views
1

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

下面我把一些代码,我试图适应执行我想要的功能,但我遇到错误,这些线路:

mat.get(0, 0, data); 

它给人的错误是:

垫数据类型不兼容: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; 
} 

回答

1

发现问题here的溶液中,该问题正在通过使用阵列中的不正确的数据类型而引起的。而不是使用字节它应该是浮动的。但是我上面链接的解决方案要好得多,因为它在编码数据之前检查数据类型。