title: How should I remove noise in this image using OpenCV?
body: <p>I'm trying to use <code>cv2.HoughLines</code> to identify the skew angle of the words in this <a href="https://i.stack.imgur.com/K602F.jpg" rel="nofollow noreferrer">image</a>.<br>
  However, after edge detection, it <a href="https://i.stack.imgur.com/ew8h3.jpg" rel="nofollow noreferrer">clearly has too much noise</a>. </p>
  
  <p>I've tried using <code>cv2.medianBlur</code> to remove the noise.<br>
  However, there is <a href="https://i.stack.imgur.com/l0tqR.jpg" rel="nofollow noreferrer">even more noise</a>. </p>
  
  <p>This means that I'm unable to set the minimum line length threshold for hough transform.</p>
  
  <p>What other functions should I be looking at?</p>
  
  <p>Image:<br>
  <a href="https://i.stack.imgur.com/K602F.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/K602F.jpg" alt="enter image description here"></a></p>
  
  <p>After edge detection:<br>
  <a href="https://i.stack.imgur.com/ew8h3.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ew8h3.jpg" alt="enter image description here"></a></p>
  
  <p>Edit: After Rotem's help, my code now identifies images with skew angles between 90 to -90 degrees including 90 but excluding -90. </p>
  
  <pre><code>import numpy as np
  import imutils
  import math
  import pytesseract
  
  img = cv2.imread('omezole.jpg')
  
  resized = imutils.resize(img, width=300)
  gray = cv2.cvtColor(resized,cv2.COLOR_BGR2GRAY)
  th3 = cv2.threshold(gray, 80, 255, cv2.THRESH_BINARY_INV)[1]
  minLineLength = 50
  maxLineGap = 3
  lines = cv2.HoughLinesP(th3, rho=1, theta=np.pi/180, threshold=100, minLineLength=minLineLength, maxLineGap=maxLineGap)
  colLineCopy = cv2.cvtColor(th3,cv2.COLOR_GRAY2BGR)
  
  #Draw but remove all vertical lines, add corresponding angle to ls
  ls = []
  for line in lines:
  
      if line is None:
          angle = 0
      else:
          x1, y1, x2, y2 = line[0].tolist()
          print(line)
          #check for vertical lines since you can't find tan90
          if (x2-x1==0):
              ls.append(-90)
          else:
              ls.append((math.degrees(math.atan((y2-y1)/(x2-x1)))))
              cv2.line(colLineCopy, (x1,y1), (x2,y2), (0,0,250), 2)
  
  #special case of strictly vertical words, if more than 0.2 of the lines are vertical assume, words are vertical
  if ls.count(-90)&gt;len(ls)//5:
      angle = 90
  else:
      for angle in ls:
          if angle &lt; -80:
              ls.remove(angle)
      angle = sum(ls)/len(ls)
  
  rotated = imutils.rotate_bound(resized, -angle)
  cv2.imshow("HoughLinesP", colLineCopy)
  cv2.imshow("rotated", rotated)
  
  gray = cv2.cvtColor(rotated, cv2.COLOR_BGR2GRAY)
  threshINV  = cv2.threshold(gray, 100, 255, cv2.THRESH_BINARY_INV)[1]
  
  cv2.imshow("final", threshINV)
  #Run OCR
  pytesseract.tesseract_cmd = r'C:\\Program Files\\Tesseract-OCR\\tesseract.exe'
  custom_config = r'--psm 11'
  print(pytesseract.image_to_string(threshINV, config = custom_config))
  cv2.waitKey(0)
  cv2.destroyAllWindows
  
  ``
  </code></pre>