3. OpenCV-boundary filling

border padding

语法:cv2.copyMakeBorder(img,top,bootom,left,right,borderType)

borderType filling method:
cv2.BORDER_REPLICATE: copy method, copy the most edge pixel
cv2.BORDER_REFLECT: reflection method. Copy the pixels in the image of interest on both sides, such as: fedcba|abcdetgh|hgfedcb
cv2.BORDER_REFLECT101: reflection method, which is symmetrical with the most edge pixel as the axis, gfedcb|abcdefgh|gfedcba
cv2.BORDER_WRAP: outer packaging method cdefgh|abcdefgh|abcdefg
cv2.BORDER_CONSTANT: Constant method, fill with constant value

sample code

import cv2  
import matplotlib.pyplot as plt

img = cv2.imread("C:\\Users\\zhangqs\\Desktop\\demo.png", cv2.IMREAD_COLOR)

# 边界填充
#cv2.BORDER_REPLICATE:复制法,复制最边缘像素
#cv2.BORDER_REFLECT:反射法。对兴趣的图像中的像素在两边进行复制,如:fedcba|abcdetgh|hgfedcb
#cv2.BORDER_REFLECT101:反射法,就是以最边缘像素为轴,对称,gfedcb|abcdefgh|gfedcba
#cv2.BORDER_WRAP:外包装法cdefgh|abcdefgh|abcdefg
#cv2.BORDER_CONSTANT:常量法,常数值填充
top,bootom,left,right=(50,50,50,50) #顺序 上、下、左、右
replicate=cv2.copyMakeBorder(img,top,bootom,left,right,borderType=cv2.BORDER_REPLICATE)
reflect=cv2.copyMakeBorder(img,top,bootom,left,right,borderType=cv2.BORDER_REFLECT)
reflect101=cv2.copyMakeBorder(img,top,bootom,left,right,borderType=cv2.BORDER_REFLECT101)
warp=cv2.copyMakeBorder(img,top,bootom,left,right,borderType=cv2.BORDER_WRAP)
constant=cv2.copyMakeBorder(img,top,bootom,left,right,borderType=cv2.BORDER_CONSTANT,value=0)

#拼合显示
plt.subplot(231),plt.imshow(img,'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()

running result

Guess you like

Origin blog.csdn.net/a497785609/article/details/131205770