In this tutorial, we’ll explore OpenCV, short for Open Source Computer Vision Library is one of the most popular and widely used libraries for Computer Vision and image processing. Originally developed by Intel in 1999, OpenCV has become a cornerstone in the field of Computer Vision, enabling developers and researchers to create real-time applications that analyze and interpret visual data.
What is OpenCV?
OpenCV is an open-source library designed to provide a unified infrastructure for Computer Vision applications. It supports image processing, video analysis, object detection, machine learning, and real-time operations. It is cross-platform, supporting programming languages like Python, C++, Java, and more, and runs on operating systems such as Windows, Linux, macOS, Android, and iOS.
How to Get Started with OpenCV
To start using OpenCV in Python:
Install the library:
pip install opencv-python
Next, import the library in your Python script:
import cv2 import numpy as np
Drawing Shapes on an Image
Let’s go step by step and learn how to draw lines, rectangles, and circles.
1. Create a Blank Image
First, create a blank canvas where you can draw shapes:
image = np.zeros((512, 512, 3), dtype=np.uint8)
2. Draw a Line
To draw a line, use the cv2.line() function:
# Draw a white line cv2.line(image, (50, 50), (450, 50), (255, 255, 255), thickness=5)
-
Explaintation:
-
image: The image where the line will be drawn.
-
(50, 50): Starting point of the line.
-
(450, 50): Ending point of the line.
-
(255, 255, 255): Color in BGR format (white in this case).
-
thickness==5: Thickness of the line.
-
output:
Terminal: array([[[0, 0, 0], [0, 0, 0], [0, 0, 0], ..., [0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0], ..., [0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0], ..., [0, 0, 0], [0, 0, 0], [0, 0, 0]], ..., [[0, 0, 0], [0, 0, 0], [0, 0, 0], ..., [0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0], ..., [0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0], ..., [0, 0, 0], [0, 0, 0], [0, 0, 0]]], dtype=uint8)
3. Draw a Rectangle
Use cv2.rectangle( ) to draw a rectangle:
# Draw a green rectangle cv2.rectangle(image, (100, 100), (400, 300), (0, 255, 0), thickness=3)
-
Explaintation:
-
(100, 100): Top-left corner of the rectangle.
-
(400, 300): Bottom-right corner of the rectangle.
-
(0, 255, 0): Green color in BGR format.
-
thickness=3: Thickness of the rectangle border. Use -1 to fill it.
-
Output:
Terminal: array([[[0, 0, 0], [0, 0, 0], [0, 0, 0], ..., [0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0], ..., [0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0], ..., [0, 0, 0], [0, 0, 0], [0, 0, 0]], ..., [[0, 0, 0], [0, 0, 0], [0, 0, 0], ..., [0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0], ..., [0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0], ..., [0, 0, 0], [0, 0, 0], [0, 0, 0]]], dtype=uint8)
4. Draw a Circle
To draw a circle, use cv2.circle():
# Draw a blue circle cv2.circle(image, (256, 256), radius=50, color=(255, 0, 0), thickness=-1)
-
Explaintation:
-
(256, 256): Center of the circle.
-
radius=50: Radius of the circle.
-
(255, 0, 0): Blue color in BGR format.
-
thickness=-1: Fills the circle when thickness is negative.
-
Output:
Terminal: array([[[0, 0, 0], [0, 0, 0], [0, 0, 0], ..., [0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0], ..., [0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0], ..., [0, 0, 0], [0, 0, 0], [0, 0, 0]], ..., [[0, 0, 0], [0, 0, 0], [0, 0, 0], ..., [0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0], ..., [0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0], ..., [0, 0, 0], [0, 0, 0], [0, 0, 0]]], dtype=uint8)
Displaying the Image
After drawing these shapes on your image:
# Display the image with shapes cv2.imshow("Shapes", image) cv2.waitKey(0) cv2.destroyAllWindows()
Complete Code Example
Here’s the full code combining all the steps:
import cv2 import numpy as np # Create a black canvas image = np.zeros((512, 512, 3), dtype=np.uint8) # Draw a white line cv2.line(image, (50, 50), (450, 50), (255, 255, 255), thickness=5) # Draw a green rectangle cv2.rectangle(image, (100, 100), (400, 300), (0, 255, 0), thickness=3) # Draw a blue circle cv2.circle(image, (256, 256), radius=50, color=(255, 0, 0), thickness=-1) # Display the result cv2.imshow("Shapes", image) cv2.waitKey(0) cv2.destroyAllWindows()
Output:
Conclusion
OpenCV is a versatile tool that empowers developers to create innovative solutions in Computer Vision. Its open-source nature ensures continuous evolution through community contributions, making it a vital resource for academic research and commercial applications alike. Whether you’re a beginner or an expert, OpenCV offers the tools necessary to unlock the potential of visual data processing.