OpenCV (Open Source Computer Vision Library) is the most widely used library for computer vision and image processing. It can read images and video, detect faces and objects, track movement, and much more. This page introduces OpenCV-Python, shows how to install it, and gives you a roadmap of the tutorials in this series.
What You Will Learn
- What OpenCV and OpenCV-Python are
- Where OpenCV is used in the real world
- How to install and test OpenCV
- How images are stored in OpenCV
Prerequisites
Basic Python: variables, lists, loops and functions. Python 3.8 or newer installed.
What Is OpenCV?
OpenCV is an open-source library written in C++ with more than 2,500 algorithms for image processing, computer vision and machine learning. It was started by Intel in 1999 and is now free for both personal and commercial use. OpenCV-Python is the Python interface to it: you write simple Python code, and the heavy work runs in fast C++ code underneath.
Why OpenCV Works So Well with Python
In OpenCV-Python, every image is a NumPy array. That means you can use all of NumPy, and libraries built on it such as Matplotlib and scikit-learn, directly on images and video frames.
Where Is OpenCV Used?
| Area | Examples |
|---|---|
| Security | Face detection and recognition, surveillance, intrusion alerts |
| Traffic | Vehicle counting, number plate reading, speed estimation |
| Retail | People counting in malls, shelf monitoring |
| Industry | Automated inspection of products for defects |
| Healthcare | Medical image analysis, X-ray and scan processing |
| Robotics | Navigation, object tracking, gesture control |
Install OpenCV
pip install opencv-python
Explanation: This installs the main modules. If you need extra modules such as face recognition (cv2.face), install opencv-contrib-python instead. Do not install both at the same time.
| Package | Use it when |
|---|---|
opencv-python |
Normal desktop use |
opencv-contrib-python |
You need extra modules like face recognition |
opencv-python-headless |
Servers and Docker, where there is no screen (no imshow) |
Test Your Installation
import cv2
print(cv2.__version__)
4.x.x
If a version number prints, OpenCV is ready. The exact number depends on what pip installed.
Your First OpenCV Program
import cv2
img = cv2.imread('machinelearning.png')
print(type(img), img.shape)
cv2.imshow('My first image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Explanation:
cv2.imread()loads the image as a NumPy array.img.shapeshows (height, width, channels).imshow,waitKeyanddestroyAllWindowsshow the image until you press a key.
Learning Path for This Series
Follow the tutorials in this order:
- Reading an Image as Color or Grayscale
- Extracting RGB Values of a Pixel
- Extracting a Region of Interest
- Resizing an Image
- Rotating an Image
- Drawing a Rectangle
- Displaying Text on an Image
- Color Channels of an Image
- Arithmetic Operations on Images
- Access Camera and Record Video
- Face Detection from an Image, from Webcam, and Face Recognition with Name
- Projects: Vehicle Detection and Bus Detection
Common Errors
| Problem | Cause and fix |
|---|---|
ModuleNotFoundError: No module named 'cv2' |
Install with pip install opencv-python, using the same Python that runs your script (python -m pip install opencv-python). |
cv2.imshow error on a server or Colab |
You have the headless package or no display. Use Matplotlib or google.colab.patches.cv2_imshow. |
Interview Questions
Q: What is OpenCV?
An open-source computer vision and image processing library, written in C++ with Python bindings.
Q: How is an image represented in OpenCV-Python?
As a NumPy array of shape (height, width, channels) with dtype uint8, in BGR order.
Summary
OpenCV is the standard library for computer vision. Install it with pip, remember that images are NumPy arrays in BGR order, and follow the learning path above.
Next: Start: Reading an Image
Machine Learning Tutorials, Courses and Certifications