2

I'm working a project using opencv. I want to detect shape from a webcam. I already succes to detect square by edit square.c, and now I want to use square.c to detect triangel.

Is it possible to detect triangle from square.c? which part that I have to add or modify?

edit

i've tried adding this code:

else if ( result->total == 3 &&
    fabs ( cvContourArea(result, CV_WHOLE_SEQ)) > 1000 &&
    cvCheckContourConvexity(result))
{
    s = 0;
    for (int i = 0; i < 3; i++)
    {
        if (i >= 1)
        {
            t = fabs( angle(
                      (CvPoint*)cvGetSeqElem(result, i),
                      (CvPoint*)cvGetSeqElem(result, i-1),
                      (CvPoint*)cvGetSeqElem(result, i-2)));
            s =  s > s ? s : t;
        }
    }
}

bu the result is, the triangle detected on the square..

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
Faqih
  • 47
  • 1
  • 8

2 Answers2

0

be i = 1: with

...
(CvPoint*)cvGetSeqElem(result, i),
(CvPoint*)cvGetSeqElem(result, i-1),
(CvPoint*)cvGetSeqElem(result, i-2)));

you access elements 1, 0 and the last element of the sequence. Is that intended?

second: why

for (int i = 0; i < 3; i++)
{
    if (i >= 1)

why not

for (int i = 1; ...
Peter Miehle
  • 5,984
  • 2
  • 38
  • 55
0

You might also consider having a look at one of my previous answers on detecting many triangles in an image.

Here is the primary function to pay attention to in that example:

vector<Point> getAllTriangleVertices(Mat& img, const vector< vector<Point> >& contours)
{
    vector<Point> approxTriangle;
    vector<Point> allTriangleVertices;
    for(size_t i = 0; i < contours.size(); i++)
    {
        approxPolyDP(contours[i], approxTriangle, arcLength(Mat(contours[i]), true)*0.05, true);
        if(approxTriangle.size() == 3)
        {
            copy(approxTriangle.begin(), approxTriangle.end(), back_inserter(allTriangleVertices));
            drawContours(img, contours, i, Scalar(0, 255, 0), CV_FILLED);

            vector<Point>::iterator vertex;
            for(vertex = approxTriangle.begin(); vertex != approxTriangle.end(); ++vertex)
            {
                circle(img, *vertex, 3, Scalar(0, 0, 255), 1);
            }
        }
    }

    return allTriangleVertices;
}

Essentially this function uses contours detected by findContours and draws the triangle vertices on the image as well as fills them.

Hope that helps!

Community
  • 1
  • 1
mevatron
  • 13,911
  • 4
  • 55
  • 72