title: How to remove noise in binary image?
body: <p>This is my code, I am trying to delete the mask (noise) from the binary image. What I am getting is white lines left around the noise. I am aware that there is a contour around that noise creating the final white line in the results. any help?</p>
  <h1>Original Image</h1>
  <p><img src="https://i.stack.imgur.com/8AdUp.png" alt="Original Image" /></p>
  <h1>Mask and results</h1>
  <p><img src="https://i.stack.imgur.com/wviWj.png" alt="Mask and results" /></p>
  <h1>Code</h1>
  <pre class="lang-py prettyprint-override"><code>import numpy as np
  import cv2
  from skimage import util
  
  img = cv2.imread('11_otsu.png')
  imgray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  ret, thresh = cv2.threshold(imgray, 127, 255, 0, cv2.THRESH_BINARY)
  contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
  #cv2.drawContours(img, contours, -1, (0,255,0), 2)
  
  # create an empty mask
  mask = np.zeros(img.shape[:2], dtype=np.uint8)
  
  # loop through the contours
  for i, cnt in enumerate(contours):
      # if the contour has no other contours inside of it
      if hierarchy[0][i][2] == -1:
          # if the size of the contour is greater than a threshold
          if cv2.contourArea(cnt) &lt;70:
              cv2.drawContours(mask, [cnt], 0, (255), -1)
              # display result
  
  cv2.imshow(&quot;Mask&quot;, mask)
  
  cv2.imshow(&quot;Img&quot;, img)
  image = cv2.bitwise_not(img, img, mask=mask)
  cv2.imshow(&quot;Mask&quot;, mask)
  cv2.imshow(&quot;After&quot;, image)
  
  cv2.waitKey()
  cv2.destroyAllWindows()
  
  </code></pre>