【opencv 学习笔记 07 addweighted() 函数】

本文介绍如何使用OpenCV的addWeighted()函数实现图像的加权和操作,包括图像融合、黑白图像生成及调整亮度。展示了如何通过不同权重和偏移值,将两幅图像进行混合,形成新的视觉效果。

addweighted()

import cv2
import numpy as np

img = cv2.imread('img.jpg')
cv2.namedWindow('Image', cv2.WINDOW_AUTOSIZE)
cv2.imshow('Image', img)
# 生成一个黑色的图像 大小和类型与源图像一样
img1 = np.zeros(img.shape, dtype=img.dtype)
cv2.imshow('Image1', img1)
# 数组后面直接加上255 则变为全白图像
img_white = img1 + 255
cv2.imshow('Image_white', img_white)
# 这个函数就相当于
src = cv2.addWeighted(img, 0.2, img1, 0.8, 0)
cv2.imshow('src', src)
src2 = cv2.addWeighted(img, 0.2, img_white, 0.8, 0)
cv2.imshow('src2', src2)
# 第五个参数是,按照权重加和之后 再给图片添加的数值,如果超过255 则全白
src3 = cv2.addWeighted(img, 0.2, img1, 0.8, 100)
cv2.imshow('src3', src3)
cv2.waitKey(0)
cv2.destroyAllWindows()

函数原型

def addWeighted(src1, alpha, src2, beta, gamma, dst=None, dtype=None): # real signature unknown; restored from __doc__
    """
    addWeighted(src1, alpha, src2, beta, gamma[, dst[, dtype]]) -> dst
    .   @brief Calculates the weighted sum of two arrays.
    .   
    .   The function addWeighted calculates the weighted sum of two arrays as follows:
    .   \f[\texttt{dst} (I)= \texttt{saturate} ( \texttt{src1} (I)* \texttt{alpha} +  \texttt{src2} (I)* \texttt{beta} +  \texttt{gamma} )\f]
    .   where I is a multi-dimensional index of array elements. In case of multi-channel arrays, each
    .   channel is processed independently.
    .   The function can be replaced with a matrix expression:
    .   @code{.cpp}
    .       dst = src1*alpha + src2*beta + gamma;
    .   @endcode
    .   @note Saturation is not applied when the output array has the depth CV_32S. You may even get
    .   result of an incorrect sign in the case of overflow.
    .   @param src1 first input array.
    .   @param alpha weight of the first array elements.
    .   @param src2 second input array of the same size and channel number as src1.
    .   @param beta weight of the second array elements.
    .   @param gamma scalar added to each sum.
    .   @param dst output array that has the same size and number of channels as the input arrays.
    .   @param dtype optional depth of the output array; when both input arrays have the same depth, dtype
    .   can be set to -1, which will be equivalent to src1.depth().
    .   @sa  add, subtract, scaleAdd, Mat::convertTo
    """

结果展示

在这里插入图片描述

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值