- OpenCV Python - Digit Recognition
- OpenCV Python - Feature Matching
- OpenCV Python - Feature Detection
- OpenCV Python - Meanshift/Camshift
- OpenCV Python - Face Detection
- OpenCV Python - Video from Images
- OpenCV Python - Images From Video
- OpenCV Python - Play Videos
- OpenCV Python - Capture Videos
- OpenCV Python - Fourier Transform
- OpenCV Python - Image Blending
- OpenCV Python - Image Addition
- OpenCV Python - Image Pyramids
- OpenCV Python - Template Matching
- OpenCV Python - Image Contours
- OpenCV Python - Transformations
- OpenCV Python - Color Spaces
- OpenCV Python - Histogram
- OpenCV Python - Edge Detection
- OpenCV Python - Image Filtering
- OpenCV Python - Image Threshold
- OpenCV Python - Resize and Rotate
- OpenCV Python - Add Trackbar
- OpenCV Python - Mouse Events
- OpenCV Python - Shapes and Text
- OpenCV Python - Bitwise Operations
- OpenCV Python - Image Properties
- OpenCV Python - Using Matplotlib
- OpenCV Python - Write Image
- OpenCV Python - Reading Image
- OpenCV Python - Environment
- OpenCV Python - Overview
- OpenCV Python - Home
OpenCV Python Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
OpenCV Python - Image Addition
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.
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
Instead of a pnear 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
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.
By varying a from 0 -> 1, a smooth transition takes place from one image to another, so that they blend together.
First image is given a weight of 0.3 and the second image is given 0.7. The gamma factor is taken as 0.
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.
Advertisements