c++ - OpenCV triangle detection -
there following working method find possible squares in image. suggestions how can change account 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; } (int c = 0; c < planes; c++) { int ch[] = {c, 0}; mixchannels(&timg, 1, &gray0, 1, ch, 1); (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; (size_t = 0; < 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; (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 appreciated, otherwise if can suggestion approach.
regards, c.
at least need change
approx.size() == 4
to
approx.size() == 3
Comments
Post a Comment