使用opencv检测图形多边形形状和颜色识别java版shape-color-dector

Adrian Rosebrock大神在pyimagesearch发布了使用opencv3实现了多边形形状检测和颜色识别
我就按照他实现的思路和方法,很粗略写了一份java版本的,欢迎大家学习:

java:

  1. 形状检测
        for( int i = 0; i < contours.size(); i++ ){
            String shape;
            String color;
            MatOfPoint2f  newMatOfPoint2f = new MatOfPoint2f( contours.get(i).toArray() );
            ShapeDector  shapeDector = new ShapeDector();
            shape = shapeDector.detect(contours.get(i),newMatOfPoint2f);
            //根据形状分别计算数量
            switch (shape){
                case "triangle":
                    shapeResult.put("triangle",shapeResult.get("triangle")+1);
                    break;
                case "square":
                    shapeResult.put("square", shapeResult.get("square") + 1);
                    break;
                case "rectangle":
                    shapeResult.put("rectangle", shapeResult.get("rectangle") + 1);
                    break;
                case "pentagon":
                    shapeResult.put("pentagon",shapeResult.get("pentagon")+1);
                    break;
                default:
                    shapeResult.put("circle",shapeResult.get("circle")+1);
                    break;
            }

 2. 颜色检测:
            /**
             * 颜色检测
             */
            ColorDector colorDector = new ColorDector();
            color = colorDector.detect(labImg,contours,i);

详细代码,可以移步github:https://github.com/wohui/shape-color-dector

Python -Adrian Rosebrock

python学习链接:[Determining object color with OpenCV - PyImageSearch]

(https://www.pyimagesearch.com/2016/02/08/opencv-shape-detection/)

最终实现效果
将图片中的图形的形状和颜色全部识别到,还可以继续扩展,将识别结果重新绘制到图片中去。

你可能感兴趣的:(opencv)