- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Arithmetic Operations on Images using OpenCV in Python
In this tutorial, we are going to learn about the arithmetic operations on Images using OpenCV. We can apply operations like addition, subtraction, Bitwise Operations, etc.., Let's see how we can perform operations on images.
We need the OpenCV module to perform operations on images. Install the OpenCV module using the following command in the terminal or command line.
pip install opencv-python==22.214.171.124
If you run the above command, you will get the following successful message.
Collecting opencv-python==126.96.36.199 Downloading https://files.pythonhosted.org/packages/1f/51/e0b9cef23098bc31c77b0e0 6221dd8d05119b9782d4c2b1d1482e22b5f5e/opencv_python-188.8.131.52-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==184.108.40.206) (1.16.2) Installing collected packages: opencv-python Successfully installed opencv-python-220.127.116.11
We can add two images using the cv2.addWeighted(). It takes five arguments, two images, and the weight of the final image from both and the light value for the final image.
Now we are going to add those two images into one image.
# importing cv2 module import cv2 # reading the images and storing in variables image_one = cv2.imread('_1.jpg') image_two = cv2.imread('_2.jpg') # adding two images result_image = cv2.addWeighted(image_one, 0.5, image_two, 0.5, 0) # displaying the final image cv2.imshow('Final Image', result_image) # deallocating the memory if cv2.waitKey(0) & 0xff == 27: cv2.destroyAllWindows()
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.
# importing cv2 module import cv2 # reading the images and storing in variables image_one = cv2.imread('_1.jpg') image_two = cv2.imread('_2.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()
If you have any doubts regarding the tutorial, mention them in the comment section.
- Arithmetic operations using OpenCV in Python
- Draw geometric shapes on images using Python OpenCv module
- Python Grayscaling of Images using OpenCV
- Performing white TopHat operation on images using OpenCV
- Performing white BlackHat operation on images using OpenCV
- How to perform arithmetic operations on a date in Python?
- Arithmetic operations in excel file using openpyxl in Python
- Erosion and Dilation of images using OpenCV in Python
- Addition and Blending of images using OpenCv in Python
- Reading and displaying images using OpenCV
- How to perform Bitwise Not operation on images using Java OpenCV?
- Dilating images using the OpenCV function dilate()
- Explain Arithmetic operations using pointers in C language?
- How to perform Bitwise And operation on two images using Java OpenCV?
- How to perform Bitwise XOR operation on two images using Java OpenCV?