当前位置:   article > 正文

通过掩膜叠加图片

掩膜叠加

python opencv 图像叠加,python opencv图像叠加/图像融合/mask掩模_weixin_39862794的博客-CSDN博客

  1. import cv2
  2. img1 = cv2.imread('pic.png')
  3. img2 = cv2.imread('small.jpg')
  4. img2 = cv2.resize(img2,(100,100))
  5. #首先获取原始图像roi
  6. rows,cols,channels = img2.shape
  7. roi = img1[0:rows, 0:cols ]
  8. #原始图像转化为灰度值
  9. # Now create a mask of logo and create its inverse mask also
  10. img2gray = cv2.cvtColor(img2,cv2.COLOR_BGR2GRAY)
  11. cv2.imshow('img2gray',img2gray)
  12. cv2.waitKey(0)
  13. '''
  14. 将一个灰色的图片,变成要么是白色要么就是黑色。(大于规定thresh值就是设置的最大值(常为255,也就是白色))
  15. '''
  16. #将灰度值二值化,获得ROI区域掩模
  17. ret, mask = cv2.threshold(img2gray, 200, 255, cv2.THRESH_BINARY)
  18. cv2.imshow('mask',mask)
  19. cv2.waitKey(0)
  20. #ROI掩模区域反向掩模
  21. mask_inv = cv2.bitwise_not(mask)
  22. cv2.imshow('mask_inv',mask_inv)
  23. cv2.waitKey(0)
  24. #掩模显示背景
  25. # Now black-out the area of logo in ROI
  26. img1_bg = cv2.bitwise_and(roi,roi,mask = mask)
  27. cv2.imshow('img1_bg',img1_bg)
  28. cv2.waitKey(0)
  29. #掩模显示前景
  30. # Take only region of logo from logo image.
  31. img2_fg = cv2.bitwise_and(img2,img2,mask = mask_inv)
  32. cv2.imshow('img2_fg',img2_fg)
  33. cv2.waitKey(0)
  34. #前背景图像叠加
  35. # Put logo in ROI and modify the main image
  36. dst = cv2.add(img1_bg,img2_fg)
  37. img1[0:rows, 0:cols ] = dst
  38. cv2.imshow('res',img1)
  39. cv2.waitKey(0)
  40. cv2.destroyAllWindows()
  41. img1[0:rows, 0:cols ] = dst
  42. cv2.imshow('res',img1)
  43. cv2.waitKey(0)
  44. cv2.destroyAllWindows()

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/酷酷是懒虫/article/detail/929299
推荐阅读