OpenCV
OpenCV is an open-source library of 2,500+ algorithms for image processing and computer vision. What it does, how it pairs with deep learning, and code.
OpenCV (Open Source Computer Vision Library) is an open-source library of more than 2,500 optimized algorithms for image processing and computer vision (CV). It reads and writes images and video, captures camera streams, filters and transforms pixels, detects and matches features, calibrates cameras, and runs trained neural networks. OpenCV began at Intel Research in 1999, is written in C++ with bindings for Python and Java, and has been distributed under the Apache 2.0 license since version 4.5.0. In Python it installs with pip install opencv-python and imports as cv2, and it represents every image as a NumPy array.
What OpenCV Does#
- Image and video I/O: reads and writes common image formats, decodes video files, and captures frames from cameras and network streams.
- Image processing: resizing, color space conversion, blurring, sharpening, thresholding, and morphological operations, the standard preprocessing steps before an image reaches a model.
- Feature detection and matching: corner and keypoint detectors such as SIFT and ORB find distinctive points that can be matched across images for panorama stitching, object localization, and visual SLAM. SIFT moved into the main library in version 4.4.0 after its patent expired.
- Camera geometry: calibration removes lens distortion, homographies correct perspective, and stereo matching recovers depth from two cameras for robotics and 3D reconstruction.
- Video analysis: background subtraction separates moving objects from a static scene, and optical flow measures how pixels move between frames.
- Deep neural network inference: the
dnnmodule loads trained models, for example in ONNX format, and runs them on CPU or GPU without a deep learning framework installed.
This classical pipeline finds the outlines of shapes in an image without any learned model:
import urllib.request
import cv2
import numpy as np
# Download a sample image and decode it into a BGR NumPy array
data = urllib.request.urlopen("https://ultralytics.com/images/bus.jpg").read()
image = cv2.imdecode(np.frombuffer(data, np.uint8), cv2.IMREAD_COLOR)
# Classical pipeline: grayscale, blur to suppress noise, then Canny edge detection
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
edges = cv2.Canny(blurred, 50, 150)
# Trace connected edge pixels into contours, the outlines of shapes in the scene
contours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
print(f"Image shape: {image.shape}, contours found: {len(contours)}")It prints Image shape: (1080, 810, 3), contours found: 1449: a 1080×810 image with three color channels, and 1,449 separate outlines traced from its edges. OpenCV stores color channels in blue-green-red (BGR) order, so images passed to libraries that expect RGB need a cv2.cvtColor conversion first.
OpenCV and Deep Learning Frameworks#
OpenCV and deep learning frameworks such as PyTorch and TensorFlow work at different stages of the same pipeline. OpenCV handles data preprocessing and output around the network, grabbing frames, resizing them, drawing results, and writing output video, and the framework trains and runs the convolutional neural network in between. A real-time video application loops through these steps for every frame:
Ultralytics YOLO26 accepts OpenCV's BGR NumPy frames directly, and results[0].plot() returns an annotated frame in the same format, so the model drops into an OpenCV loop without any conversion. This example runs one pass of that loop on the sample image bundled with the ultralytics package:
import cv2
from ultralytics import YOLO
from ultralytics.utils import ASSETS
# OpenCV loads the bundled sample image as a BGR NumPy array
frame = cv2.imread(str(ASSETS / "bus.jpg"))
# YOLO26 takes the OpenCV frame directly
model = YOLO("yolo26n.pt")
results = model(frame)
# plot() returns an annotated BGR array that OpenCV can display or save
annotated = results[0].plot()
cv2.imwrite("bus_annotated.jpg", annotated)Inside a video loop, the same three calls run on every frame from cv2.VideoCapture, and cv2.imshow or cv2.VideoWriter replaces cv2.imwrite.










