Skip to main content

Image Operations

Learn how to perform basic image operations in OpenCV, including resizing, cropping, rotating, flipping, and applying various filters to images.

Reading and Displaying Images

Before performing any operations, you need to load an image:

Resizing Images

Resize images to specific dimensions or by a scale factor:
Interpolation methods:
  • INTER_LINEAR: Bilinear interpolation (default)
  • INTER_AREA: Best for shrinking images
  • INTER_CUBIC: Bicubic interpolation for enlarging
  • INTER_LANCZOS4: Lanczos interpolation over 8x8 neighborhood

Cropping Images

Crop images using array slicing:

Rotating Images

Simple 90-degree Rotations

Arbitrary Angle Rotation

Flipping Images

Image Filtering and Smoothing

Gaussian Blur

Remove noise and detail from images:

Median Blur

Excellent for removing salt-and-pepper noise:

Bilateral Filter

Smooths images while preserving edges:

Complete Example: Image Processing Pipeline

When working with images, always check if the image was loaded successfully before performing operations. Use if img is None: in Python or if (img.empty()) in C++.

Additional Operations

Convert between different color spaces:
Create binary images:
Be careful with image data types when performing operations. OpenCV typically uses uint8 (0-255) for display, but intermediate calculations may require float32 or float64 to avoid overflow.

Next Steps