Gaussian blur

Mehmet Akif Cifci
2 min readFeb 22, 2023

--

Gaussian blur is a commonly used image processing technique that is often used as a preprocessing step before performing other operations such as edge detection, image segmentation, or object recognition. It is a type of spatial filtering that smooths an image by convolving it with a Gaussian kernel.

In OpenCV, the cv2.GaussianBlur() function is used to apply a Gaussian blur to an image. Here is an example code snippet:

import cv2
import numpy as np

# Load an image
img = cv2.imread('image.jpg')

# Apply Gaussian blur with kernel size of 5x5 and sigma value of 0
blur = cv2.GaussianBlur(img, (5, 5), 0)

# Display the resulting images side-by-side
cv2.imshow('Original Image', img)
cv2.imshow('Blurred Image', blur)
cv2.waitKey(0)
cv2.destroyAllWindows()

In the code above, we first load an image using the cv2.imread() function. We then apply a Gaussian blur to the image using the cv2.GaussianBlur() function. The second argument to this function is the size of the Gaussian kernel, which specifies the extent of the blurring. In this case, we use a kernel size of 5x5. The third argument is the sigma value, which controls the amount of blurring. A higher sigma value results in more blurring.

Finally, we display the original and blurred images side-by-side using the cv2.imshow() function. The cv2.waitKey() function waits for a key press before closing the window, while cv2.destroyAllWindows() closes all windows opened by OpenCV.

Note that the choice of kernel size and sigma value will depend on the specific application and the level of blurring desired. In general, a larger kernel size and/or sigma value will result in more blurring.

--

--

Mehmet Akif Cifci
Mehmet Akif Cifci

Written by Mehmet Akif Cifci

Mehmet Akif Cifci holds the position of associate professor in the field of computer science in Austria.

No responses yet