Updated April 7, 2023
Introduction to OpenCV addWeighted
The following article provides an outline for OpenCV addWeighted. OpenCV is a technique used in Python to process images and then use it further for analytical purposes. It can also be used to solve problems in Computer Vision. Of all the functions this library provides, addWeighted is a function that helps in alpha blending of the image. Alpha blending helps in overlaying a foreground image with a transparent look while it has a background image over this one. The transparency is usually the fourth channel of any image. This transparency mask is known as an alpha mask. The foreground image is on the top left, while the gray scale is the alpha mask which is on the top right.
Syntax:
The addweighted function helps in this transition of the image to another. In order to blend this image, we can add weights are define the transparency and translucency of the images.
The generic syntax for this can be as below:
img = cv2.addWeighted(source1, alpha, source2, beta, gamma[, dst[, dtype]])
Here we add the image and then add the pixel values. The new image is the source where we will multiply the alpha value and the second source with the beta value. The gamma value will be added to this value and help in processing and alpha blending the image.
How does addWeighted Function Work?
OpenCV library in Python helps us in adding two images. These images can be added and used together for further analysis. A way of adding images is through blending them so that they both can be visible together. We can give different weights to different images, which can make the image transparent or translucent according to the weight added.
The images can be added by using the below formula:
By changing the alpha value, which will range from 0 to 1, we can easily transform from one image to another. The value of these weights ranges from 0 to 1, and then we can have the desired view of images as per our need. The transition will be smooth and will be blended very well.
Here the images that we take into consideration say image one is given a weight of 0.4 and the second image has the weight of 0.6 the cv.addWeighted image will have us the following equation formed.
Here we will be considering the values of y as 0. This formula helps in gradating the image and then blending them together. The higher the value, the more will that image be visible. Which means it will not be transparent.
Example of OpenCV addWeighted
Given below is the example mentioned:
Firstly we will add the images by using the beta values of 0.5. This will not have many changes in the output of the image.
Code:
import cv2
# Reading the images that are present
source1 = cv2.imread('eduCBA.JPG', cv2.IMREAD_COLOR)
source2 = cv2.imread('eduCBA.JPG', cv2.IMREAD_COLOR)
# blending the image by adding necessary weights
dest = cv2.addWeighted(source1, 0.5, source2, 0.5, 0.0)
# saving the output of the image
cv2.imwrite('img.png', dest)
cv2.imshow('img.png', dest)
# Wait for a key
cv2.waitKey(0)
# Distroy all the window open
cv2.destroyAllWindows()
The above code helps us in understanding how to blend the images. It lets us use the alpha and beta values which are used for blending the images. In the above code, we first import the cv2 library, which is used for image processing in Python. Next, we define two source variables that will help in storing the images. Source 1 is storing the image while we are reading the image by using the imread() function. In a similar way, source 2 helps in storing the second image. We again use the imread function to read the image, which reads the image and stores it in the source2 variable. Once the images are read, we can now use the addWeighted function, which will help us in blending the image as needed with alpha, beta and gamma values.
Here we stored the processed image in the dest variable. Here we have specified the alpha value as 0.5 and keeping the same value for the beta. The gamma value here is 0. As the alpha and beta values are the same, we can see the output as below, where we will see that the images are blended well, and there is not much difference. The output after writing the image is returned as true. We then use the imshow function to display the image.
Output:
Before Blending:
After Blending:
Here you will not observe much change in the images as they are blended with equal alpha and gamma values.
Let us have a look when the values are now changed to 1.
Code:
import cv2
# readin the two images
source1 = cv2.imread('eduCBA.JPG', cv2.IMREAD_COLOR)
source2 = cv2.imread('eduCBA.JPG', cv2.IMREAD_COLOR)
# blending the image with alpha and beta values as 1
dest = cv2.addWeighted(source1, 1, source2, 1, 0.0)
# Saving the output image
cv2.imwrite('img.png', dest)
cv2.imshow('img.png', dest)
# Wait for a key
cv2.waitKey(0)
# Destroy the window which is open
cv2.destroyAllWindows()
In a similar way, we read both images and then apply the addWeighted function to them. We then use the imwrite function, which returns a true value after the image is processed. To display the image, we use the imshow function, which gives us the below output. Please take note that we have changed alpha and beta values to 1 here, which shows us change in the images.
Output:
Before Blending:
After Blending:
There is a considerate change in the images when the values are changed to one. The image becomes more transparent and blended in the shade.
Conclusion
The addWeighted function is a function that helps in adding two images and also blending those by passing the alpha, beta and gamma values. In order to analyse images, this helps in adjusting the gradients and in the processing of the image. The blending of the images depends on alpha and beta values which are passed as arguments to this function.
Recommended Articles
We hope that this EDUCBA information on “OpenCV addWeighted” was beneficial to you. You can view EDUCBA’s recommended articles for more information.