user3131312
user3131312

Reputation: 55

OpenCV triangle detection

There following is a working method to find possible squares in an image. Any suggestions how I can change this to account for triangles instead?

static void getSquares(const Mat& image, vector<vector<Point>>& triangles)
{
squares.clear();

Mat pyr, timg, gray0(image.size(), CV_8U), gray;

pyrDown(image, pyr, Size(image.cols/2, image.rows/2));
pyrUp(pyr, timg, image.size());
vector<vector<Point> > contours;

int planes = 1;
int canny = 0;

if (accuracy) {
    planes = 4;
    canny = 1;
}

for (int c = 0; c < planes; c++)
{
    int ch[] = {c, 0};
    mixChannels(&timg, 1, &gray0, 1, ch, 1);

    for (int l = 0; l < N; l++) {
        if (l == 0 && canny == 1) {
            Canny(gray0, gray, 0, thresh, 5);
            dilate(gray, gray, Mat(), Point(-1,-1));
        } else gray = gray0 >= (l+1)*255/N;

        findContours(gray, contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);

        vector<Point> approx;

        for (size_t i = 0; i < contours.size(); i++) {
            approxPolyDP(Mat(contours[i]), approx, arcLength(Mat(contours[i]), true)*0.02, true);

            if(approx.size() == 4 && fabs(contourArea(Mat(approx))) > 1000 && isContourConvex(Mat(approx))) {
                double maxCosine = 0;

                for (int j = 2; j < 5; j++) {
                    double cosine = fabs(angle(approx[j%4], approx[j-2], approx[j-1]));
                    maxCosine = MAX(maxCosine, cosine);
                }

                if (maxCosine < tolerance) squares.push_back(approx);
            }
        }
    }
}

}

Any help will be appreciated, otherwise if you can suggestion another approach.

Regards, C.

Upvotes: 2

Views: 2916

Answers (1)

herohuyongtao
herohuyongtao

Reputation: 50687

At least you need to change

approx.size() == 4

to

approx.size() == 3

Upvotes: 3

Related Questions