Arithmetic Operations
Arithmetic Operations like Addition, Subtraction, and Bitwise Operations(AND, OR, NOT, XOR) can be applied to the input images. These operations can be helpful in enhancing the properties of the input images. Image arithmetics are important for analyzing the input image properties. The operated images can be further used as an enhanced input image, and many more operations can be applied for clarifying, thresholding, dilating, etc of the image.
Addition of Image:
We can add two images by using function cv2.add(). This directly adds up image pixels in the two images. But adding the pixels is not an ideal situation. So, we use cv2.addweighted(). Remember, both images should be of equal size and depth.
Input Image1:
Input Image2:
Python3
# Python program to illustrate # arithmetic operation of # addition of two images # organizing imports import cv2 import numpy as np # path to input images are specified and # images are loaded with imread command image1 = cv2.imread( 'star.jpg' ) image2 = cv2.imread( 'dot.jpg' ) # cv2.addWeighted is applied over the # image inputs with applied parameters weightedSum = cv2.addWeighted(image1, 0.5 , image2, 0.4 , 0 ) # the window showing output image # with the weighted sum cv2.imshow( 'Weighted Image' , weightedSum) # De-allocate any associated memory usage if cv2.waitKey( 0 ) & 0xff = = 27 : cv2.destroyAllWindows() |
Output:
Subtraction of Image:
Just like in addition, we can subtract the pixel values in two images and merge them with the help of cv2.subtract(). The images should be of equal size and depth.
Python3
# Python program to illustrate # arithmetic operation of # subtraction of pixels of two images # organizing imports import cv2 import numpy as np # path to input images are specified and # images are loaded with imread command image1 = cv2.imread( 'star.jpg' ) image2 = cv2.imread( 'dot.jpg' ) # cv2.subtract is applied over the # image inputs with applied parameters sub = cv2.subtract(image1, image2) # the window showing output image # with the subtracted image cv2.imshow( 'Subtracted Image' , sub) # De-allocate any associated memory usage if cv2.waitKey( 0 ) & 0xff = = 27 : cv2.destroyAllWindows() |
Output:
Getting Started with Python OpenCV
Computer Vision is one of the techniques from which we can understand images and videos and can extract information from them. It is a subset of artificial intelligence that collects information from digital images or videos.
Python OpenCV is the most popular computer vision library. By using it, one can process images and videos to identify objects, faces, or even handwriting of a human. When it is integrated with various libraries, such as NumPy, python is capable of processing the OpenCV array structure for analysis.
In this article, we will discuss Python OpenCV in detail along with some common operations like resizing, cropping, reading, saving images, etc with the help of good examples.