replicate=cv.copyMakeBorder(img,top_size,bottom_size,left_size,right_size,borderType=cv.BORDER_REPLICATE)
reflect=cv.copyMakeBorder(img,top_size,bottom_size,left_size,right_size,borderType=cv.BORDER_REFLECT)
reflect101=cv.copyMakeBorder(img,top_size,bottom_size,left_size,right_size,borderType=cv.BORDER_REFLECT_101)
wrap=cv.copyMakeBorder(img,top_size,bottom_size,left_size,right_size,borderType=cv.BORDER_WRAP)
constant=cv.copyMakeBorder(img,top_size,bottom_size,left_size,right_size,borderType=cv.BORDER_CONSTANT,value=0)
plt.subplot(2,3,1)
plt.imshow(img,'gray')
plt.title('ORIGINAL')
plt.show()
展示案例如下:
import cv2 as cv
img=cv.imread("../sources/cyq.jpg", cv.IMREAD_COLOR)
top_size,bottom_size,left_size,right_size=(50,50,50,50)
# 复制法,也就是复制最边缘的像素
replicate=cv.copyMakeBorder(img,top_size,bottom_size,left_size,right_size,borderType=cv.BORDER_REPLICATE)
# 反射法,54321|1234567|76543
reflect=cv.copyMakeBorder(img,top_size,bottom_size,left_size,right_size,borderType=cv.BORDER_REFLECT)
# 反射法,65432|1234567|65432 以1、7为轴
reflect101=cv.copyMakeBorder(img,top_size,bottom_size,left_size,right_size,borderType=cv.BORDER_REFLECT_101)
# 外包装法,34567|1234567|12345
wrap=cv.copyMakeBorder(img,top_size,bottom_size,left_size,right_size,borderType=cv.BORDER_WRAP)
# 常量法,常量值填充
constant=cv.copyMakeBorder(img,top_size,bottom_size,left_size,right_size,borderType=cv.BORDER_CONSTANT,value=0)
import matplotlib.pyplot as plt
plt.subplot(2,3,1),plt.imshow(img,'gray'),plt.title('ORIGINAL')
plt.subplot(2,3,2),plt.imshow(replicate,'gray'),plt.title('replicate')
plt.subplot(2,3,3),plt.imshow(reflect,'gray'),plt.title('reflect')
plt.subplot(2,3,4),plt.imshow(reflect101,'gray'),plt.title('reflect101')
plt.subplot(2,3,5),plt.imshow(wrap,'gray'),plt.title('wrap')
plt.subplot(2,3,6),plt.imshow(constant,'gray'),plt.title('constant')
plt.show()