重磅干货,第一时间送达
使用OpenCV读取一张图像
import cv2image = cv2.imread(‘image.jpg’)
当读取图像之后,如果有必要的话可以将其从BGR格式转换为RGB格式,通过使用cv2.cvtColor()命令实现。
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
覆盖
image_1
image_1 = cv2.imread(‘image_1.jpg’)
print(image_1)
array([[[107, 108, 106],[107, 108, 106],[107, 108, 106],…,[ 77, 78, 76],[ 77, 78, 76],[ 76, 77, 75]],…,[[ 93, 88, 87],[ 93, 88, 87],[ 92, 87, 86],…,[ 52, 62, 62],[ 52, 62, 62],[ 52, 62, 62]]], dtype=uint8)
如果只改变图像某一区域的像素值,比如更改为[0,0,0],这部分区域将变成黑色,因为这是颜色为黑色的像素值。同样,如果将像素值更改为[255,0,0],则该区域将变为蓝色(OpenCV以BGR格式读取图像)。
image_1[50: 100, 50:100] = [255, 0, 0]
image_2 = cv2.imread(‘image_2.jpg’)
resized_image_2 = cv2.resize(image_2, dsize=(100, 100))
image_1[50:150, 50:150] = resized_image_2
覆盖PNG图像
image_3 = cv2.imread(‘image_3.png’, cv2.IMREAD_UNCHANGED)
print(image_3)
array([[[0 0 0 0][0 0 0 0][0 0 0 0]…[0 0 0 0][0 0 0 0][0 0 0 0]]…[[0 0 0 0][0 0 0 0][0 0 0 0]…[0 0 0 0][0 0 0 0][0 0 0 0]]], dtype=uint8)
import numpy as npones = np.ones((image_1.shape[0], image_1.shape[1]))*255
image_1 = np.dstack([image_1, ones])
image_1[150:250, 150:250] = image_3
alpha_image_3 = image_3[:, :, 3] / 255.0
alpha_image = 1 — alpha_image_3
for c in range(0, 3): image_1[150:250, 150:250, c] = ((alpha_image*image_1[150:250, 150:250, c]) + (alpha_image_3*image_3[:, :, c]))
联系客服