title: How can i get perfect edge detection of document in IOS?
body: <p>First of all sorry if question was asked. I am working on an app that can detect the corner of a document. I am right now using <strong>openCV</strong> to detect edge. I have achieved this using openCV but I am not getting the perfect result.</p>
  
  <p>I have also tried the <a href="https://github.com/BradLarson/GPUImage" rel="nofollow">BradLarson GPUImage</a> but I am able how to start with this.</p>
  
  <p>My code that detect the corner of the document but not a perfect result.</p>
  
  <pre><code>   void find_squares(Mat&amp; image, cv::vector&lt;cv::vector&lt;cv::Point&gt;&gt;&amp;squares)
  {
      // blur will enhance edge detection
      Mat blurred(image);
      //cv::resize(image, image, cvSize(0.25, 0.25));
      Mat gray0(blurred.size(), CV_8U), gray;
  
      //medianBlur(image, blurred, 9); //default 9;
      GaussianBlur(image, blurred, cvSize(9, 9), 2.0,2.0);
  
      vector&lt;vector&lt;cv::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;blurred, 1, &amp;gray0, 1, ch, 1);
  
          // try several threshold levels
          const int threshold_level = 4;
          for (int l = 0; l &lt; threshold_level; l++)
          {
              // Use Canny instead of zero threshold level!
              // Canny helps to catch squares with gradient shading
              if (l == 0)
              {
                  Canny(gray0, gray, 10, 20, 3); //
  
                  // Dilate helps to remove potential holes between edge segments
                  dilate(gray, gray, Mat(), cv::Point(-1,-1));
  
              }
              else
              {
                  gray = gray0 &gt;= (l+1) * 255 / threshold_level;
              }
  
              // Find contours and store them in a list
              findContours(gray, contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);
  
              // Test contours
              vector&lt;cv::Point&gt; approx;
              for (size_t i = 0; i &lt; contours.size(); i++)
              {
                  // approximate contour with accuracy proportional
                  // to the contour perimeter
                  approxPolyDP(Mat(contours[i]), approx, arcLength(Mat(contours[i]), true)*0.02, true);
  
                  // Note: absolute value of an area is used because
                  // area may be positive or negative - in accordance with the
                  // contour orientation
                  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; 0.3)
                          squares.push_back(approx);
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>So my questions are:</p>
  
  <p>1) is there any other library that can do this.</p>
  
  <p>2) is there any problem in above code? Should I add some image processing before detection?</p>
  
  <p>3) Can <a href="https://github.com/BradLarson/GPUImage" rel="nofollow">BradLarson GPUImage</a> do this? And if it can then are there sources of sample code for edge detection?</p>