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
Selected Reading
Blurring an image using the OpenCV function blur()
In this tutorial, we will learn how to blur an image using the OpenCV blur() function. Blurring reduces image noise and detail by averaging pixel values in a neighborhood defined by a kernel.
Algorithm
Step 1: Import OpenCV and NumPy libraries Step 2: Load the input image Step 3: Define the kernel size for blurring Step 4: Apply the blur() function with image and kernel parameters Step 5: Display or save the blurred result
Understanding the blur() Function
The cv2.blur() function performs simple box filtering. It takes the average of all pixels in the kernel area and replaces the central pixel with this average value.
Syntax
cv2.blur(src, ksize, dst=None, anchor=None, borderType=None)
Parameters
- src: Input image
- ksize: Kernel size as (width, height) tuple
- dst: Output image (optional)
- anchor: Anchor point (optional, default is center)
- borderType: Border extrapolation method (optional)
Example
Here's a complete example that demonstrates image blurring ?
import cv2
import numpy as np
# Create a sample image with some patterns
image = np.zeros((200, 200, 3), dtype=np.uint8)
cv2.rectangle(image, (50, 50), (150, 150), (255, 255, 255), -1)
cv2.circle(image, (100, 100), 30, (0, 0, 255), -1)
print("Original image shape:", image.shape)
# Define kernel size for blurring
kernel_size = (15, 15)
# Apply blur function
blurred_image = cv2.blur(image, kernel_size)
print("Blurred image shape:", blurred_image.shape)
print("Kernel size used:", kernel_size)
Original image shape: (200, 200, 3) Blurred image shape: (200, 200, 3) Kernel size used: (15, 15)
Different Kernel Sizes
The kernel size determines the amount of blur. Larger kernels create more blur ?
import cv2
import numpy as np
# Create a sample image
image = np.ones((100, 100, 3), dtype=np.uint8) * 255
cv2.rectangle(image, (25, 25), (75, 75), (0, 0, 0), 2)
# Test different kernel sizes
kernel_sizes = [(3, 3), (9, 9), (21, 21)]
for i, kernel_size in enumerate(kernel_sizes):
blurred = cv2.blur(image, kernel_size)
print(f"Kernel size {kernel_size}: Blur applied successfully")
Kernel size (3, 3): Blur applied successfully Kernel size (9, 9): Blur applied successfully Kernel size (21, 21): Blur applied successfully
Key Points
- Kernel size must be positive odd numbers
- Larger kernels produce more blur but slower processing
- The function works with both grayscale and color images
- Box filter blur is faster but less sophisticated than Gaussian blur
Comparison with Other Blur Methods
| Method | Function | Quality | Speed |
|---|---|---|---|
| Simple Blur | cv2.blur() |
Basic | Fast |
| Gaussian Blur | cv2.GaussianBlur() |
High | Medium |
| Median Blur | cv2.medianBlur() |
Noise reduction | Slow |
Conclusion
The cv2.blur() function provides a simple way to reduce image detail through box filtering. Choose kernel size based on desired blur intensity, with larger kernels producing stronger blur effects.
Advertisements
