填充 ROI 的中心

Fill the center of ROI

如何填充ROI,使图片位于ROI的中心?

ROI 是用 (x, y, w, h) = cv2.boundingRect(contour) 找到的,我用

插入 输入图像
                       image = cv2.resize(input_image, (w, h))
                       img2gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
                       _, image_mask = cv2.threshold(img2gray, 1, 255, cv2.THRESH_BINARY)
                       roi = frame[y:y+h, x:x+w]
                       roi[np.where(image_mask)] = 0
                       roi += image

hw 是我的输入图像的尺寸。

如何添加偏移量以使 ROI += image 的结果如图所示?

据我了解,您想将调整后的图像放在 ROI 的中心。

您已经获得投资回报率roi = frame[y:y+h, x:x+w]

# using this as background, hence making it white
roi[:] = (255, 255, 255)

# Obtain the spatial dimensions of ROI and image
roi_h, roi_w = roi.shape[:2]
image_h, image_w = image.shape[:2]

# Calculate height and width offsets
height_off = int((roi_h - image_h)/2)
width_off = int((roi_w - image_w)/2)

# With Numpy slicing, place the image in the center of the ROI
roi[height_off:height_off+image_h, width_off:width_off+image_w] = image

我希望这能让您了解如何进一步进行