By Nguyen Minh Binh


2013-05-22 03:08:23 8 Comments

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. enter image description here

1 comments

@William 2013-05-22 15:18:20

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: enter image description here

Polygonal approximation: enter image description here

Console output:

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

Here is the 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();

@Nguyen Minh Binh 2013-05-22 16:03:03

Based on the contour points, can I determine current shape is a rectangle/circle/... ? Is there any rule to determine this? And then, for example, If the shape is a circle, Can I get more information about this shape? e.g. Could I do some thing to get the center point and radius?

@William 2013-05-22 20:30:40

Well, yes you can.Search about cvhoughcircles, contours, cvmoments... Good luck.

Related Questions

Sponsored Content

24 Answered Questions

[SOLVED] Image Processing: Algorithm Improvement for 'Coca-Cola Can' Recognition

6 Answered Questions

14 Answered Questions

[SOLVED] Dilemma: when to use Fragments vs Activities:

0 Answered Questions

How to detect shape in openCV?

18 Answered Questions

[SOLVED] Fling gesture detection on grid layout

3 Answered Questions

[SOLVED] How to detect simple geometric shapes using OpenCV

  • 2012-07-11 00:44:16
  • Registered User
  • 135559 View
  • 43 Score
  • 3 Answer
  • Tags:   opencv

0 Answered Questions

Detect hand drawn shapes from Flowchart OpenCV

1 Answered Questions

[SOLVED] Detect a specific shape using OpenCV

0 Answered Questions

ASL hand sign detection opencv

0 Answered Questions

opencv - Detect and optimize shapes

  • 2013-05-21 15:52:12
  • Nguyen Minh Binh
  • 316 View
  • 1 Score
  • 0 Answer
  • Tags:   android opencv

Sponsored Content