python - Opencv,如何覆盖图像?

标签 python image opencv computer-vision

我有一组任意图像。图像的一半是图片,一半是定义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

lena with boarder

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/

相关文章:

python - Python函数定义中的正斜杠? [复制]

python - 在 Python Tkinter 中创建模态对话框是否需要 wait_window()?

python - 从轮廓删除背景

python - cv2.namedWindow和imshow无法正常工作

opencv - 使用Cuda进行OpenCv编译

python - 为什么 vim 在运行 Python 代码时不在控制台中打印任何内容?

python - OrderedDict 不保留顺序

html - 悬停时图像宽度为 'malfunctioning'(webkit),如何解决?

.net - 至少有潜在危险的错误,你会怎么做?

ruby-on-rails - 使用回形针保存从 api 获得的 base64 图像