我想能夠堅持矩陣到磁盤。 OpenCV的c,C++版本支持cvWrite函數。我不會在Android二進制文件中使用相同的函數。還有其他選擇嗎?Android OpenCV二進制文件是否具有數據持久性功能?
1
A
回答
1
在C/C++,你擁有所有支持的平臺上完整的API:
的Android的Java API不導出持久性API,但一些類提供保存/載入方法爲他們的配置。
0
由於OpenCV4Android還沒有自己的持久性,在我看來,存儲Mat的最普遍的方法是首先將它轉換爲像JSON這樣的數據交換格式。
當你能夠做這種轉換後,你有很大的靈活性來存儲它。 JSON很容易轉換爲字符串和/或通過網絡連接發送。
隨着OpenCV C++ you are able to store data as YAML,但這不適用於Android,但它是Andrey Kamaev指出的。這裏的JSON與YAML具有相同的用途。
要解析Java中的JSON,你可以使用這個易於使用的library Google GSON。
這裏是我第一次做的正是嘗試(我做了一個簡單的測試,它的工作,讓我知道,如果有問題):
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));
obj.addProperty("data", dataString);
Gson gson = new Gson();
String json = gson.toJson(obj);
return json;
} else {
Log.e(TAG, "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. Redis是否具有選定鍵/數據的持久性功能?
- 2. Android - opencv管理器和OpenCV二進制包是否安裝
- 3. Android數據持久性
- 4. Android數據持久性
- 5. ActiveModel的持久性功能
- 6. 檢查文件數據是否爲二進制文件
- 7. blueprintjs是否具有步進功能?
- 8. NumPy是否具有均勻性功能?
- 9. Elixir是否具有類似於Clojure的持久數據結構?
- 10. 序列化持久性/功能性數據結構
- 11. 瞭解純功能持久二叉樹
- 12. 具有數據持久性的jQuery插件模式
- 13. 二進制文件讀取性能C++
- 14. 二進制文件解析:性能
- 15. 性能讀取二進制文件
- 16. .NET二進制文件讀取性能
- 17. 二進制文件生成性能
- 18. python性能處理二進制文件
- 19. 數據持久性
- 20. 持久性數據
- 21. Android Widget ID是否持久
- 22. 如何檢查HDFS文件是否包含二進制數據?
- 23. 原始二進制文件的opencv
- 24. 是否支持構建庫二進制文件(.a/.so)?
- 25. Android(在ARM上)是否具有硬件性能計數器?
- 26. Android上的數據持久性替代
- 27. Android WebView - 表單數據持久性
- 28. Android,如何保存持久性數據?
- 29. Android中的數據短信端口是否具有實際的硬件功能?
- 30. 實現功能/持久性字典數據結構