如何在opencv python中为图像添加边框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36255654/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 17:38:40  来源:igfitidea点击:

how to add border around an image in opencv python

pythonopencvcomputer-vision

提问by Anthony

If I have an image like below, how can I add border all around the image such that the overall height and width of the final image increases but the height and width of the original image stays as-is in the middle.

如果我有一个像下面这样的图像,我如何在图像周围添加边框,使最终图像的整体高度和宽度增加,但原始图像的高度和宽度保持在中间。

enter image description here

在此处输入图片说明

采纳答案by tfv

The following code adds a constant border of size 10 pixels to all four sides of your original image.

以下代码向原始图像的所有四个边添加大小为 10 像素的恒定边框。

For the colour, I have assumed that you want to use the average gray value of the background, which I have calculated from the mean value of bottom two lines of your image. Sorry, somewhat hard coded, but shows the general how-to and can be adapted to your needs.

对于颜色,我假设您要使用背景的平均灰度值,这是我根据图像底部两行的平均值计算的。抱歉,有点硬编码,但显示了一般操作方法,可以根据您的需要进行调整。

If you leave bordersize values for bottom and right at 0, you even get a symmetric border.

如果将底部和右侧的 bordersize 值保留为 0,您甚至会得到一个对称边框。

Other values for BORDER_TYPE are possible, such as BORDER_DEFAULT, BORDER_REPLICATE, BORDER_WRAP.

BORDER_TYPE 的其他值也是可能的,例如 BORDER_DEFAULT、BORDER_REPLICATE、BORDER_WRAP。

For more details cf: http://docs.opencv.org/trunk/d3/df2/tutorial_py_basic_ops.html#gsc.tab=0

有关更多详细信息,请参阅:http: //docs.opencv.org/trunk/d3/df2/tutorial_py_basic_ops.html#gsc.tab=0

import numpy as np
import cv2

im = cv2.imread('image.jpg')
row, col = im.shape[:2]
bottom = im[row-2:row, 0:col]
mean = cv2.mean(bottom)[0]

bordersize = 10
border = cv2.copyMakeBorder(
    im,
    top=bordersize,
    bottom=bordersize,
    left=bordersize,
    right=bordersize,
    borderType=cv2.BORDER_CONSTANT,
    value=[mean, mean, mean]
)

cv2.imshow('image', im)
cv2.imshow('bottom', bottom)
cv2.imshow('border', border)
cv2.waitKey(0)
cv2.destroyAllWindows()

回答by Saransh Kejriwal

Try This:

尝试这个:

import cv2
import numpy as np     

img=cv2.imread("img_src.jpg")
h,w=img.shape[0:2]

base_size=h+20,w+20,3
# make a 3 channel image for base which is slightly larger than target img
base=np.zeros(base_size,dtype=np.uint8)
cv2.rectangle(base,(0,0),(w+20,h+20),(255,255,255),30) # really thick white rectangle
base[10:h+10,10:w+10]=img # this works

回答by Ishara Madhawa

Answer in one line

一行回答

outputImage = cv2.copyMakeBorder(
                 inputImage, 
                 topBorderWidth, 
                 bottomBorderWidth, 
                 leftBorderWidth, 
                 rightBorderWidth, 
                 cv2.BORDER_CONSTANT, 
                 value=color of border
              )