编程语言
首页 > 编程语言> > Python 6.给图片添加边框(重复,复制,纯色等) OpenCV

Python 6.给图片添加边框(重复,复制,纯色等) OpenCV

作者:互联网

import cv2
import numpy as np
from matplotlib import pyplot as plt

BLUE = [255, 0, 0]

img1 = cv2.imread('pic1.png', 1)

# change the value of bgr
b, g, r = cv2.split(img1)
img2 = cv2.merge([r, g, b])
img3 = cv2.merge([r, g, b])
img4 = cv2.merge([r, g, b])
img5 = cv2.merge([r, g, b])

# make one of bgr become 0
img3[:, :, 0] = 0
img4[:, :, 1] = 0
img5[:, :, 2] = 0

# make all kind of border
replicate = cv2.copyMakeBorder(img1, 10, 10, 10, 10, cv2.BORDER_REPLICATE)
reflect = cv2.copyMakeBorder(img1, 10, 10, 10, 10, cv2.BORDER_REFLECT)
reflect101 = cv2.copyMakeBorder(img1, 10, 10, 10, 10, cv2.BORDER_REFLECT_101)
wrap = cv2.copyMakeBorder(img1, 10, 10, 10, 10, cv2.BORDER_WRAP)
constant = cv2.copyMakeBorder(img1, 10, 10, 10, 10, cv2.BORDER_CONSTANT, value=BLUE)

# plot figure
plt.subplot(331), plt.imshow(img2), plt.title('origin')
plt.subplot(332), plt.imshow(replicate, 'gray'), plt.title('replicate')
plt.subplot(333), plt.imshow(reflect, 'gray'), plt.title('reflect')
plt.subplot(334), plt.imshow(reflect101, 'gray'), plt.title('reflect101')
plt.subplot(335), plt.imshow(wrap, 'gray'), plt.title('wrap')
plt.subplot(336), plt.imshow(constant, 'gray'), plt.title('constant')
plt.subplot(337), plt.imshow(img3), plt.title('img3')
plt.subplot(338), plt.imshow(img4), plt.title('img4')
plt.subplot(339), plt.imshow(img5), plt.title('img5')

plt.show()
 

qq_36071362 发布了28 篇原创文章 · 获赞 39 · 访问量 6744 私信 关注

标签:subplot,10,plt,title,Python,imshow,cv2,OpenCV,纯色
来源: https://blog.csdn.net/qq_36071362/article/details/104097501