OpenCV MODULE
Welcome to the world of Computer Vision, where machines can see and interpret the world around them. OpenCV, the Open Source Computer Vision Library, is your essential tool for exploring this fascinating domain using the Python programming language.
What is OpenCV?
OpenCV is a free, open-source computer vision and machine learning software library designed for real-time computer vision applications. It is written in C++ and has a Python interface for developers who prefer working in Python. With over two decades of development, OpenCV has grown into a powerful library that encompasses a wide array of computer vision tasks.
Key Features and Capabilities:
-
Image and Video Processing: OpenCV allows you to load, manipulate, and save images and videos effortlessly. It provides a rich set of functions for basic image processing, such as resizing, cropping, filtering, and transformation.
-
Object Detection and Tracking: You can build applications that detect and track objects in images or videos. This is vital for tasks like surveillance, robotics, and more.
-
Machine Learning: OpenCV includes machine learning tools for classification, clustering, and regression. It's a valuable resource for training and deploying machine learning models.
-
Face Recognition: It's widely used for face detection and recognition, making it a fundamental tool in applications like security systems, photo tagging, and more.
-
Camera Calibration: OpenCV can calibrate cameras, correct distortion, and recover 3D information from 2D images. This is essential in applications like 3D modeling, AR, and VR.
-
Augmented Reality (AR) and Virtual Reality (VR): With OpenCV, you can develop AR and VR applications by overlaying digital information on the real world or creating immersive environments.
-
Panoramic Image Stitching: Create stunning panoramic images by stitching multiple images together seamlessly.
-
Image Segmentation: Segmenting images into meaningful regions or objects is a critical task in computer vision, and OpenCV provides tools to achieve this.
Getting Started with OpenCV
For installing OpenCV Module in windows, open command prompt and give the following command :
pip install opencv-python
To use OpenCV library, it is necessary to import it as follows:
Note:
import matplotlib is used to plot the images on graph
import numpy is used to store the pixel value in an array
Loading Images and Choosing Color Space
Let's load an image using the imread function of cv2:
Output:
Output:
Output:
Output: (See colors are restored to the original)
Well, you must have noticed that there is something off about this image. There is lot of blue in here. Lets' understand the reason behind this.
OpenCV represents the images in BGR as opposed to the RGB we expect. Since it is in the reverse order, you tend to see the blue color in images.
We will use the following code for converting from BGR to RGB:
Now, let's also read the image as a grayscale image!
Note:
Here, the number zero opens the image as a grayscale image. And, cmap specifies color mapping, gray in this case.
Frequently, you will be asked to concentrate on a specific section of the picture, either to create a bounding box around the area, known as our "region of interest (ROI)," or to process the image and remove irrelevant portions.
In the end, algorithms will be utilized to determine our ROI automatically, but for now, let's manually extract the ROI!
Let's attempt to crop our picture. This time we have turned axis on so that we can get x and y coordinates.
Cropping the image
Output:
Now, to crop the rose from the our pic, we need to extract a square that focuses on the rose. Note the size of image by looking at the axis. Then we need to specify the roi which means [range of y and range of x]
The cv2.imwrite() function in OpenCV is used to save an image to a file. Here's how it works in the given code:
Output: Image will be saved as Rose.jpg in the local directory
Code Explanation:
-
cv2.imwrite(): This function writes an image to a specified file.
-
'Rose.jpg': This is the name of the file where the image will be saved. You can specify the file format by the file extension, like .jpg, .png, etc.
-
img: This is the image matrix that you want to save. It could be an image read using cv2.imread() or processed in some way.
-
The Rose.jpg file will be saved in the current working directory of the script. The current working directory is usually the folder where your script is being executed.