title: Opencv, Python - How to remove the gray pixels around the date text
body: <p>I am trying to remove the grayish “noise” surrounding the dates using Python/OpenCV to help the <code>OCR</code> (Optical Character Recognition) to recognize the dates.</p>
  <p><a href="https://i.stack.imgur.com/NvHre.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/NvHre.jpg" alt="enter image description here" /></a></p>
  <p>The original image looks like this: <a href="https://static.mothership.sg/1/2017/03/10-Feb-MC-1.jpg" rel="nofollow noreferrer">https://static.mothership.sg/1/2017/03/10-Feb-MC-1.jpg</a></p>
  <p>The python script I tried looked as below.  However, I have other similar images in which the contrast or lighting coditions varies.</p>
  <pre><code>import cv2
  import numpy as np
  
  img = cv2.imread(&quot;mc.jpeg&quot;)
  img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  
  alpha = 3.5
  beta = -2
  
  new = alpha * img + beta
  new = np.clip(new, 0, 255).astype(np.uint8)
  
  cv2.imwrite(&quot;cleaned.png&quot;, new)
  </code></pre>
  <p>I also tried <code>Thresholding</code> and/or <code>adaptiveThresholding</code> and some time, I was able to separate the dates from the grayish background.  Sometimes it was very challenging.  I wonder is there an automatic way to determine the threshold value ?</p>
  <p>Below are example of what I hope to achieve.</p>
  <p><a href="https://i.stack.imgur.com/NvHre.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/NvHre.jpg" alt="enter image description here" /></a></p>
  <p><a href="https://i.stack.imgur.com/GwuSw.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/GwuSw.jpg" alt="enter image description here" /></a></p>
  <p>Blurry Image:
  <a href="https://i.stack.imgur.com/hGv25.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/hGv25.jpg" alt="enter image description here" /></a></p>