Boundary filling of python-opencv

function prototype

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

function function

It is used to fill the boundary of the picture, to play the role of data augmentation, multi-application and convolution. operations such as zero padding.

parameter

insert image description here

1 src: the input image
2 top,bottom,left,right : the range size used to fill the border
3 borderType: the fill type

  • cv2.BORDER_CONSTANT: The pixel value of the added bounding box is constant (an additional parameter needs to be given
  • cv2.BORDER_REFLECT: The border pixels added will be the specular reflection of the border element, similar to gfedcba|abcdefgh|hgfedcba
  • cv2.BORDER_REFLECT_101or cv2.BORDER_DEFAULT: Similar to the above, but there are some subtle differences, similar to gfedcb|abcdefgh|gfedcba
  • cv2.BORDER_REPLICATE: Use the most boundary pixel value instead, similar to aaaaaa|abcdefgh|hhhhhhh
  • cv2.BORDER_WRAP: cdefgh|abcdefgh|abcdefg

4 value: If the borderType is cv2.BORDER_CONSTANT, the constant value that needs to be filled.

example

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

The results show

insert image description here

Guess you like

Origin blog.csdn.net/qq_38505858/article/details/126779146