android - OpenCV - 检测手绘形状

标签 android opencv feature-detection feature-extraction

OpenCV 能否检测到如下所示的手绘几何形状?形状可以是矩形、三角形、圆形、曲线、圆弧、多边形、... 我将开发一个检测这些形状的 android 应用程序。 enter image description here

最佳答案

好吧,我在 harry 上试过了。通常你需要骨架化输入。反正。您可以根据它们的点来推断形状。通常一个正方形有 4 个,三角形有 3 个,等等。 努力成果:

精明的结果: enter image description here

多边形近似: enter image description here

控制台输出:

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

关于android - OpenCV - 检测手绘形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16682840/

相关文章:

c++ - Opencv CAP_PROP_FRAME_COUNT 返回负大数

c++ - 从 Python 调用 C++ opencv 函数(将 cv::Mat 发送到使用 opencv 的 C++ dll)

javascript - 功能检测是否需要用户手势

c++ - 查找点相对于整个图像的 X 和 Y 坐标

android - Android 对选项卡的官方建议是什么?

android - 由于 Google 许可政策,应用程序被 Google Play 商店拒绝

image - 在房间图像中查找所有不同的对象/封闭多边形

c++ - DLIB : Training Shape_predictor for 194 landmarks (helen dataset)

android - 是否可以在运行时更新Android应用程序然后再次重新加载

java - 如何获取动态创建的复选框android的值?