
- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
Arithmetic operations using OpenCV in Python
In this tutorial, we are going to perform arithmetic operations on images using OpenCV in Python. We need to install the OpenCV module.
Run the following command to install the OpenCV module.
pip install opencv-python==4.1.1.26
If you run the above command, you will get the following successful message.
Collecting opencv-python==4.1.1.26 Downloading https://files.pythonhosted.org/packages/1f/51/e0b9cef23098bc31c77b0e0 6221dd8d05119b9782d4c2b1d1482e22b5f5e/opencv_python-4.1.1.26-cp37-cp37m-win_amd64.w hl (39.0MB) Requirement already satisfied: numpy>=1.14.5 in c:\users\hafeezulkareem\anaconda3\l ib\site-packages (from opencv-python==4.1.1.26) (1.16.2) Installing collected packages: opencv-python Successfully installed opencv-python-4.1.1.26
Adding Two Images
We need two images for the addition. We have a method called cv2.add(image_one, image_two) to perform addition. It's very hand method. The sizes of the two images must be the same. Let's see the images.
Image One
Image two
Let's see the code.
Example
# importing cv2 module import cv2 # reading the images and storing in variables image_one = cv2.imread('_one.jpg') image_two = cv2.imread('_two.jpg') # adding two images result_image = cv2.add(image_one, image_two) # displaying the final image cv2.imshow('Final Image', result_image) # deallocating the memory if cv2.waitKey(0) & 0xff == 27: cv2.destroyAllWindows()
Output
Result Image
Subtraction
We have a method called cv2.substract(image_one, image_two) to perform subtraction on two images. We are going to use the same images as an addition. Let's see the code.
Example
# importing cv2 module import cv2 # reading the images and storing in variables image_one = cv2.imread('_one.jpg') image_two = cv2.imread('_two.jpg') # substracting two images result_image = cv2.subtract(image_one, image_two) # displaying the final image cv2.imshow('Final Image', result_image) # deallocating the memory if cv2.waitKey(0) & 0xff == 27: cv2.destroyAllWindows()
Output
Result Image
Conclusion
If you have any doubts regarding the tutorial, mention them in the comment section.
- Related Articles
- Arithmetic Operations on Images using OpenCV in Python
- Arithmetic operations in excel file using openpyxl in Python
- Explain Arithmetic operations using pointers in C language?
- How to perform arithmetic operations on a date in Python?
- Decimal arithmetic operations in Computer Architecture?
- What are Arithmetic Micro-operations?
- Template matching using OpenCV in Python
- C# Program to perform all Basic Arithmetic Operations
- Cartooning an Image using OpenCV in Python?
- Smile detection using haar cascade in OpenCV using Python
- How to perform the arithmetic operations on arrays in C language?
- How to perform arithmetic operations on two-dimensional array in C?
- Histograms Equalization using Python OpenCv Module
- Python Grayscaling of Images using OpenCV
- Program to extract frames using OpenCV in Python?
