OpenCV Python – Image Addition


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


Image Addition

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

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.


Gamma Factor

Advertisements

”;

Leave a Reply

Your email address will not be published. Required fields are marked *