Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Downsampling an image using OpenCV
In this program, we will down sample an image. Downsampling is decreasing the spatial resolution while keeping the 2D representation of an image. It is typically used for zooming out of an image. We will use the pyrDown() function in the OpenCV library to complete this task.
What is Image Downsampling?
Image downsampling reduces the number of pixels in an image by decreasing its spatial resolution. OpenCV's pyrDown() function performs Gaussian pyramid downsampling, which smooths the image and reduces its size by half in each dimension.
Algorithm
Step 1: Read the image using cv2.imread() Step 2: Check the original image dimensions Step 3: Apply pyrDown() function to downsample Step 4: Compare dimensions and display result
Example Code
import cv2
import numpy as np
# Create a sample image for demonstration
image = np.random.randint(0, 255, (400, 600, 3), dtype=np.uint8)
print("Size of image before pyrDown:", image.shape)
# Apply pyrDown to downsample the image
downsampled_image = cv2.pyrDown(image)
print("Size of image after pyrDown:", downsampled_image.shape)
# Display both images (requires display environment)
cv2.imshow('Original Image', image)
cv2.imshow('Downsampled Image', downsampled_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Output
Size of image before pyrDown: (400, 600, 3) Size of image after pyrDown: (200, 300, 3)
How pyrDown() Works
The pyrDown() function performs the following operations ?
- Gaussian Blur: Applies a 5x5 Gaussian filter to smooth the image
- Subsampling: Removes every second row and column
- Size Reduction: Results in an image that is approximately 1/4 the size of the original
Multiple Downsampling Levels
import cv2
import numpy as np
# Create original image
original = np.random.randint(0, 255, (320, 480, 3), dtype=np.uint8)
print("Original size:", original.shape)
# First level downsampling
level1 = cv2.pyrDown(original)
print("Level 1 size:", level1.shape)
# Second level downsampling
level2 = cv2.pyrDown(level1)
print("Level 2 size:", level2.shape)
# Third level downsampling
level3 = cv2.pyrDown(level2)
print("Level 3 size:", level3.shape)
Output
Original size: (320, 480, 3) Level 1 size: (160, 240, 3) Level 2 size: (80, 120, 3) Level 3 size: (40, 60, 3)
Key Points
- Each
pyrDown()operation reduces image dimensions by approximately half - The function applies Gaussian smoothing before downsampling to prevent aliasing
- Total pixel count is reduced to about 1/4 of the original
- Useful for creating image pyramids and multi-scale processing
Conclusion
OpenCV's pyrDown() function efficiently downsamples images by combining Gaussian smoothing with subsampling. This technique is essential for image pyramids, multi-scale analysis, and reducing computational complexity in computer vision applications.
