如何将 OpenCV Keypoint 特征保存到数据库? [英] How to save OpenCV Keypoint features to database?

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

问题描述

我的项目是使用 OpenCV 库识别 Android 上的叶子.我使用ORB检测来获取图像的关键点并使用ORB描述符来获取关键点的特征.这是我使用的代码:

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-功能/

但每次我输入相同的图像,该图像的关键点总是不同的.如果总是不同,我可以将关键点的特征保存到数据库吗?或者我应该保存图像以保存特征数据?如果可以保存到数据库,我该怎么做??

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++ 你能够将数据存储为 YAML,但 Android 尚不可用.

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

要在 Java 中解析 JSON,您可以使用这个易于使用的 库 Google 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 Keypoint 特征保存到数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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