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:
import cv2 as cv
import numpy as np

# Read an image
img = cv.imread('image.jpg')

# Read as grayscale
gray_img = cv.imread('image.jpg', cv.IMREAD_GRAYSCALE)

# Display the image
cv.imshow('Image', img)
cv.waitKey(0)
cv.destroyAllWindows()

Resizing Images

Resize images to specific dimensions or by a scale factor:
import cv2 as cv

img = cv.imread('image.jpg')

# Resize to specific dimensions
resized = cv.resize(img, (640, 480))

# Resize by scale factor
scale = 0.5
width = int(img.shape[1] * scale)
height = int(img.shape[0] * scale)
resized_scale = cv.resize(img, (width, height), interpolation=cv.INTER_LINEAR)

# Maintain aspect ratio - scale by factor
fx, fy = 0.5, 0.5
resized_aspect = cv.resize(img, None, fx=fx, fy=fy, interpolation=cv.INTER_AREA)
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:
import cv2 as cv

img = cv.imread('image.jpg')

# Crop using slicing [y1:y2, x1:x2]
cropped = img[100:400, 200:500]

# Crop a region of interest (ROI)
x, y, w, h = 100, 50, 300, 200
roi = img[y:y+h, x:x+w]

cv.imshow('Cropped', cropped)
cv.waitKey(0)

Rotating Images

Simple 90-degree Rotations

import cv2 as cv

img = cv.imread('image.jpg')

# Rotate 90 degrees clockwise
rotated_90 = cv.rotate(img, cv.ROTATE_90_CLOCKWISE)

# Rotate 90 degrees counter-clockwise
rotated_90_ccw = cv.rotate(img, cv.ROTATE_90_COUNTERCLOCKWISE)

# Rotate 180 degrees
rotated_180 = cv.rotate(img, cv.ROTATE_180)

Arbitrary Angle Rotation

import cv2 as cv
import numpy as np

img = cv.imread('image.jpg')
height, width = img.shape[:2]

# Get rotation matrix for 45 degrees
center = (width // 2, height // 2)
angle = 45
scale = 1.0
rotation_matrix = cv.getRotationMatrix2D(center, angle, scale)

# Apply rotation
rotated = cv.warpAffine(img, rotation_matrix, (width, height))

cv.imshow('Rotated', rotated)
cv.waitKey(0)

Flipping Images

import cv2 as cv

img = cv.imread('image.jpg')

# Flip horizontally (left-right)
flipped_h = cv.flip(img, 1)

# Flip vertically (top-bottom)
flipped_v = cv.flip(img, 0)

# Flip both directions
flipped_both = cv.flip(img, -1)

Image Filtering and Smoothing

Gaussian Blur

Remove noise and detail from images:
import cv2 as cv

img = cv.imread('image.jpg')

# Apply Gaussian blur
# Kernel size must be odd: (3,3), (5,5), (7,7), etc.
blurred = cv.GaussianBlur(img, (5, 5), 0)

# Larger kernel = more blur
blurred_more = cv.GaussianBlur(img, (15, 15), 0)

Median Blur

Excellent for removing salt-and-pepper noise:
import cv2 as cv

img = cv.imread('image.jpg')

# Apply median blur (kernel size must be odd)
median = cv.medianBlur(img, 5)

Bilateral Filter

Smooths images while preserving edges:
import cv2 as cv

img = cv.imread('image.jpg')

# d: diameter of pixel neighborhood
# sigmaColor: filter in color space
# sigmaSpace: filter in coordinate space
bilateral = cv.bilateralFilter(img, d=9, sigmaColor=75, sigmaSpace=75)

Complete Example: Image Processing Pipeline

import cv2 as cv
import numpy as np

# Load image
img = cv.imread('input.jpg')
if img is None:
    print('Error loading image')
    exit()

# Resize to a standard size
img = cv.resize(img, (800, 600))

# Apply Gaussian blur to reduce noise
blurred = cv.GaussianBlur(img, (5, 5), 0)

# Crop region of interest
h, w = blurred.shape[:2]
roi = blurred[h//4:3*h//4, w//4:3*w//4]

# Rotate the ROI
center = (roi.shape[1]//2, roi.shape[0]//2)
matrix = cv.getRotationMatrix2D(center, 15, 1.0)
rotated = cv.warpAffine(roi, matrix, (roi.shape[1], roi.shape[0]))

# Display results
cv.imshow('Original', img)
cv.imshow('Processed', rotated)
cv.waitKey(0)
cv.destroyAllWindows()

# Save result
cv.imwrite('output.jpg', rotated)
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:
# BGR to Grayscale
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)

# BGR to HSV
hsv = cv.cvtColor(img, cv.COLOR_BGR2HSV)

# BGR to RGB
rgb = cv.cvtColor(img, cv.COLOR_BGR2RGB)
Create binary images:
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)

# Simple threshold
ret, thresh = cv.threshold(gray, 127, 255, cv.THRESH_BINARY)

# Adaptive threshold
adaptive = cv.adaptiveThreshold(gray, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C,
                                cv.THRESH_BINARY, 11, 2)
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