title: Mask and smooth an image using OpenCV
body: <p>The following code, as explained <a href="https://www.learnopencv.com/filling-holes-in-an-image-using-opencv-python-c/" rel="nofollow noreferrer">here</a>, effectively finds the contours of an object in an image, and fills those contours with white, while setting the background to black. </p>
  
  <pre><code>import cv2
  import numpy as np
  # Read image
  im_in = cv2.imread('bee-02.png', cv2.IMREAD_GRAYSCALE)
  th, im_th = cv2.threshold(im_in, 250, 255, cv2.THRESH_BINARY_INV)
  # Copy the thresholded image.
  im_floodfill = im_th.copy()
  # Mask used to flood filling.
  h, w = im_th.shape[:2]
  mask = np.zeros((h+2, w+2), np.uint8)
  # Floodfill from point (0, 0)
  cv2.floodFill(im_floodfill, mask, (0,0), 255)
  # Invert floodfilled image
  im_floodfill_inv = cv2.bitwise_not(im_floodfill)
  # Combine the two images to get the foreground.
  im_out = im_th | im_floodfill_inv
  # Display images.
  cv2.imshow("Foreground", im_out)
  cv2.waitKey(0)
  </code></pre>
  
  <p>Here's an example of what it does:</p>
  
  <p><a href="http://weknowyourdreams.com/images/bee/bee-02.jpg" rel="nofollow noreferrer">Image of a bee</a> => </p>
  
  <p><a href="https://i.stack.imgur.com/qYcJe.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/qYcJe.png" alt="enter image description here"></a></p>
  
  <p>What's a good way to augment the above code such that the exterior contours of the image are smoothed? If possible, I'd like to use a method that makes it possible to specify the gradation of the smoothing. I guess that one way to do it would be to apply heavy blurring before applying the masking function, but presumably, there's a better way.</p>