Ultralytics YOLO27: Join the waitlist.
License YOLO
Back to Ultralytics Glossary

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 dnn module 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.

Explore solutions

Computer vision in sports

Computer vision in sports

Discover how Ultralytics YOLO powers sports vision with player and ball tracking, performance analytics, coaching insights, and athlete rehabilitation.
Learn more
Computer vision for aerial imagery

Computer vision for aerial imagery

Turn drone and aerial imagery into real-time insights for agriculture, aquaculture, conservation, and geospatial analysis with Ultralytics YOLO.
Learn more
Computer vision in aerospace

Computer vision in aerospace

Apply computer vision in aerospace with Ultralytics YOLO for aircraft inspection, runway FOD detection, airport ground operations, and composite defect detection.
Learn more
Computer vision in security

Computer vision in security

Protect every site with Ultralytics YOLO models. Vision AI powers perimeter monitoring, threat detection, license-plate recognition, and workplace safety.
Learn more
Computer vision in robotics

Computer vision in robotics

Power smarter machines with Ultralytics YOLO models. Vision AI in robotics drives autonomous navigation, perception, object tracking, and real-time control.
Learn more
Computer vision in logistics

Computer vision in logistics

Streamline logistics with Ultralytics YOLO models. Vision AI enables package inspection, sorting, vehicle tracking, and real-time warehouse safety monitoring.
Learn more
Computer vision in retail

Computer vision in retail

Reimagine retail with Ultralytics YOLO models. Vision AI powers inventory tracking, shelf monitoring, queue management, and smarter customer insights.
Learn more
Computer vision in healthcare

Computer vision in healthcare

Build healthcare solutions with Ultralytics YOLO models. Vision AI in healthcare powers faster medical imaging, smarter diagnostics, and patient monitoring.
Learn more
Computer vision in manufacturing

Computer vision in manufacturing

Optimize manufacturing with Ultralytics YOLO models. Vision AI drives quality control, defect detection, PPE compliance, and assembly line automation.
Learn more
Computer vision in automotive

Computer vision in automotive

Apply computer vision in automotive with Ultralytics YOLO models. Vision AI elevates road safety, driver assistance, and vehicle automation for smarter roads.
Learn more
Computer vision in agriculture

Computer vision in agriculture

Bring vision AI to smart agriculture with Ultralytics YOLO models. Power crop monitoring, livestock tracking, and precision farming for higher, smarter yields.
Learn more

Let's build the future of AI together!

Begin your journey with the future of machine learning