How do transparent background of png image in opencv?

hello
i have an png image
but when i use it in on a opencv window
it can not transparent background

please help to solve it
i want in png image only show lines (not background)

import cv2
import numpy as np
  
img1 = cv2.imread('c:/51.jpg')
ball = cv2.imread('c:/pit.png')

RED= [0,0,255]
constant= cv2.copyMakeBorder(ball,5,5,5,5,cv2.BORDER_CONSTANT,value=RED)

img1[20:219, 30:335] = constant


cv2.imshow('HORI8TAL', img1)


  
cv2.waitKey(0)
cv2.destroyAllWindows()

could you upload here your images (51.jpg , pit.png)



thanks just rename files

untried idea:

invert your lines image (so the lines are white) and use it as a mask with copyTo()

1 Like

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

i don’t know if we can use copyTo() in python

1 Like

yes, we can :}

>>> help(cv2.copyTo)
Help on built-in function copyTo:

copyTo(...)
    copyTo(src, mask[, dst]) -> dst
    .   @brief  This is an overloaded member function, provided for convenience (python)
    .   Copies the matrix to another one.
    .   When the operation mask is specified, if the Mat::create call shown above reallocates the matrix, the newly allocated matrix is initialized with all zeros before copying the data.
    .   @param src source matrix.
    .   @param dst Destination matrix. If it does not have a proper size or type before the operation, it is
    .   reallocated.
    .   @param mask Operation mask of the same size as \*this. Its non-zero elements indicate which matrix
    .   elements need to be copied. The mask has to be of type CV_8U and can have 1 or multiple channels.
2 Likes

thank you very much , your helps were great

i used draw lines to make pitch , because i couldn’t have click in back of images , thanks again i used your helps in my code

after change codes i draw this shape only width line and circle


now i can calibrate field by 4 clicks on 4 red circle on the drawn shape and then 4 clicks on real world image as same , then i can use homography for calbration (manually ) , it is very precise , but this is start of my work , i dont know what changes need to do it automatically , (without need to click on real image) , thanks again for your help