Thanks. The link was very helpful. I can avoid using opencv at the first step. but there is another problem.
Let me explain my problem a little bit more.  The outputs of my network are n (x,y) positions, for a car, and they are in the world coordinate frame. I do some transformation on the output to bring them in the image coordinate frame by using pytorch and kornia and keep the gradient. I wanted to draw some rectangles or ellipses for these points on a mask but for now I just want to consider them as a single point in the mask tensor.
Now I have a target mask (an rgb image with zero and one elements) and want to calculate a metric like IoU between this target mask and my n points which are transformed into image coordinate frame. I wanted to create a mask for these points and easily calculate the IoU with the target mask, but I don’t know how to do it to be able to use backprop and keep gradient. Can I attach the created mask tensor from my points to the graph and do backpropagation?