title: OpenCV edge detection on PNG image with alpha layer
body: <p>I have the following image of a logo, which is a PNG file with an alpha (transparency) channel. Very common format for logos, but here is the Reuters logo as an example:</p>
  
  <p><a href="https://i.stack.imgur.com/vGEHl.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vGEHl.png" alt="enter image description here"></a></p>
  
  <p>I'm using OpenCV Canny edge detection in Python to turn this into single-colour edge template which can be used for matching with a larger image:</p>
  
  <pre><code>import matplotlib.pyplot as plt
  import cv2
  img = cv2.imread(filepath) # Save above image and add location here
  img_edged = cv2.Canny(img, 100, 200)
  </code></pre>
  
  <p>Unfortunately, this is picking up the rectangular border around the logo, which is being masked by the alpha channel:</p>
  
  <pre><code>plt.imshow(img, cmap='gray')
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/T8pS0.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/T8pS0.png" alt="enter image description here"></a></p>
  
  <p>I have tried the following suggestion, mentioned in response to <a href="https://stackoverflow.com/questions/3803888/opencv-how-to-load-png-images-with-4-channels">this question</a>, but it doesn't resolve this problem:</p>
  
  <pre><code>img = cv2.imread(filepath, cv2.IMREAD_UNCHANGED)
  </code></pre>
  
  <p>(It does make the image display correctly with imshow, but cv2.Canny still picks up the hidden "box" around the logo.)</p>
  
  <p>Is there a way of preprocessing the image to avoid this problem?</p>