如何OpenCV的关键点的功能保存到数据库? [英] How to save OpenCV Keypoint features to database?

查看:503
本文介绍了如何OpenCV的关键点的功能保存到数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目是识别使用OpenCV库在Android上的叶子。我使用的ORB检测来获得图像的关键点,并用ORB描述符来获得关键点的特征。这是code,我使用:

My project is recognition the leaf on Android using OpenCV library. I am using ORB detection to get the keypoint of image and use ORB descriptor to get the feature of the keypoint. This is the code that i use:

bmp=BitmapFactory.decodeResource(getResources(),R.drawable.t1);
Utils.bitmapToMat(bmp, mat);
FeatureDetector detector = FeatureDetector.create(FeatureDetector.ORB);
detector.detect(mat, keypoints);
DescriptorExtractor extractor = DescriptorExtractor.create(DescriptorExtractor.ORB);
extractor.compute(mat, keypoints, features);

来源: http://answers.opencv.org/question/6260/orb -features /

但我每次输入相同的图像,该图像总是不同的关键点。我可以关键点的功能保存到数据库中,如果总是有什么不同?或者我应该保存图像保存功能的数据? 如果能保存到数据库中,我怎么能做到这一点?

But every i input the same image, the keypoint of that image always different. Can i save the feature of keypoint to database if that always different? Or should i save the image to save the feature data? If that can save to database, how can i do that??

推荐答案

在我看来,以保存关键点的最普遍的方法是首先将它们转换为像JSON数据交换格式。

In my opinion the most universal way to store the keypoints is to first convert them to a data-interchange format like JSON.

在你能够做到这一点的转换有很大的灵活性来储存。 JSON是很容易通过网络连接转换为字符串和/或发送。

After you are able to do that conversion you have a lot of flexibility to store it. JSON is easily converted to a String and/or sent through a network connection.

使用OpenCV的C ++ <一href="http://docs.opencv.org/modules/core/doc/xml_yaml_persistence.html#void%20FileStorage%3a%3arelease%28%29"相对=nofollow>你是能够存储数据YAML ,但事实并非可用于Android呢。

With OpenCV C++ you are able to store data as YAML, but that is not available for Android yet.

要解析JSON在Java中,你可以使用这个简单易用库谷歌GSON

To parse JSON in Java you can use this easy to use library Google GSON.

这里是我第一次尝试这样做正是:

And here is my first attempt to do exactly that:

 public static String keypointsToJson(MatOfKeyPoint mat){
    if(mat!=null && !mat.empty()){          
        Gson gson = new Gson();

        JsonArray jsonArr = new JsonArray();            

        KeyPoint[] array = mat.toArray();
        for(int i=0; i<array.length; i++){
            KeyPoint kp = array[i];

            JsonObject obj = new JsonObject();

            obj.addProperty("class_id", kp.class_id); 
            obj.addProperty("x",        kp.pt.x);
            obj.addProperty("y",        kp.pt.y);
            obj.addProperty("size",     kp.size);
            obj.addProperty("angle",    kp.angle);                          
            obj.addProperty("octave",   kp.octave);
            obj.addProperty("response", kp.response);

            jsonArr.add(obj);               
        }

        String json = gson.toJson(jsonArr);         

        return json;
    }
    return "{}";
}

public static MatOfKeyPoint keypointsFromJson(String json){
    MatOfKeyPoint result = new MatOfKeyPoint();

    JsonParser parser = new JsonParser();
    JsonArray jsonArr = parser.parse(json).getAsJsonArray();        

    int size = jsonArr.size();

    KeyPoint[] kpArray = new KeyPoint[size];

    for(int i=0; i<size; i++){
        KeyPoint kp = new KeyPoint(); 

        JsonObject obj = (JsonObject) jsonArr.get(i);

        Point point = new Point( 
                obj.get("x").getAsDouble(), 
                obj.get("y").getAsDouble() 
        );          

        kp.pt       = point;
        kp.class_id = obj.get("class_id").getAsInt();
        kp.size     =     obj.get("size").getAsFloat();
        kp.angle    =    obj.get("angle").getAsFloat();
        kp.octave   =   obj.get("octave").getAsInt();
        kp.response = obj.get("response").getAsFloat();

        kpArray[i] = kp;
    }

    result.fromArray(kpArray);

    return result;
}

这篇关于如何OpenCV的关键点的功能保存到数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆