Blending face in ROI


A beginner in OpenCV and python world. I am trying to display video frame in the oval shaped mirror. I found some rectangular, circular and ellipse shaped ROIs in open cv but it is not working for me as can be seen in case of rectangular ROI. Can some one share an idea how to calculate the ROI of the mirror and blend the video frame in that ROI to perfectly imitate like looking in the mirror. Below is my Code.

background = cv2.imread(r"C:\Users\background\mirror.png")

cap = cv2.VideoCapture(0)
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))      
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))

x = width-420
y = height-400
w = width-400
h = height-200

while True:
    start = time.time()

    ret, frame =

    roi = np.zeros(frame.shape[:2], np.uint8)
    roi = cv2.rectangle(roi, (x, y), (x+w, y+h), 255 ,thickness= -1)

    mask = np.ones_like(frame) * 255
    mask =  cv2.bitwise_and(mask, frame, mask=roi) - cv2.bitwise_and(mask, mask, mask=~roi)
    img1 = mask

    rows,cols,channels = img1.shape
    roi = background[0:rows, 0:cols]

    img2gray = cv2.cvtColor(img1,cv2.COLOR_BGR2GRAY)

    ret, mask = cv2.threshold(img2gray, 10, 255, cv2.THRESH_BINARY)

    mask_inv = cv2.bitwise_not(mask)
    img1_bg = cv2.bitwise_and(roi,roi,mask = mask_inv)
    img2_fg = cv2.bitwise_and(img1,img1,mask = mask)

    dst = cv2.add(img1_bg,img2_fg)
    frame = dst

    k = cv2.waitKey(1) & 0xff                                                      
    if k==ord('q') or k== ord('Q'):                                              


just open the picture in a photo editor and erase the center part. give it transparency. then use imread with IMREAD_UNCHANGED to obtain the alpha channel. you can use that alpha channel then.