title: OpenCV removing the background with a mask image
body: <p>Given two input i. original image &amp; ii. mask image, what's the best way to remove to the background from the original image. </p>
  
  <p><strong>Original Image</strong></p>
  
  <p><a href="https://i.stack.imgur.com/I8hMl.png" rel="noreferrer"><img src="https://i.stack.imgur.com/I8hMl.png" alt="enter image description here"></a></p>
  
  <p><strong>Mask Image</strong></p>
  
  <p><a href="https://i.stack.imgur.com/L0v0Y.png" rel="noreferrer"><img src="https://i.stack.imgur.com/L0v0Y.png" alt="enter image description here"></a></p>
  
  <p>The final output would contain just the dog without the background and look transparent. I have seen the mask images are also created with OpenCV. Is there a way to just the existing mask image and generate the output image?</p>
  
  <p><strong>Update</strong>
  I tried this </p>
  
  <pre><code>import cv2
  
  # opencv loads the image in BGR, convert it to RGB
  img = cv2.imread("originalImage.png")
  mask = cv2.imread("maskImage.png")
  final = cv2.bitwise_and(img, mask)
  cv2.imwrite("final.png", final)
  </code></pre>
  
  <p><strong>Final Image</strong></p>
  
  <p><a href="https://i.stack.imgur.com/iZj1t.png" rel="noreferrer"><img src="https://i.stack.imgur.com/iZj1t.png" alt="enter image description here"></a></p>
  
  <p>Is there a way to set the background to be transparent?</p>