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
OpenCV Python Program to blur an image?
OpenCV is one of the best Python packages for image processing. Like signals carry noise attached to them, images too contain different types of noise mainly from the source itself (camera sensor). Python's OpenCV package provides ways for image smoothing, also called blurring. One of the most common techniques is using a Gaussian filter for image blurring, which smooths sharp edges while minimizing excessive blur.
Syntax
cv2.GaussianBlur(src, ksize, sigmaX[, dst[, sigmaY[, borderType]]])
Parameters
src − Input image
ksize − Gaussian kernel size as (width, height). Both values must be odd and positive
sigmaX − Kernel standard deviation along X-axis (horizontal direction)
sigmaY − Kernel standard deviation along Y-axis (vertical direction). If 0, it equals sigmaX
dst − Output image (optional)
borderType − Pixel extrapolation method for image boundaries
Example 1: Basic Gaussian Blur
Here's a simple example that applies Gaussian blur with a small kernel size ?
import cv2
import numpy as np
# Create a sample image with text
img = np.ones((200, 400, 3), dtype=np.uint8) * 255
cv2.putText(img, 'OpenCV Blur Example', (50, 100), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 2)
# Apply Gaussian blur with small kernel
blurred = cv2.GaussianBlur(img, (5, 5), 0)
# Display both images
cv2.imshow('Original vs Blurred', np.hstack([img, blurred]))
cv2.waitKey(0)
cv2.destroyAllWindows()
Example 2: Different Blur Intensities
Let's compare different kernel sizes to see their effect on blurring ?
import cv2
import numpy as np
# Read an image (replace with your image path)
img = cv2.imread('image.jpg')
# Apply different levels of blur
blur_small = cv2.GaussianBlur(img, (5, 5), 0)
blur_medium = cv2.GaussianBlur(img, (15, 15), 0)
blur_large = cv2.GaussianBlur(img, (31, 31), 0)
# Create a comparison grid
top_row = np.hstack([img, blur_small])
bottom_row = np.hstack([blur_medium, blur_large])
result = np.vstack([top_row, bottom_row])
cv2.imshow('Blur Comparison', result)
cv2.waitKey(0)
cv2.destroyAllWindows()
Example 3: Using Different Sigma Values
You can control blur intensity using sigma values instead of relying on kernel size ?
import cv2
import numpy as np
# Create a sample image
img = np.zeros((300, 400, 3), dtype=np.uint8)
cv2.rectangle(img, (50, 50), (350, 250), (255, 255, 255), -1)
cv2.rectangle(img, (100, 100), (300, 200), (0, 0, 255), -1)
# Apply blur with different sigma values
blur_sigma_1 = cv2.GaussianBlur(img, (0, 0), sigmaX=1)
blur_sigma_3 = cv2.GaussianBlur(img, (0, 0), sigmaX=3)
blur_sigma_5 = cv2.GaussianBlur(img, (0, 0), sigmaX=5)
# Display results
result = np.hstack([img, blur_sigma_1, blur_sigma_3, blur_sigma_5])
cv2.imshow('Different Sigma Values', result)
cv2.waitKey(0)
cv2.destroyAllWindows()
Key Points
Kernel size must be odd numbers (3, 5, 7, 15, etc.)
Larger kernel sizes create more blur effect
When ksize is (0,0), kernel size is calculated from sigma values
Higher sigma values produce more pronounced blurring
Gaussian blur preserves edges better than simple averaging
Common Use Cases
Noise reduction in images
Pre-processing for edge detection
Creating background blur effects
Smoothing images for better feature extraction
Conclusion
Gaussian blur is an essential image processing technique in OpenCV that effectively reduces noise while preserving important image features. By adjusting kernel size and sigma values, you can control the blur intensity to suit your specific application needs.
