import cv2
import matplotlib.pyplot as pltimg2 = cv2.imread("E:\Jupyter_workspace\study\data/cat.png")#读取照片,第二个参数若为0,则灰度图;若不填或者1则彩色图或本身图top_size,bottom_size,left_size,right_size = (50,50,50,50)
replicate = cv2.copyMakeBorder(img2,top_size,bottom_size,left_size,right_size,borderType=cv2.BORDER_REPLICATE)
reflect = cv2.copyMakeBorder(img2,top_size,bottom_size,left_size,right_size,borderType=cv2.BORDER_REFLECT)
reflect101 = cv2.copyMakeBorder(img2,top_size,bottom_size,left_size,right_size,borderType=cv2.BORDER_REFLECT_101)
wrap = cv2.copyMakeBorder(img2,top_size,bottom_size,left_size,right_size,borderType=cv2.BORDER_WRAP)
constant = cv2.copyMakeBorder(img2,top_size,bottom_size,left_size,right_size,borderType=cv2.BORDER_CONSTANT,value = 0)plt.subplot(231),plt.imshow(img2,'gray'),plt.title('ORIGINAL')
plt.subplot(232),plt.imshow(replicate,'gray'),plt.title('REPLICATE')
plt.subplot(233),plt.imshow(reflect,'gray'),plt.title('REFLECT')
plt.subplot(234),plt.imshow(reflect101,'gray'),plt.title('REFLECT_101')
plt.subplot(235),plt.imshow(wrap,'gray'),plt.title('WRAP')
plt.subplot(236),plt.imshow(constant,'gray'),plt.title('CONSTANT')plt.show()
参数 | 功能效果 |
---|---|
BORDER_ REPLICATE:复制法,也就是复制最边缘像素 | aaaaa labcd ldddd |
BORDER_ _REFLECT: 反射法,对感兴趣的图像中的像素在两边进行复制 | fedcba labcdefgh lhgfedcb |
BORDER_ REFLECT_ 101: 反射法,也就是以最边缘像索为轴,对称, | gfedcb labedefgh lgfedcba |
BORDER_ WRAP:外包装法 | cdefgh labcdefgh labedefg |
BORDER_ _CONSTANT:常量法 | 常数值填充代表不同的颜色,例如:0为黑色 |
效果如下: