Image blur refers to the effect of making an image less sharp and more difficult to distinguish the details. Although blurring is typically a negative effect in photographs, it is useful method in image processing tasks. Image blur is used in image processing for a variety of reasons, such as image smoothing, edge detection, obscuring sensitive information, and preprocessing steps.
Image blur is achieved by convolving the image with a specific kernel or filter. A kernel is a small matrix that is applied to each pixel in the image. The value of each pixel in the output image is calculated by multiplying the corresponding values in the kernel with the values of the pixels in the neighborhood of the current pixel, and then summing the results.
In this tutorial, you'll briefly learn how to blur image with OpenCV API in Python.
The tutorial covers;
- Gaussian blur
- Median blur
- Bilateral filter method
- Averaging method
- Source code listing
We'll start by loading the required libraries.
import cv2 import matplotlib.pyplot as plt
Matplotlib
library to display the image, the RGB color order needs to be adjusted.
file = "/Users/user/Desktop/image.jpg"
img0 = cv2.imread(file)
img = cv2.cvtColor(img0, cv2.COLOR_BGR2RGB)
plt.imshow(img)
plt.show()
The Gaussian filter is a kernel that gives more weight to pixels that
are closer to the center pixel and less weight to pixels that are
farther away. It is implemented using the cv2.GaussianBlur()
function in OpenCV.
gaussian = cv2.GaussianBlur(img, (5, 5), 5) plt.imshow(gaussian) plt.show()
Median blur is a non-linear image smoothing filter that is used to reduce noise in an image while preserving edges. It works by replacing the color of each pixel with the median of the colors in its neighborhood.
Median blur is implemented using the cv2.medianBlur()
function in OpenCV. The function takes two parameters: the input image and the kernel size. The kernel size parameter is used to specify the size of the neighborhood to consider for calculating the median of each pixel.
median = cv2.medianBlur(img, 7) plt.imshow(median) plt.show()
cv2.bilateralFilter()
function is an
edge-preserving filter that is used to blur an image while preserving
the edges. Unlike the Gaussian filter, which only consider the pixel values in the
neighborhood, the bilateral filter also takes into account the pixel
intensity difference between the center pixel and the pixels in its
neighborhood.
bilateral = cv2.bilateralFilter(img, 7, 75, 75) plt.imshow(bilateral) plt.show()
cv2.blur()
function can be used to apply the
averaging method of blurring to an image. The function uses a
square-shaped kernel with equal values in all its elements and the sum
of all elements is equal to 1. The kernel is convolved with the image,
and the value of each pixel in the output image is calculated by
averaging the values of the pixels in the neighborhood of the current
pixel.cv2.blur()
function.
averaging = cv2.blur(img, (7, 7)) plt.imshow(averaging) plt.show()
Below code shows how to display above blurred images in a plot.
gaussian = cv2.GaussianBlur(img, (15, 15), 5) median = cv2.medianBlur(img, 25) bilateral = cv2.bilateralFilter(img, 21,61,61) average = cv2.blur(img, (25,25)) blurs = { "Gaussian blur":gaussian, "Median blur": median, "Average blur": average, "Bilateral blur": bilateral } i=0 fig, ax = plt.subplots(nrows=2, ncols=2, figsize=(12, 8)) for row in range(2): for col in range(2): ax[row][col].imshow(list(blurs.values())[i]) ax[row][col].set_title(list(blurs.keys())[i], fontsize=20) ax[row][col].axis('off') i = i + 1 plt.tight_layout() plt.show()
import cv2 import matplotlib.pyplot as plt
file = "/Users/user/Desktop/image.jpg"
img0 = cv2.imread(file)
img = cv2.cvtColor(img0, cv2.COLOR_BGR2RGB)
plt.imshow(img)
plt.show()
gaussian = cv2.GaussianBlur(img, (5, 5), 5) plt.imshow(gaussian) plt.show()
median = cv2.medianBlur(img, 7) plt.imshow(median) plt.show()
bilateral = cv2.bilateralFilter(img, 7, 75, 75) plt.imshow(bilateral) plt.show()
averaging = cv2.blur(img, (7, 7)) plt.imshow(averaging) plt.show()
gaussian = cv2.GaussianBlur(img, (15, 15), 5) median = cv2.medianBlur(img, 25) bilateral = cv2.bilateralFilter(img, 21,61,61) average = cv2.blur(img, (25,25)) blurs = { "Gaussian blur":gaussian, "Median blur": median, "Average blur": average, "Bilateral blur": bilateral } i = 0 fig, ax = plt.subplots(nrows=2, ncols=2, figsize=(12, 8)) for row in range(2): for col in range(2): ax[row][col].imshow(list(blurs.values())[i]) ax[row][col].set_title(list(blurs.keys())[i], fontsize=20) ax[row][col].axis('off') i = i + 1 plt.tight_layout() plt.show()
No comments:
Post a Comment