文章忘保存丢了,气死我了,直接上应用层的了,
应用一:图片融合logo
效果图:
处理一 、读取图片
import cv2 as cv
img1 = cv.imread('./imgs/img1.png')
img2 = cv.imread('./imgs/logo.png')
规定图片操作的区域, 设定ROI
rows, cols, channels = img2.shape
roi = img1[0: rows, 0: cols]
将logo图片灰度化,制作阈值mask
img2gray = cv.cvtColor(img2, cv.COLOR_BGR2GRAY)
ret, mask = cv.threshold(img2gray, 10, 255, cv.THRESH_BINARY)
mask 为, 黑色区域为需要提取的部分,白色为过滤的部分
获取logo的区域, 进行非运算
inv_mask = cv.bitwise_not(mask)
logo 的mask 区域
获取背景和logo图像
logo的图像添加在图片上
img1_bg = cv.bitwise_and(roi,roi,mask=inv_mask)
获取logo 的区域
img2_fg= cv.bitwise_and(img2, img2,mask= mask)
加在一起
dst = cv.add(img1_bg,img2_fg)
img1[0:rows, 0:cols ] = dst
完成效果
完整代码
import cv2 as cv
img1 = cv.imread('./imgs/img1.png')
img2 = cv.imread('./imgs/logo.png')
rows, cols, channels = img2.shape
roi = img1[0: rows, 0:cols]
img2gray = cv.cvtColor(img2, cv.COLOR_BGR2GRAY)
ret, mask = cv.threshold(img2gray, 10, 255, cv.THRESH_BINARY)
inv_mask = cv.bitwise_not(mask)
inv_mask = cv.erode(inv_mask, cv.getStructuringElement(cv.MORPH_ELLIPSE, (3,3)))
img1_bg = cv.bitwise_and(roi,roi,mask=inv_mask)
img2_fg = cv.bitwise_and(img2,img2,mask = mask)
dst = cv.add(img1_bg,img2_fg)
img1[0:rows, 0:cols ] = dst
cv.imshow('res',img1)
cv.waitKey(0)
cv.destroyAllWindows()
|