title: How to use OpenCV to create binary mask of images?
body: <p>I want to create a binary mask of an image using Opencv.</p>
  <p><a href="https://img.techpowerup.org/201105/019590-0.jpg" rel="nofollow noreferrer">https://img.techpowerup.org/201105/019590-0.jpg</a> and convert this to</p>
  <p><a href="https://i.stack.imgur.com/EP64S.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/EP64S.png" alt="Text" /></a></p>
  <p>How to do this using OpenCV ?
  I tried running this code block</p>
  <pre><code>import cv2
  import numpy as np
  
  # load image with alpha channel
  img = cv2.imread('019590_0.png', cv2.IMREAD_UNCHANGED)
  
  # extract alpha channel
  alpha = img[:, :, 3]
  
  # threshold alpha channel
  alpha = cv2.threshold(alpha, 0, 255, cv2.THRESH_BINARY)[1]
  
  # save output
  cv2.imwrite('object_alpha.png', alpha)
  </code></pre>
  <p>But this is giving an error : <code>IndexError: index 3 is out of bounds for axis 2 with size 3</code></p>