Opencv Python 简明教程
OpenCV Python - Image Addition
读取图像的图像对象本质上是一个二维或三维矩阵,具体取决于图像是否为灰度图像或 RGB 图像。
When an image is read by imread() function, the resultant image object is really a two or three dimensional matrix depending upon if the image is grayscale or RGB image.
因此, cv2.add() 函数将两个图像矩阵相加,并返回另一个图像矩阵。
Hence, cv2.add() functions add two image matrices and returns another image matrix.
Example
以下代码读取两张图像并执行其二进制加法:
Following code reads two images and performs their binary addition −
kalam = cv2.imread('kalam.jpg')
einst = cv2.imread('einstein.jpg')
img = cv2.add(kalam, einst)
cv2.imshow('addition', img)
Result
OpenCV 有一个 addWeighted() 函数来执行两个数组的加权和,而不是线性二进制加法。对应的命令如下:
Instead of a linear binary addition, OpenCV has a addWeighted() function that performs weighted sum of two arrays. The command for the same is as follows
Cv2.addWeighted(src1, alpha, src2, beta, gamma)
Parameters
addWeighted() 函数的参数如下:
The parameters of the addWeighted() function are as follows −
-
src1 − First input array.
-
alpha − Weight of the first array elements.
-
src2 − Second input array of the same size and channel number as first
-
beta − Weight of the second array elements.
-
gamma − Scalar added to each sum.
此函数根据以下方程式将图像相加:
This function adds the images as per following equation −
\mathrm{g(x)=(1-\alpha)f_{0}(x)+\alpha f_{1}(x)}
在上述示例中获得的图像矩阵用于执行加权和。
The image matrices obtained in the above example are used to perform weighted sum.
通过将 a 从 0 更改到 1,可以平滑地从一张图像过渡到另一张图像,以便它们融合在一起。
By varying a from 0 → 1, a smooth transition takes place from one image to another, so that they blend together.
第一张图像的权重为 0.3,第二张图像的权重为 0.7。将余弦因子设置为 0.
First image is given a weight of 0.3 and the second image is given 0.7. The gamma factor is taken as 0.
addWeighted() 函数的命令如下:
The command for addWeighted() function is as follows −
img = cv2.addWeighted(kalam, 0.3, einst, 0.7, 0)
可以看出,与二进制加法相比,图像加法更加平滑。
It can be seen that the image addition is smoother compared to binary addition.