OpenCV的 - 检测手工绘制形状 [英] OpenCV - Detect hand-drawing shapes

查看:770
本文介绍了OpenCV的 - 检测手工绘制形状的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

能的OpenCV检测由如下手工绘制的几何形状?的形状可以是矩形,三角形,圆形,曲线​​,圆弧,多边形,... 我要开发一个Android应用程序,它检测到这些形状。

Could OpenCV detect the geometric shapes which is drawn by hand as below? The shape can be a rectangle, triangle, circle, curve, arc,polygon,... I am going to develop an android application which detect these shapes.

推荐答案

嗯,我试了一下在哈利。通常你需要用缩略的输入。无论如何。您可以推理基础上的点的形状。通常,一个正方形有4个,三角形3,等等。 努力的结果:

Well, I tried it in a harry. Normally you need to skeletonize the input. Anyway. You can reason about the shapes based on their points. Normally a square has 4, a triangle 3, etc. Effort results:

康力的结果:

Canny results:

多边形逼近:

Polygonal approximation:

控制台输出:

contour points:11
contour points:6
contour points:4 
contour points:5

下面是code:

    Mat src=imread("WyoKM.png");
    Mat src_gray(src.size(),CV_8UC1);
    if (src.empty()) exit(-10);
    imshow("img",src);

    /// Convert image to gray and blur it
    cvtColor( src, src_gray, CV_BGR2GRAY );
    threshold(src_gray,src_gray,100,255,src_gray.type());
    imshow("img2",src_gray);

    Mat canny_output;
    vector<vector<Point> > contours;
    vector<Vec4i> hierarchy;

    /// Detect edges using canny
    int thresh=100;
    Canny( src_gray, canny_output, thresh, thresh*2, 3 );
    imshow("canny",canny_output);
    imwrite("canny.jpg",canny_output);

    /// Find contours
    findContours( canny_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );

    // testing the approximate polygon
    cv::Mat result(src_gray.size(),CV_8U,cv::Scalar(255));
    for(int i=0;i<contours.size();i=i+4) //for testing reasons. Skeletonize input.
    {
        std::vector<cv::Point> poly;
        poly.clear();
        cv::approxPolyDP(cv::Mat(contours[i]),poly,
            5, // accuracy of the approximation
            true); // yes it is a closed shape

        // Iterate over each segment and draw it
        std::vector<cv::Point>::const_iterator itp= poly.begin();
        cout<<"\ncontour points:"<<poly.size();
        while (itp!=(poly.end()-1)) {
            cv::line(result,*itp,*(itp+1),cv::Scalar(0),2);
            ++itp;
        }
        // last point linked to first point
        cv::line(result,
            *(poly.begin()),
            *(poly.end()-1),cv::Scalar(20),2);
    }

    imshow("result",result);
    imwrite("results.jpg",result);

    cvWaitKey();

这篇关于OpenCV的 - 检测手工绘制形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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