Skip to main content

Image as Matrix

In OpenCV, images are represented as Mat objects - multi-dimensional arrays where:
  • Grayscale images: Single-channel 2D arrays (CV_8UC1)
  • Color images: Multi-channel 2D arrays (typically CV_8UC3 for BGR)
  • Pixel values: Usually 8-bit unsigned integers (0-255)

Image Properties

Dimensions

Data Type

Color Spaces

OpenCV uses BGR (not RGB) as the default color order:
Most other libraries use RGB order. Always convert when interfacing with external systems.
See Color Spaces for conversion details.

Pixel Access

Single Pixel

Efficient Row Access

Multi-Channel Access

Image Operations

Creating Images

Copying Images

Image ROI

Channel Operations

Split and Merge

Extract Single Channel

Image I/O

Reading Images

Writing Images

Common Patterns

Convert to Grayscale

Resize Image

Crop Image

Performance Tips

Use Pointer Access

Prefer ptr<T>() over at<T>() for pixel-level operations

Check Continuity

Continuous matrices enable faster processing

Avoid Copies

Use references and ROI instead of cloning

Batch Operations

Use OpenCV functions instead of pixel loops

See Also