Skip to main content
The Core module is the foundation of OpenCV, providing essential data structures, basic operations, and utilities that other modules depend on.

Overview

From the OpenCV source (~/workspace/source/modules/core/include/opencv2/core.hpp:62-67):
The Core module is the backbone of OpenCV, offering fundamental data structures, matrix operations, and utility functions that other modules depend on. It’s essential for handling image data, performing mathematical computations, and managing memory efficiently within the OpenCV ecosystem.

Key Components

Mat Class

N-dimensional dense array for storing images and matrices

Array Operations

Mathematical operations on arrays and matrices

Persistence

XML/YAML/JSON file I/O for data structures

Utilities

System functions, logging, and error handling

Mat - The Core Data Structure

The Mat class is OpenCV’s primary container for images and matrices.

Creating Mat Objects

Mat Properties

Array Operations

The core module provides extensive array operations defined in opencv2/core.hpp.

Arithmetic Operations

Element-wise Operations

Matrix Operations

Reduction Operations

From core.hpp:210-215, OpenCV provides reduction types:

Statistical Functions

Border Handling

From core.hpp:223-244, OpenCV provides borderInterpolate() for extrapolation:

Data Persistence

Save and load data structures to XML/YAML/JSON files.

Utility Functions

Exception Handling

From core.hpp:112-156:

System Information

Data Types

OpenCV supports various data types:

Sorting

From core.hpp:158-167:

Memory Management

Example: Matrix Operations

From samples/cpp/cout_mat.cpp:

Best Practices

Memory Efficiency:
  • Use Mat::clone() only when you need a deep copy
  • Prefer ROI over copying when working with image regions
  • Let Mat destructor handle memory cleanup automatically
Type Safety:
  • Always check Mat::type() before operations
  • Use convertTo() for type conversion
  • Verify dimensions match before matrix operations

Source Reference

Key header file: ~/workspace/source/modules/core/include/opencv2/core.hpp See also:
  • opencv2/core/mat.hpp - Mat class implementation
  • opencv2/core/operations.hpp - Array operations
  • opencv2/core/persistence.hpp - File I/O