2016-03-04 83 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的溶液中,該問題正在通過使用陣列中的不正確的數據類型而引起的。而不是使用字節它應該是浮動的。但是我上面鏈接的解決方案要好得多,因爲它在編碼數據之前檢查數據類型。