2D形状识别算法 - 寻找指导 [英] 2D Shape recognition algorithm - looking for guidance

查看:176
本文介绍了2D形状识别算法 - 寻找指导的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够验证用户是否正确绘制了形状,从简单的形状开始,如圆形,三角形和更高级的形状,如字母A.

I need the ability to verify that a user has drawn a shape correctly, starting with simple shapes like circle, triangle and more advanced shapes like the letter A.

我需要能够实时计算正确性,例如,如果用户应绘制一个圆形但是绘制一个矩形,我希望能够在绘图时检测到它。

I need to be able to calculate correctness in real time, for example if the user is supposed to draw a circle but is drawing a rectangle, my hope is to be able to detect that while the drawing takes place.

有一些不同的形状识别方法,遗憾的是我没有经验或时间来尝试它们,看看有什么用。

There are a few different approaches to shape recognition, unfortunately I don't have the experience or time to try them all and see what works.

您会为此特定任务推荐哪种方法?

Which approach would you recommend for this specific task?

非常感谢您的帮助。

推荐答案

我们可以将识别定义为检测元素中的特征/特征并将其与已知元素的特征进行比较的能力根据我们的经验。具有相似特征的对象可能是类似的对象。特征的数量和复杂程度越高,我们区分类似对象的能力就越大。

We may define "recognition" as the ability to detect features/characteristics in elements and compare them with features of known elements seen in our experience. Objects with similar features probably are similar objects. The higher the amount and complexity of the features, the greater is our power to discriminate similar objects.

在形状的情况下,我们可以使用它们的几何属性,例如角度数,角度值,边数,边尺寸等。因此,为了完成您的任务,您应该使用图像处理算法从图纸中提取这些特征。

In the case of shapes, we can use their geometric properties such as number of angles, the angles values, number of sides, sides sizes and so forth. Therefore, in order to accomplish your task you should employ image processing algorithms to extract such features from the drawings.

下面我介绍一种非常简单的方法,在实践中展示这一概念。我们将使用角数识别不同的形状。正如我所说:功能的数量和复杂程度越高,我们辨别类似物体的能力就越大。由于我们只使用一个特征,角落的数量,我们可以区分几种不同的形状。具有相同角数的形状将不会被区分。因此,为了改进方法,您可以添加新功能。

Below I present a very simple approach that shows this concept in practice. We gonna recognize different shapes using the numbers of corners. As I said: "The higher the amount and complexity of the features, the greater is our power to discriminate similar objects". Since we are using just one feature, the number of corners, we can differentiate a few different kinds of shapes. Shapes with the same number of corners will not be discriminated. Therefore, in order to improve the approach you might add new features.



更新:

为了实时完成此任务,您可以实时提取这些功能。如果要绘制的对象是三角形并且用户正在绘制任何其他图形的第四面,则您知道他或她没有绘制三角形。关于正确性级别,您可以计算所需对象的特征向量与绘制对象的特征向量之间的距离。

In order to accomplish this task in real time you might extract the features in real time. If the object to be drawn is a triangle and the user is drawing the fourth side of any other figure, you know that he or she is not drawing a triangle. About the level of correctness you might calculate the distance between the feature vector of the desired object and the drawn one.

输入:

算法


  1. 缩小输入图像,因为可以在较低分辨率下检测到所需的特征。

  2. 对每个要独立处理的对象进行分段。

  3. 对于每个对象,提取其功能,在这种情况下,只是角落的数量。

  4. 使用这些功能,对对象形状进行分类。

  1. Scale down the input image since the desired features can ben detected in lower resolution.
  2. Segment each object to be processed independently.
  3. For each object, extract its features, in this case, just the number of corners.
  4. Using the features, classify the object shape.

软件:

下面介绍的软件是用Java开发的,使用 Marvin图像处理框架。但是,您可以使用任何编程语言和工具。

The software presented below was developed in Java and using Marvin Image Processing Framework. However, you might use any programming language and tools.

import static marvin.MarvinPluginCollection.floodfillSegmentation;
import static marvin.MarvinPluginCollection.moravec;
import static marvin.MarvinPluginCollection.scale;

public class ShapesExample {

    public ShapesExample(){
        // Scale down the image since the desired features can be extracted
        // in a lower resolution.
        MarvinImage image = MarvinImageIO.loadImage("./res/shapes.png");
        scale(image.clone(), image, 269);

        // segment each object
        MarvinSegment[] objs = floodfillSegmentation(image);
        MarvinSegment seg;

        // For each object...
        // Skip position 0 which is just the background
        for(int i=1; i<objs.length; i++){
            seg = objs[i];
            MarvinImage imgSeg = image.subimage(seg.x1-5, seg.y1-5, seg.width+10, seg.height+10);
            MarvinAttributes output = new MarvinAttributes();
            output = moravec(imgSeg, null, 18, 1000000);
            System.out.println("figure "+(i-1)+":" + getShapeName(getNumberOfCorners(output)));
        }
    }

    public String getShapeName(int corners){
        switch(corners){
            case 3: return "Triangle";
            case 4: return "Rectangle";
            case 5: return "Pentagon";
        }
        return null;
    }

    private static int getNumberOfCorners(MarvinAttributes attr){
        int[][] cornernessMap = (int[][]) attr.get("cornernessMap");
        int corners=0;
        List<Point> points = new ArrayList<Point>();
        for(int x=0; x<cornernessMap.length; x++){
            for(int y=0; y<cornernessMap[0].length; y++){
                // Is it a corner?
                if(cornernessMap[x][y] > 0){
                    // This part of the algorithm avoid inexistent corners
                    // detected almost in the same position due to noise.
                    Point newPoint = new Point(x,y);
                    if(points.size() == 0){
                        points.add(newPoint); corners++;
                    }else {
                        boolean valid=true;
                        for(Point p:points){
                            if(newPoint.distance(p) < 10){
                                valid=false;
                            }
                        }
                        if(valid){
                            points.add(newPoint); corners++;
                        }
                    }
                }
            }
        }
        return corners;
    }

    public static void main(String[] args) {
        new ShapesExample();
    }
}

软件输出:

figure 0:Rectangle
figure 1:Triangle
figure 2:Pentagon

这篇关于2D形状识别算法 - 寻找指导的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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