Overview of OpenCV.
OpenCV is a huge open-source library for computer vision, machine learning, and image processing. It can process images and videos to identify objects, faces, or even the handwriting of a human. When it is integrated with various libraries, such as Numpy which is a highly optimized library for numerical operations, then whatever operations one can do in Numpy can be combined with OpenCV.
Install the library:
OpenCV can be used by many programming language. In Python, there is a library 'opencv-python' from which we can use cv2 module. To install the library:
$ pip install opencv-python
We need Numpy also for some practices. So, also install numpy:
$ pip install numpy
Lets take some example for better understanding.
1. Create the image by using numpy.
# import cv2 # import numpy
Python uses image data in the form of a Numpy array, i.e. [Height, Width, Channel] format. Every Pixel is an array. So by using array, we can create (not click or capture) a image.
numpy.zeros((500,500,3),numpy.uint8)
numpy.zeros() function create the array of given size having all the element zero(0). Now apply the crop operation and assign different color combination values (0-255, 0-255,0-255). Now using the cv2 module, we can open the image.
cv2.imshow('image',img5) # open the image with window name 'image' cv2.waitKey(0) # wait for any key to stop the process cv2.destroyAllWindows() # close the window
If we want to save the image in our memory, then imwrite() function will perform this action.
2. Collapse two images.
Here , I have three images of different pixel or size. Lets collapse these images and create something new.
As we know, image is an array in form of Numpy having some rows and column. So we can iperform many operation using numpy function.
cv2.imread('path/of/image') # to open the image
There is an image of size(457,500,3) as shown in the image. Now in numpy, there are functions vtsack() and hstack() by which we can add two arrays vertically and horizontally.
numpy.hstack((arr1,arr2))
3. Swap a portion of image with other:
cv2.cvtColor(img , color)
cvtColor() convert the color of the image.
Now , we can swap a portion of the image with other image. In numpy, there is a function copy() that copy the entire array. After copying, we have two same images. So we can take dimension and then swap that portion using slicing in the array.
We can slice the rows and column and swap them.
This is the fun activity to learn cv2 easily.
Thank you for reading.