当前位置: 首页 > news >正文

详解cv2.addWeighted函数【使用 OpenCV 添加(混合)两个图像-Python版本】

文章目录

  • 简介
  • 函数原型
  • 代码示例
  • 参考资料

简介

有的时候我们需要将两张图片在alpha通道进行混合,比如深度学习数据集增强方式MixUp。OpenCV的addWeighted提供了相关操作,此篇博客将详细介绍这个函数,并给出代码示例。🚀🚀

函数原型

o u t p u t I m g = s a t u r a t e ( α ∗ i n p u t I m g 1 + β ∗ i n p u t I m g 2 + γ ) \rm outputImg=saturate( \alpha*inputImg1+ \beta*inputImg2 + \gamma) outputImg=saturate(αinputImg1+βinputImg2+γ)

cv.addWeighted(	src1, alpha, src2, beta, gamma[, dst[, dtype]]	) -> dst
参数说明
src1图片1
alpha图片1的权重
src2图片2
beta图片2的权重
gamma添加到每个总和的标量。一般为0
dst输出图片,Python版本不需要指定👎
dtype输出数组的可选深度,默认即可

代码示例

在这里插入图片描述

import cv2
import matplotlib.pyplot as plt# 加载两张图片
img1 = cv2.imread(filename="Lenna.png")
img2 = cv2.imread(filename="horses.jpg")# 将两张图片都调整到640*640
shape1 = img1.shape  # HWC
shape2 = img2.shape  # HWC
max1 = max(shape1[0], shape1[1])
max2 = max(shape2[0], shape2[1])
img1 = cv2.copyMakeBorder(src=img1,top=int((max1 - shape1[0])/2),bottom=int((max1 - shape1[0])/2),left=int((max1 - shape1[1])/2),right=int((max1 - shape1[1])/2),borderType=cv2.BORDER_REFLECT101,
)
img1 = cv2.resize(src=img1, dsize=(640, 640), interpolation=cv2.INTER_LINEAR)
img2 = cv2.copyMakeBorder(src=img2,top=int((max2 - shape2[0])/2),bottom=int((max2 - shape2[0])/2),left=int((max2 - shape2[1])/2),right=int((max2 - shape2[1])/2),borderType=cv2.BORDER_REFLECT101,
)
img2 = cv2.resize(src=img2, dsize=(640, 640), interpolation=cv2.INTER_LINEAR)# 按照比例将两张图片进行混合
alpha = 0.5
beta = 1.0 - alpha
img_blending = cv2.addWeighted(src1=img1, alpha=alpha, src2=img2, beta=beta, gamma=.0)# 绘制图片
fig = plt.figure(figsize=(9, 3))
fig.suptitle(t="Blend two images")ax1 = fig.add_subplot(1, 3, 1)
ax2 = fig.add_subplot(1, 3, 2)
ax3 = fig.add_subplot(1, 3, 3)ax1.set_title(label="image1")
ax1.spines["top"].set_visible(b=False)
ax1.spines["bottom"].set_visible(b=False)
ax1.spines["left"].set_visible(b=False)
ax1.spines["right"].set_visible(b=False)
ax1.axes.xaxis.set_visible(b=False)
ax1.axes.yaxis.set_visible(b=False)
ax1.imshow(X=cv2.cvtColor(src=img1, code=cv2.COLOR_BGR2RGB))ax2.set_title(label="image2")
ax2.spines["top"].set_visible(b=False)
ax2.spines["bottom"].set_visible(b=False)
ax2.spines["left"].set_visible(b=False)
ax2.spines["right"].set_visible(b=False)
ax2.axes.xaxis.set_visible(b=False)
ax2.axes.yaxis.set_visible(b=False)
ax2.imshow(X=cv2.cvtColor(src=img2, code=cv2.COLOR_BGR2RGB))ax3.set_title(label="blending image")
ax3.spines["top"].set_visible(b=False)
ax3.spines["bottom"].set_visible(b=False)
ax3.spines["left"].set_visible(b=False)
ax3.spines["right"].set_visible(b=False)
ax3.axes.xaxis.set_visible(b=False)
ax3.axes.yaxis.set_visible(b=False)
ax3.imshow(X=cv2.cvtColor(src=img_blending, code=cv2.COLOR_BGR2RGB))plt.show()

参考资料

  1. Computer Vision: Algorithms and Applications
  2. OpenCV文档:Adding (blending) two images using OpenCV👍
  3. OpenCV文档:addWeighted() 🚀

收集整理和创作不易, 若有帮助🉑, 请帮忙点赞👍➕收藏❤️, 谢谢!✨✨🚀🚀

http://www.lryc.cn/news/197413.html

相关文章:

  • 单链表经典OJ题:反转链表
  • 软考高级信息系统项目管理师系列论文六:论信息系统项目的人力资源管理
  • Kubeadm部署k8s集群
  • YOLOv7改进:新机制,扩展DCNv3,基于DCNv2优化 | CVPR2023 InternImage
  • SMAP(Soil Moisture Active and Passive)数据下载
  • 【Huawei S5700交换机】产品介绍
  • 华为Atlas 200I DK A2开发者套件--基础使用配置
  • C++DAY47
  • 四边形不等式
  • Jmeter(四):请求默认值元件应用,正则表达式提取器元件讲解
  • LCR 001. 两数相除
  • LeCun和Bengio“吵”起来了,人工智能是“潘多拉魔盒”吗?
  • 电子期刊制作宝典,让你成为专业行家
  • ESP32网络开发实例-Web显示传感器实时数据
  • ARM Cortex-A9:裸机开发,点亮LED3
  • QT学习day2
  • 214. Devu和鲜花
  • 【C++初阶(三)引用与内联函数】
  • RK3288 Android11 mini-pcie接口 4G模组EC200A适配(含自适应功能)
  • Windows安装Jenkins
  • 计算属性,侦听属性,方法区别及例子
  • Windows工业三防平板全功能NFC近距离感应一维/二维扫描
  • git远端协同开发、解决冲突、分支合并、gitlab使用、远程仓库回滚、为开源项目贡献代码、git工作流,git pull和git fetch,变基
  • ims-go项目搭建
  • 2022最新版-李宏毅机器学习深度学习课程-P26 Recurrent Neural Network
  • 【Qt控件之QButtonGroup】概述及使用
  • 【开源分享】基于Html开发的房贷计算器,模仿新浪财经
  • ftp文件上传缓慢问题
  • 【周末闲谈】VR新视界,“眼”见未来
  • CSRF和XSS是什么?