其他分享
首页 > 其他分享> > 06. 图像基本运算和位运算

06. 图像基本运算和位运算

作者:互联网

图像基本运算和位运算

示例:在一个图片上绘制logo图标,主要技术点:使用mask掩码,对位运算的灵活运用

import cv2
import numpy as np

bg = cv2.imread('./images/bg.jpg')
# burn.png是一个4通道的图片,但是读取之后,变成了3通道,猜测跟flags有关
logo = cv2.imread('./images/burn.png')
logo_mask = cv2.imread('./images/burn.png', cv2.IMREAD_GRAYSCALE)
# 在数组中根据条件进行批量变更数据
logo_mask = np.where(logo_mask > 0, 255, 0)  # 这里的255是int32
logo_mask = logo_mask.astype('uint8')  # 需要将logo_mask.dtype从int32转为uint8
logo_width, logo_height = logo.shape[1], logo.shape[0]

# print(logo_mask[60:80, 60:80])
# print(cv2.bitwise_not(logo_mask)[60:80, 60:80])

x, y = 400, 100
bg_section = bg[y:y+logo_height, x:x+logo_width, :]
bg_section = cv2.bitwise_and(bg_section, bg_section, mask=cv2.bitwise_not(logo_mask))
bg_section = cv2.bitwise_or(bg_section, logo)
bg[y:y+logo_height, x:x+logo_width, :] = bg_section

# 绘制背景色
window_name = 'window'
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.resizeWindow(window_name, (800, 450))
cv2.imshow(window_name, bg)

# cv2.imshow('logo', logo_mask)
cv2.waitKey(0)
cv2.destroyAllWindows()

image image

标签:bg,06,运算,mask,section,cv2,bitwise,图像,logo
来源: https://www.cnblogs.com/TheoryDance/p/16407000.html