【问题标题】:OpenCV - Detect hand-drawing shapesOpenCV - 检测手绘形状
【发布时间】:2013-05-16 23:28:12
【问题描述】:

OpenCV 是否可以检测如下手工绘制的几何形状?形状可以是矩形、三角形、圆形、曲线、圆弧、多边形、... 我将开发一个检测这些形状的安卓应用程序。

【问题讨论】:

  • 连接组件的多边形逼近可能。
  • 你能告诉我更多细节吗?有没有关键字或算法名称?
  • 我给出了答案。看看吧。

标签: android opencv feature-detection feature-extraction


【解决方案1】:

嗯,我急忙试了一下。通常你需要骨架化输入。反正。你可以根据它们的点来推断形状。通常一个正方形有 4 个,三角形有 3 个,以此类推。 努力成果:

Canny 结果:

多边形近似:

控制台输出:

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

代码如下:

    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();

【讨论】:

  • 基于轮廓点,我可以确定当前形状是矩形/圆形/...吗?有什么规则可以确定吗?然后,例如,如果形状是圆形,我可以得到关于这个形状的更多信息吗?例如我可以做点什么来获得中心点和半径吗?
  • 嗯,是的,你可以。搜索 cvhoughcircles、contours、cvmoments...祝你好运。
猜你喜欢
  • 1970-01-01
  • 2016-06-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-10
  • 1970-01-01
  • 2021-06-28
  • 1970-01-01
相关资源
最近更新 更多