如何填充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
h和w是输入图像的尺寸。
如何添加偏移量,以使ROI += image的结果与图像中所显示的一样?

发布于 2022-04-27 12:45:07
根据我的理解,您希望将调整大小的图像放在ROI的中心。
您已经获得了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我希望这能给你一个关于如何进一步推进的想法。
https://stackoverflow.com/questions/72026292
复制相似问题