How do transparent background of png image in opencv?

a naive idea could be drawing found contours which produces like
HORI8TAL

import cv2
import numpy as np
  
img1 = cv2.imread('d:/test/51.jpeg')
ball = cv2.imread('d:/test/pit.png',cv2.IMREAD_REDUCED_GRAYSCALE_2)

contours, hierarchy = cv2.findContours( ball, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours( img1, contours, -1, (0,0,255),2, cv2.LINE_AA, None, 99, (20,30))
cv2.imshow('HORI8TAL', img1)
cv2.waitKey(0)
  
cv2.destroyAllWindows()

( pit.png i changed it using your image’s alpha channel and saves as grayscale)
pit

1 Like