58

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

stateMachine
  • 5,227
  • 4
  • 13
  • 29
Anthony
  • 33,838
  • 42
  • 169
  • 278
  • 6
    How about [cv2.copyMakeBorder](http://docs.opencv.org/2.4/modules/imgproc/doc/filtering.html#void%20copyMakeBorder%28InputArray%20src,%20OutputArray%20dst,%20int%20top,%20int%20bottom,%20int%20left,%20int%20right,%20int%20borderType,%20const%20Scalar&%20value%29) ? – Dan Mašek Mar 28 '16 at 03:49
  • 1
    By the way, opencv official documentation provides a [tutorial](https://docs.opencv.org/master/dc/da3/tutorial_copyMakeBorder.html) how to append a border to am image using cv2.copyMakeBorder. – Evgeny Bobkin Jun 05 '19 at 09:58

4 Answers4

53

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

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 border_size values for bottom and right at 0, you even get a symmetric border.

Other values for BORDER_TYPE are possible, such as 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

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]

border_size = 10
border = cv2.copyMakeBorder(
    im,
    top=border_size,
    bottom=border_size,
    left=border_size,
    right=border_size,
    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()
tfv
  • 6,016
  • 4
  • 36
  • 67
  • 2
    Using the tutorial I thought that the value for Red (confusingly labelled Blue) should be [255, 0, 0]. But actually it was [1.0, 0, 0] with floats as the image value colours. – hum3 May 25 '17 at 11:56
20

Answer in one line

outputImage = cv2.copyMakeBorder(
                 inputImage, 
                 topBorderWidth, 
                 bottomBorderWidth, 
                 leftBorderWidth, 
                 rightBorderWidth, 
                 cv2.BORDER_CONSTANT, 
                 value=color of border
              )
JustBaron
  • 2,319
  • 7
  • 25
  • 37
Ishara Madhawa
  • 3,549
  • 5
  • 24
  • 42
16

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
Evgeny Bobkin
  • 4,092
  • 2
  • 17
  • 21
Saransh Kejriwal
  • 2,467
  • 5
  • 15
  • 39
8

Add border using openCV

import cv2
white = [255,255,255]
img1 = cv2.imread('input.png')
constant= cv2.copyMakeBorder(img1,20,20,20,20,cv2.BORDER_CONSTANT,value=white)
cv2.imwrite('output.png',constant)
Eliyaz KL
  • 654
  • 1
  • 7
  • 13