Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

NumPy - Convolution



NumPy Convolution

Convolution in NumPy is a mathematical operation used to combine two arrays (such as signals or images) in a specific way to produce a third array. This operation helps in filtering, smoothing, and detecting features within the data.

When you perform convolution, you slide one array (called the kernel or filter) over another array (the input) and calculate the sum of element-wise multiplications at each position. This process enhances certain aspects of the input array, like edges in an image or specific frequencies in a signal.

In NumPy, you can use the numpy.convolve() function for one-dimensional arrays and scipy.ndimage.convolve() for multi-dimensional arrays to perform convolution, which is widely used in signal processing and image analysis.

1D Convolution

One-dimensional convolution is commonly used in signal processing. It involves sliding one signal (kernel) over another (input signal) and computing the dot product at each position.

Example: 1D Convolution

In the following example, we perform a 1D convolution between an input signal and a kernel using NumPy −

import numpy as np

# Define the input signal
input_signal = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])

# Define the convolution kernel
kernel = np.array([0.2, 0.5, 0.2])

# Perform 1D convolution
convolved_signal = np.convolve(input_signal, kernel, mode='same')

print("Input signal:", input_signal)
print("Kernel:", kernel)
print("Convolved signal:", convolved_signal)

The result shows the input signal, kernel, and convolved signal −

Input signal: [1 2 3 4 5 6 7 8 9]
Kernel: [0.2 0.5 0.2]
Convolved signal: [0.9 1.8 2.7 3.6 4.5 5.4 6.3 7.2 6.1]

2D Convolution

Two-dimensional convolution is widely used in image processing for tasks such as blurring, sharpening, and edge detection. It involves sliding a 2D kernel over a 2D input array (image) and computing the dot product at each position.

Example: 2D Convolution

In the following example, we perform a 2D convolution on a sample image using a kernel −

import numpy as np
from scipy.signal import convolve2d

# Define a sample image (2D array)
image = np.array([
    [1, 2, 3, 4],
    [5, 6, 7, 8],
    [9, 10, 11, 12],
    [13, 14, 15, 16]
])

# Define a 2D convolution kernel
kernel = np.array([
    [0.1, 0.2, 0.1],
    [0.2, 0.4, 0.2],
    [0.1, 0.2, 0.1]
])

# Perform 2D convolution
convolved_image = convolve2d(image, kernel, mode='same', boundary='fill', fillvalue=0)

print("Image:\n", image)
print("Kernel:\n", kernel)
print("Convolved image:\n", convolved_image)

The result shows the original image, kernel, and convolved image −

Image:
[[ 1  2  3  4]
 [ 5  6  7  8]
 [ 9 10 11 12]
 [13 14 15 16]]
Kernel:
[[0.1 0.2 0.1]
 [0.2 0.4 0.2]
 [0.1 0.2 0.1]]
Convolved image:
[[ 2.4  4.   5.2  4.5]
 [ 6.4  9.6 11.2  9.2]
 [11.2 16.  17.6 14. ]
 [10.8 15.2 16.4 12.9]]

3D Convolution

Three-dimensional convolution is used in various applications such as 3D image processing, video processing, and volumetric data analysis. It involves sliding a 3D kernel over a 3D input array and computing the dot product at each position.

Example: 3D Convolution

In the following example, we perform a 3D convolution on a sample 3D array −

import numpy as np
from scipy.ndimage import convolve

# Define a sample 3D array
array_3d = np.random.rand(4, 4, 4)

# Define a 3D convolution kernel
kernel_3d = np.ones((3, 3, 3)) / 27

# Perform 3D convolution
convolved_3d = convolve(array_3d, kernel_3d, mode='constant', cval=0.0)

print("3D array:\n", array_3d)
print("Kernel:\n", kernel_3d)
print("Convolved 3D array:\n", convolved_3d)

The result shows the original 3D array, kernel, and convolved 3D array −

3D array:
[[[0.46186776 0.09130699 0.36913034 0.51669149]
  [0.90316515 0.38362845 0.90886156 0.60454144]
  [0.80756784 0.28656032 0.73140925 0.75789388]
  [0.36958966 0.66157156 0.19902489 0.89519004]]

 [[0.04953332 0.98571523 0.80654445 0.47526839]
  [0.67375222 0.31837149 0.20836025 0.10996474]
  [0.48799518 0.34754979 0.85689208 0.21079349]
  [0.91936308 0.79818294 0.18737238 0.01728286]]

 [[0.50793178 0.93691426 0.00515023 0.29870646]
  [0.22871996 0.3098202  0.0396516  0.98755326]
  [0.3347781  0.56108282 0.89520242 0.77143481]
  [0.64504437 0.0133608  0.61686021 0.01443242]]

 [[0.96126985 0.11224998 0.79332687 0.0438432 ]
  [0.39348891 0.36066344 0.06157876 0.29697117]
  [0.40409768 0.88212056 0.22872878 0.7545221 ]
  [0.09578231 0.06486727 0.94749091 0.79605238]]]
Kernel:
[[[0.03703704 0.03703704 0.03703704]
  [0.03703704 0.03703704 0.03703704]
  [0.03703704 0.03703704 0.03703704]]

 [[0.03703704 0.03703704 0.03703704]
  [0.03703704 0.03703704 0.03703704]
  [0.03703704 0.03703704 0.03703704]]

 [[0.03703704 0.03703704 0.03703704]
  [0.03703704 0.03703704 0.03703704]
  [0.03703704 0.03703704 0.03703704]]]
Convolved 3D array:
[[[0.14323484 0.22815693 0.21401425 0.14812454]
  [0.21470421 0.35845228 0.3322031  0.24282783]
  [0.25767769 0.37219326 0.3142019  0.21065136]
  [0.17327335 0.24641033 0.22036013 0.14280959]]

 [[0.21669359 0.30327501 0.30948818 0.19742312]
  [0.32134299 0.49990604 0.51018517 0.35385371]
  [0.33518903 0.5071755  0.47010555 0.3338045 ]
  [0.23083876 0.35997806 0.32674433 0.2279181 ]]

 [[0.21623817 0.28714973 0.26483904 0.15284887]
  [0.32800203 0.47227742 0.46885114 0.29053678]
  [0.29033486 0.44004365 0.43174681 0.29633869]
  [0.20571203 0.34395451 0.33200848 0.23322462]]

 [[0.14115031 0.17447281 0.15727516 0.0935845 ]
  [0.22196806 0.29691764 0.30887114 0.19172851]
  [0.15903061 0.26234589 0.31860718 0.23742514]
  [0.11115311 0.21071912 0.2424502  0.18610089]]]

Applications of Convolution

Convolution has a wide range of applications, such as −

  • Image processing: Blurring, sharpening, edge detection, and feature extraction.
  • Signal processing: Filtering, smoothing, and noise reduction in audio and communication signals.
  • Machine learning: Convolutional neural networks (CNNs) use convolutional layers for feature extraction in tasks such as image recognition and natural language processing.
  • Time series analysis: Convolution can be used to smooth or filter time series data.
Advertisements