본문 바로가기
Lecture/OpenCV Master with Python (Beginner)

OpenCV + Python Filters and Convolutions

by codingwalks.en 2024. 10. 24.
728x90
반응형

 

 

Hello. This is codingwalks.

In this article, we will learn about various image filtering techniques using OpenCV and Python. We will understand and implement filtering techniques through the basic theory, mathematical expressions, and actual code of filtering. In particular, we will cover sharpening and unsharp masking.

 

1. Image Filtering

Image filtering is the operation of manipulating the pixel values ​​of an image to emphasize features or reduce noise. Filters are used to smooth an image, make it sharper, or extract specific features. Below, we will explain the principle of convolution, which is important in filtering operations. Convolution is an operation that transforms each pixel of an image using a small filter (kernel).

Image filtering is a basic operation in image processing that modifies or enhances an image. One of the most common techniques for image filtering is compositing. Compositing is a mathematical operation that combines two functions to produce a third function. In the context of image processing, one function is the input image and the other is the kernel (also called a filter or mask). The compositing operation slides the kernel over the image, performs element-wise multiplication, and sums the results to produce an output pixel value, and this process is repeated for every pixel in the image. Kernels are used to smooth images, make them sharper, or extract specific features.

\[ G(x, y) = \sum_{i=-k}^{k} \sum_{j=-k}^{j} I(x+i, y+j) \cdot H(i, j) \]

Where \( I(x, y) \) is the input image, \( H(i, j) \) is the kernel, and \( G(x, y) \) is the output image.

Source code:

import cv2
import numpy as np

# Load an image
img = cv2.imread('resources/lena.bmp')

# Define a Gaussian blur kernel
kernel_blur = np.array([[1, 2, 1],
                       [2, 4, 2],
                       [1, 2, 1]]) / 16

# Apply Gaussian blur using convolution
blurred_img = cv2.filter2D(img, -1, kernel_blur)

# Define a sharpening kernel
kernel_sharpen = np.array([[0, -1, 0],
                         [-1, 5, -1],
                         [0, -1, 0]])

# Apply sharpening using convolution
sharpened_img = cv2.filter2D(img, -1, kernel_sharpen)

# Display the results
cv2.imshow('Original Image', img)
cv2.imshow('Blurred Image', blurred_img)
cv2.imshow('Sharpened Image', sharpened_img)
cv2.waitKey(0)
cv2.destroyAllWindows()

2d-image filtering

This code shows how to apply blur and sharpness to an image using the cv2.filter2D() function, a synthesis operation in OpenCV.

 

2. Image Blurring

Various types of filters have different characteristics and advantages and disadvantages, and which filter to use depends on the characteristics of the image and the purpose of processing. Generally, Gaussian noise uses the average filter or Gaussian filter, impulse noise uses the median filter, and when noise removal and edge preservation are desired at the same time, a bilateral filter is used.

2.1. Average filter

This is a filter that smoothes the image by averaging the pixel values ​​in the kernel.

\[ G(x, y) = \frac{1}{n^2} \sum_{i=-k}^{k} \sum_{j=-k}^{j} I(x+i, y+j) \]

Here, \( n \) is the size of the kernel.

Advantages:

  • It is simple to implement and requires little calculation, making it suitable for real-time processing.
  • Effective for removing random noises such as Gaussian noise.

Disadvantages:

  • Image details (edges, corners, etc.) may become blurred.
  • Image boundaries may become blurred.
  • Not effective for all types of noise.

OpenCV functions:

average_blurred = cv2.blur(image, (5, 5))

2.2. Gaussian filter

A filter that reduces high-frequency noise in an image using a Gaussian kernel, and has better edge preservation than the mean filter.

\[ G(x, y) = \sum_{i=-k}^{k} \sum_{j=-k}^{j} I(x+i, y+j) \cdot \frac{1}{2\pi\sigma^2} e^{-\frac{x^2 + y^2}{2\sigma^2}} \]

Where \( \sigma \) is the standard deviation of the Gaussian distribution.

Advantages:

  • You can get a more natural blurring effect than the mean filter.
  • It is very effective in removing Gaussian noise.

Disadvantages:

  • The computational amount is more than the mean filter.
  • The loss of image details may still occur.

OpenCV function:

gaussian_blurred = cv2.GaussianBlur(img, (5, 5), 0)

2.3. Median filter

A nonlinear filter that sorts pixel values ​​in the kernel in ascending or descending order and replaces the median value of the sorted values ​​with the new value of the pixel to remove noise. It is particularly effective in removing salt-and-pepper noise.

Advantages:

  • Very effective in removing impulse noise (salt and pepper noise).
  • Preserves image details relatively well.
  • Less edge blurring.

Disadvantages:

  • It is not as effective as the mean filter or Gaussian filter in removing Gaussian noise.
  • The computational effort is greater than that of the mean filter.

OpenCV function:

median_blurred = cv2.medianBlur(img, 5)

2.4. Bilateral filter

This is a filter that removes noise while preserving edges. In the filtering process, it considers not only the difference in pixel values ​​but also the spatial distance. It is a filter that filters using Gaussian weights in two areas: the spatial domain and the pixel value domain. In the spatial domain, weights are given according to the spatial distance, and in the value domain, weights are given according to the difference in pixel values.

Advantages:

  • It is excellent in preserving edges while removing noise.
  • You can get a natural blurring effect.

Disadvantages:

  • It may not be suitable for real-time processing because the amount of calculation is very large.
  • Parameter adjustment is complicated.

OpenCV function:

bilateral_filtered = cv2.bilateralFilter(img, 9, 75, 75)

2.5. Full source code

import cv2
import matplotlib.pyplot as plt

def apply_blurring(image):
    # Average Blur
    avg_blur = cv2.blur(image, (5, 5))
    # Gaussian Blur
    gauss_blur = cv2.GaussianBlur(image, (5, 5), 0)
    # Median Blur
    median_blur = cv2.medianBlur(image, 5)
    # Bilateral Filter
    bilateral = cv2.bilateralFilter(image, 9, 75, 75)
    return avg_blur, gauss_blur, median_blur, bilateral

# Load an image
image = cv2.imread('input_image.jpg', cv2.IMREAD_GRAYSCALE)

# Apply blurring techniques
avg, gauss, median, bilateral = apply_blurring(image)

# Display the results
cv2.imshow('Average Blur', avg)
cv2.imshow('Gaussian Blur', gauss)
cv2.imshow('Median Blur', median)
cv2.imshow('Bilateral Filter', bilateral)
cv2.waitKey(0)
cv2.destroyAllWindows()

image blurring

 

3. Embossing

Embossing is a filtering technique that emphasizes the transition between different areas of an image, creating a 3D-like effect. Embossing works by replacing each pixel with a highlight or shadow, depending on the border of the original image, using the embossing kernel below.

\[ K = \begin{bmatrix}
0 & -1 & -1 \\
1 & 0 & -1 \\
1 & 1 & 0
\end{bmatrix} \]

import cv2
import matplotlib.pyplot as plt

def apply_emboss(image):
    # Embossing kernel
    kernel = np.array([[0, -1, -1],
                       [1,  0, -1],
                       [1,  1,  0]])
    
    return cv2.filter2D(image, -1, kernel, anchor=(-1,-1), delta=128)

# Load an image
image = cv2.imread('resources/lena.bmp', cv2.IMREAD_GRAYSCALE)

# Apply embossing filter
embossed = apply_emboss(image)

# Display results
cv2.imshow('Original Image', image)
cv2.imshow('Embossed Image', embossed)
cv2.waitKey(0)
cv2.destroyAllWindows()

image embossing

 

4. Image Sharpening and Unsharp Mask

4.1. Sharpening

This is a technique to sharpen an image by emphasizing its edges through convolution operations. It uses the following sharpening kernel.

\[ K = \begin{bmatrix}
-1 & -1 & -1 \\
-1 & 9 & -1 \\
-1 & -1 & -1
\end{bmatrix} \]

OpenCV Function:

kernel = np.array([[-1, -1, -1], [-1, 9, -1], [-1, -1, -1]])
sharpened = cv2.filter2D(img, -1, kernel)

4.2. Unsharp Mask

Unsharp Mask is a technique that emphasizes edges by using the difference between the blurred image and the original image.

\[ I_{\text{unsharp}} = I + \lambda (I - G(I)) \]

Where \( I \) is the original image, \( G(I) \) is the Gaussian blurred image, and \( \lambda \) is a coefficient that controls the sharpening strength.

OpenCV function:

# Gaussian blur processing
gaussian_blurred = cv2.GaussianBlur(img, (9, 9), 10.0)
# Apply unsharp mask
unsharp_image = cv2.addWeighted(img, 1.5, gaussian_blurred, -0.5, 0)

4.3. Full source code

import cv2
import numpy as np
import matplotlib.pyplot as plt

def apply_sharpening(image):
    kernel = np.array([[-1, -1, -1],
                       [-1,  9, -1],
                       [-1, -1, -1]])
    return cv2.filter2D(image, -1, kernel)

def apply_unsharpening_mask(image):
    gaussian = cv2.GaussianBlur(image, (5,5), 2)
    return cv2.addWeighted(image, 1.5, gaussian, -0.5, 0)

# Load an image
image = cv2.imread('resources/lena.bmp', cv2.IMREAD_GRAYSCALE)

# Apply filters
sharpened = apply_sharpening(image)
unsharpened = apply_unsharpening_mask(image)

# Display the results
cv2.imshow('Sharpened', sharpened)
cv2.imshow('Unsharp Mask', unsharpened)
cv2.waitKey(0)
cv2.destroyAllWindows()

sharpening and unsharp mask

 

5. Conclusion

In this article, we looked at various image filtering techniques using OpenCV and Python. Filtering plays a very important role in image processing and can be applied for various purposes such as noise removal, edge enhancement, and image detail preservation. The mean filter and Gaussian filter remove noise from the image and make it smooth, but may lose details. The median filter is excellent for removing impulse noise such as salt-and-pepper noise, and the bilateral filter performs well in both noise removal and edge preservation. In addition, we were able to emphasize details by sharpening the edges of the image using the sharpening filter and unsharp mask. While sharpening directly sharpens the image, unsharp masking allows for detailed adjustments by utilizing the difference from the blurred image. The selection of filters in image processing depends on the characteristics of the image and the purpose of processing, and appropriate use of such filtering techniques can greatly improve image quality in various situations. We hope that you will continue to apply these filtering techniques to your projects to create better images.

 

If you found this post useful, please like and subscribe below. ^^

Buy me a coffee

 

[Codingwalks]에게 송금하기 - AQR

[Codingwalks]에게 송금하기 - AQR

aq.gy

★ All contents are referenced from the link below. ★

 

OpenCV: OpenCV-Python Tutorials

Core Operations In this section you will learn basic operations on image like pixel editing, geometric transformations, code optimization, some mathematical tools etc.

docs.opencv.org

728x90
반응형