title: Remove background text and noise from an image using image processing with OpenCV
body: <p>I have these images</p>
  
  <p><a href="https://i.stack.imgur.com/VOfgD.png" rel="noreferrer"><img src="https://i.stack.imgur.com/VOfgD.png" alt="enter image description here"></a></p>
  
  <p><a href="https://i.stack.imgur.com/TdRKg.png" rel="noreferrer"><img src="https://i.stack.imgur.com/TdRKg.png" alt="enter image description here"></a></p>
  
  <p>For which I want to remove the text in the background. Only the <code>captcha characters</code> should remain(i.e K6PwKA, YabVzu). The task is to identify these characters later using tesseract.</p>
  
  <p>This is what I have tried, but it isn't giving much good accuracy.</p>
  
  <pre><code>import cv2
  import pytesseract
  
  pytesseract.pytesseract.tesseract_cmd = r"C:\Users\HPO2KOR\AppData\Local\Tesseract-OCR\tesseract.exe"
  img = cv2.imread("untitled.png")
  gray_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  gray_filtered = cv2.inRange(gray_image, 0, 75)
  cv2.imwrite("cleaned.png", gray_filtered)
  </code></pre>
  
  <p>How can I improve the same?</p>
  
  <p><strong>Note :</strong> 
  I tried all the suggestion that I was getting for this question and none of them worked for me.</p>
  
  <p><strong>EDIT :</strong> 
  According to Elias, I tried finding the color of the captcha text using photoshop by converting it to grayscale which came out to be somewhere in between [100, 105]. I then threshold the image based on this range. But the result which I got did not give satisfactory result from tesseract.</p>
  
  <pre><code>gray_filtered = cv2.inRange(gray_image, 100, 105)
  cv2.imwrite("cleaned.png", gray_filtered)
  gray_inv = ~gray_filtered
  cv2.imwrite("cleaned.png", gray_inv)
  data = pytesseract.image_to_string(gray_inv, lang='eng')
  </code></pre>
  
  <p>Output :</p>
  
  <pre><code>'KEP wKA'
  </code></pre>
  
  <p>Result : </p>
  
  <p><a href="https://i.stack.imgur.com/AqYeO.png" rel="noreferrer"><img src="https://i.stack.imgur.com/AqYeO.png" alt="enter image description here"></a></p>
  
  <p><strong>EDIT 2 :</strong></p>
  
  <pre><code>def get_text(img_name):
      lower = (100, 100, 100)
      upper = (104, 104, 104) 
      img = cv2.imread(img_name)
      img_rgb_inrange = cv2.inRange(img, lower, upper)
      neg_rgb_image = ~img_rgb_inrange
      cv2.imwrite('neg_img_rgb_inrange.png', neg_rgb_image)
      data = pytesseract.image_to_string(neg_rgb_image, lang='eng')
      return data
  </code></pre>
  
  <p>gives : </p>
  
  <p><a href="https://i.stack.imgur.com/rSrSz.png" rel="noreferrer"><img src="https://i.stack.imgur.com/rSrSz.png" alt="enter image description here"></a></p>
  
  <p>and the text as</p>
  
  <pre><code>GXuMuUZ
  </code></pre>
  
  <p>Is there any way to soften it a little</p>