OpenCV (Open-Source Computer Vision Library) is an open-source library specially designed for computer vision, image processing, and machine learning tasks. It provides a comprehensive set of tools and functions to analyze, process, and manipulate images and videos. This library has more than 2500 optimized algorithms, which include a comprehensive set of both classic and state-of-the-art computer vision and machine learning algorithms.
It can be easily used with programming languages like Python, C++, etc, and is currently widely used in Healthcare (Medical image analysis), Surveillance and security (Motion detection and Face detection), Robotics, the Automotive industry, etc. OpenCV is primarily developed in C++. Let's work through some hands-on examples with OpenCV.
Installing OpenCV using pip
To get started, your system must have Python and the VS Code editor installed. I already have them installed on my system. Run the below command from the terminal in order to install the OpenCV using pip.
pip install opencv-python

Verifying the installation
After installation, you can verify it using the command below.
import cv2
print(cv2.__version__)

Reading and showing the image using OpenCV
In the below code, first, we have imported the OpenCV library which provides functions for reading, processing, and displaying images. The imread function reads the image and returns a NumPy array, which contains the pixel-wise data of the image. If the image is not loaded successfully, an error message will be shown to the user else, the shape of the image will be printed, which provides the dimensions(height, width, channel).
Then, the image is displayed in a window named “Sample Image” with the help of imshow function. I used the waitKey(0) to pause the program and wait for a user to press any key. After that, the destroyAllWindows is called to close all the OpenCV windows created during the execution of the program.
import cv2
#Read an image
image = cv2.imread("Sample-Image.jpg")
#Check if image not exists/not loaded successfully
if image is None:
print("Image not found")
else:
print(image.shape)
#imshow to show the image in window
cv2.imshow("Sample Image",image)
# Wait for a key press to close the window and
# close all OpenCV windows
cv2.waitKey(0)
cv2.destroyAllWindows()






Join the conversation! Your thoughts help the community grow.