Skip to main content

Overview

The OpenCV Python bindings provide a Pythonic interface to OpenCV’s C++ API. The bindings are automatically generated and offer excellent performance while maintaining ease of use. All functions work seamlessly with NumPy arrays.

Installation

The easiest way to install OpenCV for Python:
The opencv-python package includes prebuilt binaries for Windows, macOS, and Linux. No compilation required.

Version Requirements

  • Python 3.6 or higher
  • NumPy (automatically installed as a dependency)

Building from Source

For custom builds or the latest development version:

Quick Start

Importing OpenCV

The module is imported as cv2 for historical reasons. This naming convention is standard across all OpenCV Python code.

Verify Installation

Core Concepts

NumPy Integration

OpenCV images are represented as NumPy arrays:

Image Format: BGR vs RGB

OpenCV uses BGR color order by default, not RGB. Convert when working with other libraries like Matplotlib or PIL.

Code Examples

Reading and Writing Images

Video Capture and Display

Face Detection

Image Processing Pipeline

Histogram Calculation and Visualization

Working with ROI (Region of Interest)

Image Transformations

Advanced Features

Working with Multiple Images

Performance Tips

Package Structure

The Python bindings are organized to mirror the C++ API:

Common Issues

AttributeError: If you get “module ‘cv2’ has no attribute”, make sure you’re using the correct module name and that the feature is included in your OpenCV build.

Virtual Environments

Resources

Next Steps