OpenCV Tutorial For Beginners- Part 3

OpenCV is your best choice if you are planning to develop applications that use computer vision to work like self-driving cars, recognizing the license plate of the vehicles, counting objects, to name a few.

In the previous article, we’ve talked about converting the image colors, saving the image, and making the image quality looks better using the OpenCV library. Still, this tutorial will help you learn to perform arithmetic operations on the pictures.

1. Arithmetic Operations on Image

You can perform arithmetic operations on the images, such as adding two images, one above the other, like you do in photo editing software like photoshop. Also, you can perform subtraction, where you will subtract the pixel values from two images and merge them. 

1.1. Adding Image

There are many other arithmetic operations you can perform on the images, but we will stick with these two for now, and let’s see adding two images on top of each other:

import cv2 as cv #Import OpenCV

img1 = cv.imread("Image.jpg") #Import the image
img2 = cv.imread("Image2.jpg") #Import the image

add = cv.addWeighted(img1, 0.5, img2, 0.4, 0) #Add images

cv.imshow("add", add) #Show the image

cv.waitKey(0) #Milliseconds time to show the image
cv.destroyAllWindows() #Close the window
OpenCV
First Image: Source
OpenCV
Second Image: Source
OpenCV
Final Result

You can see that we could merge these two images when you run the previous code using the .addWeighted() function. 

You will need first to specify the first image, and then you will need to set a value which is 0.5 in the previous case, and it represents the weight of the image to be applied to the final result. 

Feel free to try changing this value to have more clues on how it affects the final result. After that, you will need to specify the second image and its weight which is 0.4 in this case. Finally, set the gamma value, which is the measurement of the light. You can also use another simple function to add these two images:

import cv2 as cv #Import OpenCV

img1 = cv.imread("Image.jpg") #Import the image
img2 = cv.imread("Image2.jpg") #Import the image

add = cv.add(img1, img2) #Add images

cv.imshow("add", add) #Show the image

cv.waitKey(0) #Milliseconds time to show the image
cv.destroyAllWindows() #Close the window
OpenCV

We’ve used the .add() function, which is more straightforward than the previous one. You will need only to specify the two images without any other arguments, but it will give you less control of the light and weight of every image in the final result. 

Remember that both images must be in the exact resolution to work correctly. In the previous examples, the dimensions were 600×400 pixels for all pictures.

1.2. Subtracting Images

As we can add two images, we can also subtract the pictures where you will subtract the pixel values from two photos and merge them. Let me show you an example:

import cv2 as cv #Import OpenCV

img1 = cv.imread("Image.jpg") #Import the image
img2 = cv.imread("Image2.jpg") #Import the image

subtract = cv.subtract(img1, img2) #Subtract images

cv.imshow("subtract", subtract) #Show the image

cv.waitKey(0) #Milliseconds time to show the image
cv.destroyAllWindows() #Close the window
OpenCV

The final results show an ugly picture where we have subtracted the second image from the first one using the .subtract() function followed by the two images.

2. Bitwise Operators

Another type of arithmetic operation is called the bitwise operator, which consists of four types (AND, OR, XOR, NOT). It is used a lot in image manipulations and things like adding watermarks for the images. 

This technique works on individual pixels, delivering better results than the other algorithms or tools. Let’s first see an example about the Bitwise_AND operation:

import cv2 as cv #Import OpenCV

img1 = cv.imread("draw1.png") #Import the image
img2 = cv.imread("draw2.png") #Import the image

Bitwise_AND = cv.bitwise_and(img2, img1) #Bitwise_AND

cv.imshow("Bitwise_AND", Bitwise_AND) #Show the image

cv.waitKey(0) #Milliseconds time to show the image
cv.destroyAllWindows() #Close the window
OpenCV
First Image
OpenCV
Second Image
OpenCV
Final Result

If we have both black sections, the final result will be black. If you have black and white, the final result will be black, but white with white, it will show you the white color. This is what Bitwise_AND operations do. Let’s move now to the Bitwise_OR operation:

import cv2 as cv #Import OpenCV

img1 = cv.imread("draw1.png") #Import the image
img2 = cv.imread("draw2.png") #Import the image

Bitwise_OR = cv.bitwise_or(img2, img1) #Bitwise_OR

cv.imshow("Bitwise_OR", Bitwise_OR) #Show the image

cv.waitKey(0) #Milliseconds time to show the image
cv.destroyAllWindows() #Close the window
OpenCV

If you have both images in black, the result will be black. If both are white, you will have a white screen. But, if you have white & black screens, then the final one will be white. 

That’s how the Bitwise_OR operation works but let’s now see another example with the Bitwise_XOR operation:

import cv2 as cv #Import OpenCV

img1 = cv.imread("draw1.png") #Import the image
img2 = cv.imread("draw2.png") #Import the image

Bitwise_XOR = cv.bitwise_xor(img2, img1) #Bitwise_XOR

cv.imshow("Bitwise_XOR", Bitwise_XOR) #Show the image

cv.waitKey(0) #Milliseconds time to show the image
cv.destroyAllWindows() #Close the window
OpenCV

When you have black & white screens, they will be white. If you have two white screens, they will be black. If both of them are black, then it will be black. Let’s find out the last one, which is the Bitwise_NOT operation:

import cv2 as cv #Import OpenCV

img1 = cv.imread("draw1.png") #Import the image
img2 = cv.imread("draw2.png") #Import the image

Bitwise_NOT1 = cv.bitwise_not(img1) #Bitwise_NOT1
Bitwise_NOT2 = cv.bitwise_not(img2) #Bitwise_NOT2

cv.imshow("Bitwise_NOT1", Bitwise_NOT1) #Show the image
cv.imshow("Bitwise_NOT2", Bitwise_NOT2) #Show the image

cv.waitKey(0) #Milliseconds time to show the image
cv.destroyAllWindows() #Close the window
OpenCV
Bitwise_NOT1
OpenCV
Bitwise_NOT2

You can see that Bitwise_NOT will reverse everything. If the screen is black, it will be white, and if the screen is white, it will be black. Notice that this operation will work only on one image, not two like in the previous examples.

Conclusion

Thanks for reading! I hope you’ve learned something new here in this article about OpenCV that discussed how to perform arithmetic operations on the images. This tutorial will be helpful later when you build some computer vision applications using the OpenCV library.

Scroll to Top