title: How can I select the best set of parameters in the Canny edge detection algorithm implemented in OpenCV?
body: <p>I am working with OpenCV on the Android platform. With the tremendous help from this community and techies, I am able to successfully detect a sheet out of the image.</p>
  
  <p>These are the step I used.</p>
  
  <ol>
  <li>Imgproc.cvtColor()</li>
  <li>Imgproc.Canny()</li>
  <li>Imgproc.GausianBlur()</li>
  <li>Imgproc.findContours()</li>
  <li>Imgproc.approxPolyDP()</li>
  <li>findLargestRectangle()</li>
  <li>Find the vertices of the rectangle</li>
  <li>Find the vertices of the rectangle top-left anticlockwise order using center of mass approach</li>
  <li>Find the height and width of the rectangle just to maintain the aspect ratio and do warpPerspective transformation.</li>
  </ol>
  
  <p>After applying all these steps I can easily get the document or the largest rectangle from an image. But it highly depends on the difference in the intensities of the background and the document sheet. As the <a href="https://en.wikipedia.org/wiki/Canny_edge_detector" rel="noreferrer">Canny edge detector</a> works on the principle of intensity gradient, a difference in intensity is always assumed from the implementation side. That is why Canny took into the account the various threshold parameters.</p>
  
  <ol>
  <li>Lower threshold</li>
  <li>Higher threshold</li>
  </ol>
  
  <p>So if the intensity gradient of a pixel is greater than the higher threshold, it will be added as an edge pixel in the output image. A pixel will be rejected completely if its intensity gradient value is lower than the lower threshold. And if a pixel has an intensity between the lower and higher threshold, it will only be added as an edge pixel if it is connected to any other pixel having the value larger than the higher threshold.</p>
  
  <p>My main purpose is to use Canny edge detection for the document scanning. So how can I compute these thresholds dynamically so that it can work with the both cases of dark and light background?</p>
  
  <p>I tried a lot by manually adjusting the parameters, but I couldn't find any relationship associated with the scenarios.</p>