title: OpenCV triangle detection
body: <p>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? </p>
  
  <pre><code>static void getSquares(const Mat&amp; image, vector&lt;vector&lt;Point&gt;&gt;&amp; 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&lt;vector&lt;Point&gt; &gt; contours;
  
  int planes = 1;
  int canny = 0;
  
  if (accuracy) {
      planes = 4;
      canny = 1;
  }
  
  for (int c = 0; c &lt; planes; c++)
  {
      int ch[] = {c, 0};
      mixChannels(&amp;timg, 1, &amp;gray0, 1, ch, 1);
  
      for (int l = 0; l &lt; N; l++) {
          if (l == 0 &amp;&amp; canny == 1) {
              Canny(gray0, gray, 0, thresh, 5);
              dilate(gray, gray, Mat(), Point(-1,-1));
          } else gray = gray0 &gt;= (l+1)*255/N;
  
          findContours(gray, contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);
  
          vector&lt;Point&gt; approx;
  
          for (size_t i = 0; i &lt; contours.size(); i++) {
              approxPolyDP(Mat(contours[i]), approx, arcLength(Mat(contours[i]), true)*0.02, true);
  
              if(approx.size() == 4 &amp;&amp; fabs(contourArea(Mat(approx))) &gt; 1000 &amp;&amp; isContourConvex(Mat(approx))) {
                  double maxCosine = 0;
  
                  for (int j = 2; j &lt; 5; j++) {
                      double cosine = fabs(angle(approx[j%4], approx[j-2], approx[j-1]));
                      maxCosine = MAX(maxCosine, cosine);
                  }
  
                  if (maxCosine &lt; tolerance) squares.push_back(approx);
              }
          }
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Any help will be appreciated, otherwise if you can suggestion another approach.</p>
  
  <p>Regards, C.</p>