opencv cv2.copyMakeBorder padding
Making Borders for Images (Padding) If you want to create a border around the image, something like a photo frame, you can use cv2.copyMakeBorder() function. But it has more applications for convolution operation, zero padding etc. This function takes following arguments:
src - input image top, bottom, left, right - border width in number of pixels in corresponding directions borderType - Flag defining what kind of border to be added. It can be following types: cv2.BORDER_CONSTANT - Adds a constant colored border. The value should be given as next argument. cv2.BORDER_REFLECT - Border will be mirror reflection of the border elements, like this : fedcba|abcdefgh|hgfedcb cv2.BORDER_REFLECT_101 or cv2.BORDER_DEFAULT - Same as above, but with a slight change, like this : gfedcb|abcdefgh|gfedcba cv2.BORDER_REPLICATE - Last element is replicated throughout, like this: aaaaaa|abcdefgh|hhhhhhh cv2.BORDER_WRAP - Can’t explain, it will look like this : cdefgh|abcdefgh|abcdefg value - Color of border if border type is cv2.BORDER_CONSTANT
src : 输入的图片 top, bottom, left, right :相应方向上的边框宽度 borderType:定义要添加边框的类型,它可以是以下的一种: cv2.BORDER_CONSTANT:添加的边界框像素值为常数(需要额外再给定一个参数) cv2.BORDER_REFLECT:添加的边框像素将是边界元素的镜面反射,类似于gfedcb|abcdefgh|gfedcba cv2.BORDER_REFLECT_101 or cv2.BORDER_DEFAULT:和上面类似,但是有一些细微的不同,类似于gfedcb|abcdefgh|gfedcba cv2.BORDER_REPLICATE:使用最边界的像素值代替,类似于aaaaaa|abcdefgh|hhhhhhh cv2.BORDER_WRAP:不知道怎么解释,直接看吧,cdefgh|abcdefgh|abcdefg value:如果borderType为cv2.BORDER_CONSTANT时需要填充的常数值。
img = cv2.imread('testimg.png')
img = cv2.resize(img,(256,256))
cv2.imshow('origin',img),cv2.waitKey(0),cv2.destroyAllWindows()
replicate = cv2.copyMakeBorder(img,20,20,20,20,cv2.BORDER_REPLICATE)
cv2.imshow('replicate',replicate),cv2.waitKey(0),cv2.destroyAllWindows()
constant = cv2.copyMakeBorder(img,20,20,20,20,cv2.BORDER_CONSTANT,value=(255,255,255))
cv2.imshow('constant',constant),cv2.waitKey(0),cv2.destroyAllWindows()
reflect = cv2.copyMakeBorder(img,20,20,20,20,cv2.BORDER_REFLECT)
cv2.imshow('reflect',reflect),cv2.waitKey(0),cv2.destroyAllWindows()
reflect101 = cv2.copyMakeBorder(img,20,20,20,20,cv2.BORDER_REFLECT_101)
cv2.imshow('reflect101',reflect101),cv2.waitKey(0),cv2.destroyAllWindows()
wrap = cv2.copyMakeBorder(img,20,20,20,20,cv2.BORDER_WRAP)
cv2.imshow('wrap',wrap),cv2.waitKey(0),cv2.destroyAllWindows()