def copyMakeBorder(src: Mat, top, bottom, left, right, borderType, dts: Mat = ..., value=...) -> typing.Any:
用于给图片填充边界,起到数据增广的左右,多应用与卷积。零填充等操作。
1 src: 输入的图像
2 top,bottom,left,right : 用于填充边界的范围大小
3 borderType: 填充类型
cv2.BORDER_CONSTANT
:添加的边界框像素值为常数(需要额外再给定一个参cv2.BORDER_REFLECT
:添加的边框像素将是边界元素的镜面反射,类似于gfedcba|abcdefgh|hgfedcbacv2.BORDER_REFLECT_101
or cv2.BORDER_DEFAULT
:和上面类似,但是有一些细微的不同,类似于gfedcb|abcdefgh|gfedcbacv2.BORDER_REPLICATE
:使用最边界的像素值代替,类似于aaaaaa|abcdefgh|hhhhhhhcv2.BORDER_WRAP
: cdefgh|abcdefgh|abcdefg4 value:如果borderType为cv2.BORDER_CONSTANT时需要填充的常数值。
from inspect import ismemberdescriptor
import cv2
import matplotlib.pyplot as plt
import numpy as np
img =cv2.imread("./lena.jpg",cv2.IMREAD_COLOR)
cv2.imshow("Lena",img)
print(img.shape)
top,bottom,left,right = (20,20,20,20)
original = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
replicate = cv2.copyMakeBorder(original,top,bottom,left,right,borderType=cv2.BORDER_REPLICATE)
reflect = cv2.copyMakeBorder(original,top,bottom,left,right,borderType=cv2.BORDER_REFLECT)
reflect101 = cv2.copyMakeBorder(original,top,bottom,left,right,borderType=cv2.BORDER_REFLECT101)
warp = cv2.copyMakeBorder(original,top,bottom,left,right,borderType=cv2.BORDER_WRAP)
constant = cv2.copyMakeBorder(original ,top,bottom,left,right,borderType=cv2.BORDER_CONSTANT,value=0)
plt.subplot(231),plt.imshow(original,"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("REFLECT101")
plt.subplot(235),plt.imshow(warp,"gray"),plt.title("WARP")
plt.subplot(236),plt.imshow(constant,"gray"),plt.title("CONSTANT")
plt.show()
cv2.waitKey(0)
cv2.destroyAllWindows()