python-opencv之边界填充

函数原型

def copyMakeBorder(src: Mat, top, bottom, left, right, borderType, dts: Mat = ..., value=...) -> typing.Any:

函数功能

用于给图片填充边界,起到数据增广的左右,多应用与卷积。零填充等操作。

参数

python-opencv之边界填充_第1张图片

1 src: 输入的图像
2 top,bottom,left,right : 用于填充边界的范围大小
3 borderType: 填充类型

  • cv2.BORDER_CONSTANT:添加的边界框像素值为常数(需要额外再给定一个参
  • cv2.BORDER_REFLECT:添加的边框像素将是边界元素的镜面反射,类似于gfedcba|abcdefgh|hgfedcba
  • cv2.BORDER_REFLECT_101 or cv2.BORDER_DEFAULT:和上面类似,但是有一些细微的不同,类似于gfedcb|abcdefgh|gfedcba
  • cv2.BORDER_REPLICATE:使用最边界的像素值代替,类似于aaaaaa|abcdefgh|hhhhhhh
  • cv2.BORDER_WRAP: cdefgh|abcdefgh|abcdefg

4 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()

结果显示

python-opencv之边界填充_第2张图片

你可能感兴趣的:(OpenCV,Python,opencv,python,计算机视觉)