我希望将随机OpenCV描述符提取器生成的描述符矩阵存储为数据库实体。
假设desc是描述符矩阵
String s = (desc.reshape(1,1)).dump;为了从数据库中取回矩阵,我做了
Mat desc = new Mat(numberOfRows * numberOfCols * numberOfChannels, 1, type);
/* I stored number of rows and number of cols and type of the matrix separately and pass them to the function that returns the matrix */
String ss[] = s.substring(1, s.length -1).split(", ");
int[] temp = new int[sss.length ];
for(int i = 0; i < sss.length; i++) temp [i] = Integer.parseInt(sss[i]);
MatOfInt int_values = new MatOfInt(temp);
int_values.convertTo(desc, type);
desc = fingerprint.desc(numberOfChannels, numberOfRows);这很好,但花费了很多时间,特别是.split方法和涉及parseInt的循环,因为矩阵中有大量的元素(在我的例子中大约是100 k)。
我的问题是:有没有更好的方法来做到这一点?
我的第二个问题是,来吧,分配一个100 K长的字符串数组并解析它(考虑到这些字符串实际上最多为2-3个字符)不应该真的花2秒的时间,是不是因为Android/Java比较慢?
发布于 2014-12-12 04:59:38
OpenCV为持久化提供了FileStorage类,就像在链接中一样。基本上,Mat映像可以保存为XML/YAML文件,我们以后可以从中读取该文件。下面是一个简单的例子。
//for writing
cv::FileStorage store(“save.yml", cv::FileStorage::WRITE);
store << “mat” << img;
store.release();
//for reading
cv::FileStorage store(“save.yml", cv::FileStorage::READ);
store[“mat”] >> img;
store.release();但是我们在Android中没有FileStorage java包装。Alternative可以在JNI上调用上面的代码,就像在这个https://github.com/zh-wang/Android-Opencv-AR-tutorial/blob/master/AndroidOpencvARTutorial/jni/jni_part.cpp#L372中那样,或者更好地检查Berak的https://stackoverflow.com/a/26815584/1180117。
发布于 2014-12-12 22:28:01
好的,正如Kiran所提到的,我发现了一个类似于贝拉克的解决方案。我的尝试不那么普遍,但对于描述符矩阵来说工作得很好,速度很快,而且不涉及jni。
// for writing
MatOfByte bytes = new MatOfByte(desc.reshape(1, nrows * ncols * nchannels));
byte[] bytes_ = bytes.toArray();
String s = Base64.encodeToString(bytes_, Base64.DEFAULT);
// for reading
byte[] bytes_ = Base64.decode(s, Base64.DEFAULT);
MatOfByte bytes = new MatOfByte(bytes_);
Mat desc = new Mat(nrows * ncols * nchannels, 1, type);
bytes.convertTo(desc, type);
desc = desc.reshape(nchannels, nrows);https://stackoverflow.com/questions/27433261
复制相似问题