裁剪图像并粘贴到与源相同的坐标

crop image and paste in same coords as from source

我能够检测到人并找到人的坐标,然后我可以裁剪图像。但我想保留这个人并删除所有背景/将其设为白色背景/复制裁剪后的图像并将其粘贴到另一个文件中,坐标与从源中获得的坐标相同。

这是我需要的图片:

  1. 使用输入图像的高度和宽度制作白色背景图像。
  2. 根据坐标将裁剪后的图片放入白底图片

import cv2
import numpy as np

image = cv2.imread('img.png')
person = image[110:532, 250:516] # persons coordinates

h, w, c = image.shape

white_background = np.zeros([h, w, 3])
for y in range(h):
    for x in range(w):
        white_background[y,x] = [255,255,255] # fill with white pixels

white_background[110:532, 250:516] = person  # paste the image into background image

cv2.imwrite('resized_centered.png', white_background)

结果: