title: How can I make all background white in a binary image
body: <p><a href="https://i.stack.imgur.com/dFwTm.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/dFwTm.png" alt="Binary Image" /></a></p>
  <p>In the image I would like to have only the lungs in black not the background. The background [Top black and bottom black area of the image] must be white not black. How can I do that in python?. Code that resuls in the image above from an original grayscale image (&quot;image&quot;) is:</p>
  <pre><code>from skimage.filters import sobel
  
  img = cv2.GaussianBlur(image, (5, 5), 0)
  img = cv2.erode(img, None, iterations=2)
  img = cv2.dilate(img, None, iterations=2)
  
  elevation_map = sobel(img)
  
  markers = np.zeros_like(image)
  
  markers[image &lt; 70] = 1
  markers[image &gt; 254] = 2
  segmentation = skimage.morphology.watershed(elevation_map, markers)
  
  fig, ax = plt.subplots(figsize=(7, 7))
  ax.imshow(segmentation, cmap=plt.cm.gray, interpolation='nearest')
  ax.axis('off')
  ax.set_title('segmentation')
  </code></pre>