Definition
The same library helps a car keep its lane, a factory spot cracked bottles and your phone straighten a scanned receipt. OpenCV has been the workhorse of computer vision for over twenty years.
OpenCV (Open Source Computer Vision Library) is a free library of more than 2,500 algorithms for images and video, started at Intel in 1999 and first released in 2000. It is written in C++ with Python bindings, imported as cv2, and covers reading and writing images, resizing, colour conversion, filtering, edge detection, contours, feature matching, camera calibration, video processing and running trained deep-learning models.
In simple words
OpenCV is like a huge toolbox for photos and videos. Instead of writing your own code to blur, rotate or find edges, you pick the right tool and use it in one line.
Overview
- OpenCV (Open Source Computer Vision Library) is the most widely used library for image and video processing.
- It began at Intel in 1999, is written in C++ and has Python bindings used as import cv2.
- OpenCV images are NumPy arrays, so NumPy operations work on them directly.
- It loads images in BGR order rather than RGB.
- It includes thousands of algorithms, from blurring to face detection and camera calibration.
- This site's browser cannot run OpenCV, so lessons show NumPy and Pillow equivalents.
Why it matters
OpenCV is used in robotics, manufacturing, medical imaging, agriculture and mobile apps. Its classic tools often solve a problem faster and more reliably than deep learning, and it is the standard way to prepare images before and after a neural network.
Key terms
- cv2
- The Python module name for OpenCV.
- imread and imwrite
- Functions that load an image file into an array and save an array to a file.
- Resize and interpolation
- Changing image size, estimating new pixel values from neighbours.
- Thresholding
- Turning a grayscale image into black and white by comparing each pixel with a cut-off.
- Region of interest (ROI)
- A cropped part of an image selected for processing.
- VideoCapture
- An OpenCV object that reads frames from a camera or video file.
Types
OpenCV's functions fall into several types of operation. These are the groups you will use most.
1. Input and output
Reading and writing images and video, and showing them.
- How it works: cv2.imread and cv2.imwrite handle files; cv2.VideoCapture reads cameras or video files frame by frame; cv2.imshow displays a window.
- Example: Reading webcam frames at 30 per second for a live gesture detector.
- When to use it: Use them at the start and end of every vision pipeline, checking that images loaded correctly.
2. Geometric transformations
Resizing, cropping, rotating, flipping and warping images.
- How it works: cv2.resize changes size with chosen interpolation; cv2.warpAffine rotates and shifts; cv2.warpPerspective corrects tilted views of documents.
- Example: Straightening a photographed bill so OCR can read it.
- When to use it: Use them to prepare images to the size and orientation a model expects.
3. Colour conversion and thresholding
Changing colour spaces and turning images into black-and-white masks.
- How it works: cv2.cvtColor converts BGR to grey or HSV; cv2.threshold and adaptive thresholding separate foreground from background; Otsu's method picks the threshold automatically.
- Example: Binarising a scanned page before reading its text.
- When to use it: Use them to simplify images before finding shapes, text or objects.
4. Filtering and feature detection
Blurring, sharpening, edge detection and finding key points.
- How it works: cv2.GaussianBlur smooths noise; cv2.Canny finds edges; ORB and SIFT detect distinctive points that can be matched between images.
- Example: Matching key points to stitch several photos into a panorama.
- When to use it: Use them for classical vision tasks and for preprocessing before deep learning.
5. Contours, drawing and deep-learning modules
Finding object outlines, drawing results and running neural networks.
- How it works: cv2.findContours traces shapes in a mask; cv2.rectangle and cv2.putText draw boxes and labels; the cv2.dnn module runs pre-trained models such as YOLO.
- Example: Counting coins on a table and drawing a labelled box around each.
- When to use it: Use them to measure objects, show predictions and deploy models without a heavy framework.
Explained in detail
Core operations
OpenCV images are NumPy arrays, so everything you know about arrays still works. The table lists the operations you will use most. You can install it on your own computer with pip install opencv-python. The runnable example on this page uses NumPy and Pillow, which run directly in your browser, to perform the same operations, so you can see exactly what each OpenCV call does to the pixels.
| OpenCV call | What it does |
|---|---|
| img = cv2.imread("leaf.jpg") | Loads the image as a BGR array; returns None if the path is wrong |
| rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) | Converts colour space; also BGR2GRAY and BGR2HSV |
| small = cv2.resize(img, (224, 224)) | Resizes to width 224 and height 224 |
| roi = img[50:150, 100:200] | Crops rows 50 to 149 and columns 100 to 199 with array slicing |
| flipped = cv2.flip(img, 1) | Mirrors left to right |
| _, bw = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY) | Pixels above 127 become white, the rest black |
| cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 2) | Draws a green box two pixels thick, as detection results are shown |
| cap = cv2.VideoCapture(0); ok, frame = cap.read() | Reads one frame from the default webcam |
Thresholding and choosing the cut-off
A fixed threshold works when lighting is even, as on a scanner. In uneven light, adaptive thresholding computes a separate cut-off for each small neighbourhood, and Otsu's method, published by Nobuyuki Otsu in 1979, chooses the single cut-off that best separates the dark and bright groups of pixels automatically. Document-scanning apps use these steps to turn a phone photo of a page into crisp black text on white.
- OpenCV can run trained neural networks through its dnn module, and it is often paired with PyTorch or TensorFlow models.
- A common bug is displaying an OpenCV image with another library and getting blue faces: convert BGR to RGB first.
- Resizing changes the aspect ratio unless you scale both sides by the same factor or pad the image.
OpenCV also handles geometry. Functions such as getPerspectiveTransform and warpPerspective straighten a photo of a tilted document into a flat, rectangular scan, and camera calibration removes lens distortion so measurements in millimetres are accurate.
How it works
- Install with pip install opencv-python and import cv2.
- Read the image and check that it is not None.
- Convert colour space, resize and crop as needed.
- Apply processing such as thresholding, filtering or edge detection.
- Draw results and save or display the output.
In code
import numpy as np
from PIL import Image
# Build a 6 x 8 grayscale "image": a bright square on a dark background
gray = np.full((6, 8), 40, dtype=np.uint8)
gray[1:5, 1:4] = 200
print("Original:\n", gray)
roi = gray[1:4, 1:5] # crop: rows 1-3, columns 1-4
print("Cropped region of interest:\n", roi)
flipped = gray[:, ::-1] # like cv2.flip(img, 1)
print("First row before and after flip:", gray[1], flipped[1])
bw = np.where(gray > 127, 255, 0).astype(np.uint8) # like cv2.threshold(gray, 127, 255, THRESH_BINARY)
print("Thresholded:\n", bw)
small = np.array(Image.fromarray(gray).resize((4, 3), Image.NEAREST)) # like cv2.resize(img, (4, 3))
print("Resized to width 4, height 3:\n", small)
bgr_pixel = np.array([255, 0, 0]) # OpenCV order: blue, green, red
print("BGR [255, 0, 0] read as RGB looks:", "blue" if bgr_pixel[::-1][2] == 255 else "red")Output
Original: [[ 40 40 40 40 40 40 40 40] [ 40 200 200 200 40 40 40 40] [ 40 200 200 200 40 40 40 40] [ 40 200 200 200 40 40 40 40] [ 40 200 200 200 40 40 40 40] [ 40 40 40 40 40 40 40 40]] Cropped region of interest: [[200 200 200 40] [200 200 200 40] [200 200 200 40]] First row before and after flip: [ 40 200 200 200 40 40 40 40] [ 40 40 40 40 200 200 200 40] Thresholded: [[ 0 0 0 0 0 0 0 0] [ 0 255 255 255 0 0 0 0] [ 0 255 255 255 0 0 0 0] [ 0 255 255 255 0 0 0 0] [ 0 255 255 255 0 0 0 0] [ 0 0 0 0 0 0 0 0]] Resized to width 4, height 3: [[200 200 40 40] [200 200 40 40] [ 40 40 40 40]] BGR [255, 0, 0] read as RGB looks: blue
Try it yourself
Counting bottle caps on a line
A small bottling plant uses OpenCV to check that every bottle has a cap.
Match the OpenCV call to its job
What does each call do?
- cv2.imread
- cv2.cvtColor
- cv2.threshold
- cv2.rectangle
- cv2.VideoCapture
Common mistakes
- Not checking whether imread returned None for a wrong path.
- Forgetting that cv2.resize takes (width, height), while shape is (height, width).
- Displaying BGR images as RGB.
Check yourself
Summary
- OpenCV is a free library of thousands of image and video algorithms, used as cv2 in Python.
- Images are NumPy arrays in BGR order; crop with slicing, resize, convert and threshold with single calls.
- Classic operations solve many tasks and prepare images for neural networks.
Finished reading?
Sources: OpenCV documentation and tutorials · Richard Szeliski, Computer Vision: Algorithms and Applications (2nd ed.)