我有一组任意图像。图像的一半是图片,一半是定义ROIS的蒙版。
在程序的当前版本中,我使用ROI裁剪图像(即我在图像中提取与ROI蒙版的边框匹配的矩形)。问题是,ROI蒙版并不是完美的,在我的案例中,过度预测比预测不足要好。
因此,我想复制的不仅仅是ROI矩形,但是如果执行此操作,则可能要裁切出图像。
即:
x, y, w, h = cv2.boundingRect(mask_contour)
img = img[int(y-h*0.05):int(y + h * 1.05), int(x-w*0.05):int(x + w * 1.05)]
可能会失败,因为它尝试访问超出范围的像素。我可以固定值,但是我想知道是否有更好的方法
最佳答案
您可以添加boarder using OpenCV
import cv2 as cv
import random
src = cv.imread('/home/stephen/lenna.png')
borderType = cv.BORDER_REPLICATE
boarderSize = .5
top = int(boarderSize * src.shape[0]) # shape[0] = rows
bottom = top
left = int(boarderSize * src.shape[1]) # shape[1] = cols
right = left
value = [random.randint(0,255), random.randint(0,255), random.randint(0,255)]
dst = cv.copyMakeBorder(src, top, bottom, left, right, borderType, None, value)
cv.imshow('img', dst)
c = cv.waitKey(0)
关于python - Opencv,如何覆盖图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55733086/