title: OpenCV squares: filtering output
body: <p>Here is the out put of square-detection example my problem is filter this squares </p>
  
  <p><img src="https://i.stack.imgur.com/Cm8wE.png" alt="http://ozsulastik.com/ocvsquares.png"></p>
  
  <ul>
  <li>first problem is its drawing one than more lines for same area;</li>
  <li>second one is i just need to detect object not all image.</li>
  </ul>
  
  <p>The other problem is i have to take just biggest object except all image.</p>
  
  <p><img src="https://i.stack.imgur.com/YhMpt.png" alt="http://ozsulastik.com/ocvsquares2.png"></p>
  
  <p>Here is a code for detection:</p>
  
  <p><code>
  static void findSquares( const Mat&amp; image, vector >&amp; squares ){</p>
  
  <pre><code>squares.clear();
  
  Mat pyr, timg, gray0(image.size(), CV_8U), gray;
  
  // down-scale and upscale the image to filter out the noise
  pyrDown(image, pyr, Size(image.cols/2, image.rows/2));
  pyrUp(pyr, timg, image.size());
  vector&lt;vector&lt;Point&gt; &gt; contours;
  
  // find squares in every color plane of the image
  for( int c = 0; c &lt; 3; c++ )
  {
      int ch[] = {c, 0};
      mixChannels(&amp;timg, 1, &amp;gray0, 1, ch, 1);
  
      // try several threshold levels
      for( int l = 0; l &lt; N; l++ )
      {
          // hack: use Canny instead of zero threshold level.
          // Canny helps to catch squares with gradient shading
          if( l == 0 )
          {
              // apply Canny. Take the upper threshold from slider
              // and set the lower to 0 (which forces edges merging)
              Canny(gray0, gray, 0, thresh, 5);
              // dilate canny output to remove potential
              // holes between edge segments
              dilate(gray, gray, Mat(), Point(-1,-1));
          }
          else
          {
              // apply threshold if l!=0:
              gray = gray0 &gt;= (l+1)*255/N;
          }
  
          // find contours and store them all as a list
          findContours(gray, contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);
  
          vector&lt;Point&gt; approx;
  
          // test each contour
          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++ )
                  {
                      // find the maximum cosine of the angle between joint edges
                      double cosine = fabs(angle(approx[j%4], approx[j-2], approx[j-1]));
                      maxCosine = MAX(maxCosine, cosine);
                  }
  
                  if( maxCosine &lt; 0.3 )
                      squares.push_back(approx);
              }
          }
      }
  }
  </code></pre>
  
  <p>}
  </code></p>