def overlayimg(back, fore, x, y, w, h):
# Load two images
img1 = np.array(back)
img2 = np.array(fore)
# create new dimensions
r = float((h)) / img2.shape[1]
dim = ((w), int(img2.shape[1] * r))
# Now create a mask of box and create its inverse mask also
img2gray = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
ret, mask = cv2.threshold(img2gray, 10, 255, cv2.THRESH_BINARY)
mask_inv = cv2.bitwise_not(mask)
# resize box and masks
resized_img2 = cv2.resize(img2, dim, interpolation=cv2.INTER_AREA)
resized_mask = cv2.resize(mask, dim, interpolation=cv2.INTER_AREA)
resized_mask_inv = cv2.resize(mask_inv, dim, interpolation=cv2.INTER_AREA)
# I want to put box in co-ordinates, So I create a ROI
rows, cols, channels = resized_img2.shape
roi = img1[y:y+rows, x:x+cols]
# Now black-out the area of box in ROI
img1_bg = cv2.bitwise_and(roi, roi, mask=resized_mask_inv)
# Take only region of box from box image.
img2_fg = cv2.bitwise_and(resized_img2, resized_img2, mask=resized_mask)
# Put box in ROI and modify the main image
dst = cv2.add(img1_bg, img2_fg)
img1[y:y+rows, x:x+cols] = dst
return img1
评论列表
文章目录