通过掩膜叠加图片
【摘要】
python opencv 图像叠加,python opencv图像叠加/图像融合/mask掩模_weixin_39862794的博客-CSDN博客
import cv2 img1 = cv2.imread('pic.png') img2 = cv2.imread('small.jpg') img2 = cv2.resize(i...
python opencv 图像叠加,python opencv图像叠加/图像融合/mask掩模_weixin_39862794的博客-CSDN博客
-
import cv2
-
-
-
img1 = cv2.imread('pic.png')
-
-
img2 = cv2.imread('small.jpg')
-
-
img2 = cv2.resize(img2,(100,100))
-
-
-
#首先获取原始图像roi
-
-
rows,cols,channels = img2.shape
-
-
roi = img1[0:rows, 0:cols ]
-
-
#原始图像转化为灰度值
-
-
# Now create a mask of logo and create its inverse mask also
-
-
img2gray = cv2.cvtColor(img2,cv2.COLOR_BGR2GRAY)
-
-
cv2.imshow('img2gray',img2gray)
-
-
cv2.waitKey(0)
-
-
'''
-
-
将一个灰色的图片,变成要么是白色要么就是黑色。(大于规定thresh值就是设置的最大值(常为255,也就是白色))
-
-
'''
-
-
#将灰度值二值化,获得ROI区域掩模
-
-
ret, mask = cv2.threshold(img2gray, 200, 255, cv2.THRESH_BINARY)
-
-
cv2.imshow('mask',mask)
-
-
cv2.waitKey(0)
-
-
#ROI掩模区域反向掩模
-
-
mask_inv = cv2.bitwise_not(mask)
-
-
cv2.imshow('mask_inv',mask_inv)
-
-
cv2.waitKey(0)
-
-
#掩模显示背景
-
-
# Now black-out the area of logo in ROI
-
-
img1_bg = cv2.bitwise_and(roi,roi,mask = mask)
-
-
cv2.imshow('img1_bg',img1_bg)
-
-
cv2.waitKey(0)
-
-
#掩模显示前景
-
-
# Take only region of logo from logo image.
-
-
img2_fg = cv2.bitwise_and(img2,img2,mask = mask_inv)
-
-
cv2.imshow('img2_fg',img2_fg)
-
-
cv2.waitKey(0)
-
-
#前背景图像叠加
-
-
# Put logo in ROI and modify the main image
-
-
dst = cv2.add(img1_bg,img2_fg)
-
-
img1[0:rows, 0:cols ] = dst
-
-
cv2.imshow('res',img1)
-
-
cv2.waitKey(0)
-
-
cv2.destroyAllWindows()
-
-
img1[0:rows, 0:cols ] = dst
-
-
cv2.imshow('res',img1)
-
-
cv2.waitKey(0)
-
-
cv2.destroyAllWindows()
文章来源: blog.csdn.net,作者:AI视觉网奇,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/jacke121/article/details/124464506
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)