Ask Your Question
1

Best way to classify polygons? (triangle, pentagon, hexagon etc.)

asked 2018-05-29 19:43:09 -0600

Hello,

I suppose this is a very easy problem but I am pretty new to computer vision and could not find a way:

Currently I am working on the problem of classifying different polygons such as triangle, pentagon, hexagon.After thresholding, and canny edge detection, polygons seem pretty clear, but I could not find a way to count how many edges they have, so that I could do the classification. I tried to use approxPolyDP and findContours, however could not figure out how to count edges. Also, I tried hough line transformation, but it finds too many lines, and could not make it work good even though I played with its parameters for some time.

Could you please give me some ideas? I would also really appreciate if you have any code examples for that.

PS: I attached some thresholded images, and using C++.

C:\fakepath\1.JPG C:\fakepath\2.JPG C:\fakepath\3.JPG

edit retag flag offensive close merge delete

Comments

sturkmen gravatar imagesturkmen ( 2018-05-29 19:48:39 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2018-05-29 21:42:37 -0600

sjhalayka gravatar image

updated 2018-05-29 21:58:59 -0600

Here is the input image, "cat.png":

image description

Here is C++ code to know how many sides a polygon (n-gon) has, using approxPolyDP. If you find this code useful, then please mark this answer as correct, and upvote me. Thanks!!!

#include <iostream>
using namespace std;

#include <opencv2/opencv.hpp>
using namespace cv;
#pragma comment(lib, "opencv_world340.lib")

int main(void)
{
    Mat cat_img = imread("cat.png", CV_LOAD_IMAGE_GRAYSCALE);

    threshold(cat_img, cat_img, 127, 255, THRESH_BINARY);

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

    findContours(cat_img, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0));

    vector<vector<Point> > contours_poly(contours.size());

    for (int i = 0; i < contours.size(); i++)
        approxPolyDP(Mat(contours[i]), contours_poly[i], 3, true);

    Mat cat_contours = Mat::zeros(cat_img.size(), CV_8UC3);

    for (int i = 0; i < contours_poly.size(); i++)
    {
        if(1 == contours_poly[i].size())
        {
            // a point
            line(cat_contours, contours_poly[i][0], contours_poly[i][0], Scalar(0, 255, 0), 1, 8, 0);
        }
        else if (2 == contours_poly[i].size())
        {
            // a line segment
            line(cat_contours, contours_poly[i][0], contours_poly[i][1], Scalar(0, 0, 255), 1, 8, 0);
        }
        else
        {
            // an n-gon, where n = contours_poly[i].size()
            for (int j = 0; j < contours_poly[i].size() - 1; j++)
                line(cat_contours, contours_poly[i][j], contours_poly[i][j + 1], Scalar(255, 0, 0), 1, 8, 0);

            line(cat_contours, contours_poly[i][contours_poly[i].size() - 1], contours_poly[i][0], Scalar(255, 0, 0), 1, 8, 0);
        }
    }

    imshow("cat img", cat_contours);

    waitKey(0);

    destroyAllWindows();

    return 0;
}

Here is the output:

image description

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2018-05-29 19:43:09 -0600

Seen: 771 times

Last updated: May 29 '18