title: How to use OpenCV to process image so that the text become sharp and clear?
body: <p>Wanted to achieve something like this: <a href="http://www.leptonica.com/binarization.html" rel="noreferrer">http://www.leptonica.com/binarization.html</a></p>
  
  <p>While searching for solutions, most of the answers were general instructions such as advise to look at adaptive filter, gaussian blur, dilation and erosion but none of them provide any sample code to start with (so can play around with the values)..</p>
  
  <p>I know different image require different methods and values to achieve optimum clarity, but I just need some general filter so that the image at least slightly sharper and less noisy compare to the original, before doing any OCR on it.</p>
  
  <p>this is what I've tried so far..</p>
  
  <pre><code>Mat imageMat = new Mat();
  Utils.bitmapToMat(photo, imageMat);
  Imgproc.cvtColor(imageMat, imageMat, Imgproc.COLOR_BGR2GRAY);
  Imgproc.GaussianBlur(imageMat, imageMat, new Size(3, 3), 0);
  Imgproc.adaptiveThreshold(imageMat, imageMat, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY_INV, 5, 4);
  </code></pre>
  
  <p>but being an image processing newb, obviously I don't know what I'm doing XD</p>
  
  <p>original image:
  <img src="https://i.stack.imgur.com/ClGNr.jpg" alt="original image"></p>
  
  <p>after applying the above:
  <img src="https://i.stack.imgur.com/YuIlZ.jpg" alt="image after applying filters"></p>
  
  <p>How to do it correctly?</p>
  
  <p>UPDATE: got it much closer thanks to metsburg, berak and Aurelius</p>
  
  <p>Using the medianBlur method since cvSmooth with CV_MEDIAN is deprecated and replaced with medianBlur:</p>
  
  <pre><code>Imgproc.medianBlur(imageMat, imageMat, 3);
  Imgproc.threshold(imageMat, imageMat, 0, 255, Imgproc.THRESH_OTSU);
  </code></pre>
  
  <p>Result:
  <img src="https://i.stack.imgur.com/8URbW.jpg" alt="using medianblur before applying otsu"></p>
  
  <p>Using back the GaussianBlur method, the result actually is slightly better:</p>
  
  <pre><code>Imgproc.GaussianBlur(imageMat, imageMat, new Size(3, 3), 0);
  Imgproc.threshold(imageMat, imageMat, 0, 255, Imgproc.THRESH_OTSU);
  </code></pre>
  
  <p>Result:
  <img src="https://i.stack.imgur.com/YVtxZ.jpg" alt="using gaussianblur before applying otsu"></p>
  
  <p>For this image the difference is not noticable, so I tried another image which is a photo taken off the computer screen. Computer screen gives a lot of noises (wavy lines) so it is very hard to remove the noise.</p>
  
  <p>Example original image:
  <img src="https://i.stack.imgur.com/Nd1iB.jpg" alt="pcscreen original image"></p>
  
  <p>Directly applying otsu:
  <img src="https://i.stack.imgur.com/weBSZ.jpg" alt="pcscreen directly apply otsu"></p>
  
  <p>using medianBlur before otsu:
  <img src="https://i.stack.imgur.com/HNbw5.jpg" alt="pcscreen using medianBlur before applying otsu"></p>
  
  <p>using GaussianBlur before otsu:
  <img src="https://i.stack.imgur.com/BbhHk.jpg" alt="pcscreen using GaussianBlur before applying otsu"></p>
  
  <p>Seems like gaussian blur is slightly better, however I'm still playing with the settings..
  If anyone can advise on how to improve the computer screen photo further, please, let us know :)
  One more thing.. using this method on the image inside the top link yields horrible results :( see it here: <a href="https://imgur.com/vOZAaE0" rel="noreferrer">http://imgur.com/vOZAaE0</a></p>