title: Otsu Thresholding on a Sobel Filtered Image gives different Results
body: <p>I'm creating a Sudoku solving application on an Android platform and I've run into an issue when processing the image. I'm trying to find the horizontal lines of the puzzle using OpenCV using a Sobel filter and then thresholding with the Otsu algorithm:</p>
  
  <pre><code>Mat kernaly = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,2));
  Mat dy = new Mat();
  Mat close = new Mat();
  Imgproc.Sobel(img, dy, CvType.CV_16S, 0, 2);
  Core.convertScaleAbs(dy, dy);
  Core.normalize(dy,dy,0,255,Core.NORM_MINMAX);
  Imgproc.threshold(dy, close, 0, 255, Imgproc.THRESH_BINARY|Imgproc.THRESH_OTSU);
  Imgproc.morphologyEx(close, close, Imgproc.MORPH_DILATE, kernaly);
  </code></pre>
  
  <p>This method actually works well for most images, for example:</p>
  
  <p><img src="https://i.stack.imgur.com/BPDzO.png" alt="enter image description here"></p>
  
  <p>However, it fails for the following image:</p>
  
  <p><img src="https://i.stack.imgur.com/l20yJ.png" alt="enter image description here"></p>
  
  <p>Can someone explain why the results are so vastly different and the second image above returns only one line?
  Also, should I just use another methodology instead, such as Canny or Hough lines?</p>
  
  <p>Thanks in advance!</p>
  
  <hr>
  
  <p>Edit:
  Using marol's advice, I tried removing as much of the black border as possible without having to warp the image. This is the result when applying the same process above to these reworked images.</p>
  
  <p>Image 1:</p>
  
  <p><img src="https://i.stack.imgur.com/qUWXl.png" alt="Image 1"></p>
  
  <p>Image 2:</p>
  
  <p><img src="https://i.stack.imgur.com/N2vG2.png" alt="Image 2"></p>
  
  <p>As you can see, the results are better as most lines have been detected. However, it's still not good enough. It can be improved by adding in a fixed threshold but that has to be different for each image.</p>
  
  <p>I'll probably just use a new approach as this method does not seem to be robust enough. Any tips will be greatly appreciated.</p>