How to copy masked part from one image into another image using Opencv in python?

49 views Asked by At

I want to crop several polygons by tuple points, given in points_list. I came up with this code:

img = cv2.imread('019.jpg')
bg_img = cv2.imread('yellow.jpg')

points_list = [
    [(571, 1792), (563, 1845), (595, 1867), (664, 1864), (695, 1827), (671, 1795)],
    [(1242, 1408), (1241, 1462), (1252, 1472), (1265, 1459), (1263, 1408)]
]

for points in points_list:
    mask = np.zeros(img.shape[0:2], dtype=np.uint8)
    polygon_points =  np.array([points], dtype=np.int32)
    cv2.fillPoly(mask, polygon_points, (255, 255, 255))

    cropped_region = cv2.bitwise_and(img, bg_img, mask=mask)
   
cv2.imwrite('merge.jpg', cropped_region)

from an old image (019.jpg) to a new image (yellow.jpg).

However, it only shows polygons part in a dark image, how to merge it with yellow.jpg

enter image description here

enter image description here enter image description here

0

There are 0 answers