How to blur image in OpenCV Python
In this post, you will learn how to blur an image using the Python OpenCV.
Blurring is a great option if you're trying to maintain the aesthetic of an image. It deems the private or unnecessary information. Blurring proves to be useful for sending documents, updates, purchase orders, or any information internally that contains client personal info you'd prefer to keep hidden.
Python OpenCV has several filtering techniques to perform smoothing operations on images. These smoothing techniques are generally used to reduce noise, reduce detail, and so on. These techniques can also be applied to reduce the pixelated effect in low resolution images.
Blur image using blur() method
We can use the blur() method of Python OpenCV to blur an image.
Syntax of cv2.blur()cv2.blur(image, ksize)
Here, the image is the image source, and ksize is the size of blurring kernel.
import cv2
import numpy as np
# load the image into system memory
image = cv2.imread('mountain.jpg',
flags=cv2.IMREAD_COLOR)
cv2.imshow('Original Image', image)
image_blurred = cv2.blur(src=image, ksize=(5, 5))
cv2.imshow('Blur Image', image_blurred)
cv2.waitKey()
cv2.destroyAllWindows()
Output of the above code:
Blur image using Gaussian blur filter
The OpenCV Gaussian filtering provides the cv2.GaussianBlur() method to blur an image by using a Gaussian Kernel. Each pixel in an image gets multiplied by a Gaussian Kernel. It means, a Gaussian Kernel is a square array of pixels.
Syntax of Gaussian Filtercv2.GaussianBlur(src, ksize, sigma_x, dst, sigma_y, border_type)
src- the input image,
ksize- Gaussian kernel size (width and height), the width and height can have different values and must be positive and odd,
sigma_x- Gaussian kernel standard deviation along the X-axis,
dst- output image,
sigma_y- Gaussian kernel standard deviation along the Y-axis,
boader_type- image boundaries.
The given Python program uses the gaussianblur filter to blur an image.
import cv2
import numpy
# image path
path = r'cat.jpg'
# using imread()
img = cv2.imread(path)
dst = cv2.GaussianBlur(img,(5,5),cv2.BORDER_DEFAULT)
cv2.imshow('image', numpy.hstack((img, dst)))
cv2.waitKey(0);
cv2.destroyAllWindows();
cv2.waitKey(1)
Python blur image using median blur filter
We can also use the Python OpenCV cv2.medianBlur() function to blur an image with a median kernel. This is a non-linear filtering technique. It is highly effective in removing salt-and-pepper noise. This takes the median of all the pixels under the kernel area and replaces the central component with this median value.
Syntax of Median Filtercv2.medianBlur(image, ksize)
Here, the image represents the image for operation. The ksize is a size object representing the size of the kernel.
Example of Median FilterIn the given program, we are importing the required modules. Then we are reading the image using the imread() function. Then we are utilizing the medianBlur() function to remove the noise from the image. Then we are displaying the noiseless image as the output on the screen.
import cv2
import numpy
# image path
path = r'cat.jpg'
# using imread()
img = cv2.imread(path)
dst = cv2.medianBlur(img,7)
cv2.imshow('image', numpy.hstack((img, dst)))
cv2.waitKey(0);
cv2.destroyAllWindows();
cv2.waitKey(1)
Output of the above code:
Related Articles
Python OpenCV Overview and ExamplesDraw different shapes on image using Python OpenCV
OpenCV Logical Operators- Bitwise AND, OR, NOR, XOR
Python OpenCV Histogram of Grayscale Image
Python OpenCV Image Filtering
Python OpenCV Overlaying or Blending Two Images
Python OpenCV Histogram of Color Image
Arithmetic Operations on Images using Python OpenCV
Detect Specific Color From Image using Python OpenCV
Capture a video in Python OpenCV and save
Contour Detection using Python OpenCV
Python OpenCV ColorMap
Adaptive Thresholding in Python OpenCV
Geometric Transformation OpenCV Python
Find the stop words in nltk Python
Install NLTK for Python on Windows 64 bit
Eye Detection Program in Python OpenCV
Python Turtle Graphics Overview