'Border around photo when using laplace edge detection on PNG with alpha channel
I'm trying to use matchtemplate with a template with alpha channels and it's not working very well (error 215:failed assertion). I decided to switch to using edge detection but when I used laplacian operator on the image, it results in a border around the colored bits. https://imgur.com/a/cQIFWy7
For reference this is the original image: https://imgur.com/a/sF7t3ww
image = cv2.imread('image.png', cv2.IMREAD_UNCHANGED)
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray_image, (3, 3), cv2.BORDER_DEFAULT)
canned = cv2.Canny(gray_image, 150, 190)
Also if anyone know how to get alpha channels to work with matchtemplate that would also be very helpful.
Thanks
I've tried using both laplacian and canny edge, but they result in a rectangular border around the image.
Previously I also tried to read invisible pixels on pngs using IMREAD_UNCHANGED but it doesn't seem to work, which is why I decided to switch to edge detection.
Solution 1:[1]
As you stated, the image contains alpha channel (transparency). And our job is perform edge detection without considering this transparency.
1. Why does this happen?
To answer this all you have to do is display the image as it is:
img = cv2.imread(f, cv2.IMREAD_UNCHANGED)
cv2.imshow('image', img)
Now when you perform edge detection (Gaussian blur or Canny method) you will find the rectangular border around the colored region.
2. How to solve this?
The image consists of 4 channels:
>>> img.shape[2]
4
The alpha channel contains the distinct foreground and background regions. As a result, we have to consider only the fourth channel of the image for further processing:
edge_img = cv2.Canny(img[:,:,3], 100, 200)
cv2.imshow('Canny edge image', edge_img)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |


