Python Scipy Convolve 2d

In this Python Scipy tutorial, we will learn about the “Python Scipy Convolve 2d” to combine two-dimensional arrays into one, the process is called convolution, and also we will deal with the edges or boundaries of the input array by covering the following topics.

  • What is convolution?
  • Python Scipy Convolve 2d
  • How to apply the gaussian filter on the convolved data
  • How to deal with boundaries of the input arrays in convolving
  • Python Scipy Convolve 2d Symm
  • How to pad the input arrays while convolution
  • Python Scipy Convolve 2d Wrap

What is convolution?

Convolution is a straightforward mathematical operation that forms the basis for several popular image-processing techniques. By “multiplying together” two arrays of numbers, typically of different sizes but of the same dimensionality, convolution offers a method for creating a third array of numbers, also of the same dimensionality.

Using this, image processing operators can be implemented whose output pixel values are straightforward linear combinations of certain input pixel values.

Normally, one of the input arrays in an image processing context only contains a grayscale image. The kernel is a second array that is typically much smaller and two-dimensional (but it may just be one pixel thick).

The convolution is carried out by moving the kernel through all of the points where the kernel completely fits within the bounds of the picture, often starting at the top left corner.

A single output pixel is associated with each kernel position, and its value is calculated by multiplying the kernel value by the pixel value of the underlying image of each of the cells in the kernel, and thereafter adding all these numbers together.

Now we know the importance of convolution in images, one can also convolve two signals into one signal.

Read Scipy Convolve

Python Scipy Convolve 2d

The Python Scipy has a method convolve2d() in a module scipy.signal that take two-dimensional arrays and convolve them into one array.

Combine in1 and in2 while letting the output size and boundary conditions be set by the mode, boundary, and fillvalue.

The syntax is given below.

scipy.signal.convolve2d(in1, in2, mode='full', boundary='fill', fillvalue=0)

Where parameters are:

  • in1(array_data): It is the first input.
  • in2(array_data): Second input has the same quantity of dimensions as in1.
  • mode(string): A string indicating the output’s size.
  • boundary(string): A flag showing the proper way to deal with borders.
  • fillvalue(scalar): Value to use as padding for input arrays. 0 is the default.
READ:  Python Turtle Speed With Examples

The method convolve2d() returns out(a two-dimensional array made up of a portion of the discrete linear convolution between in1 and in2).

Let’s take an example by following the below steps:

Import the required libraries or methods using the below python code.

from scipy.signal import convolve2d
import numpy as np
from scipy import misc
import matplotlib.pyplot as plt

Make a 2D convolution using a complex Scharr operator to calculate an image’s gradient. If you don’t want to create edges at the edges of the image, use symmetric boundary conditions.

ascent_ = misc.ascent()
scharr_ = np.array([[ -2-3j, 0-10j,  +2 -3j],
                   [-5+0j, 0+ 0j, +5 +0j],
                   [ -2+3j, 0+10j,  +2 +3j]])

grad_ = convolve2d(ascent_, scharr_, boundary='symm', mode='same')
Python Scipy Convolve 2d Example
Python Scipy Convolve 2d Example

Plot the computed gradient using the below code.

fig, (ax_orig, ax_mag, ax_ang) = plt.subplots(3, 1, figsize=(6, 15))
ax_orig.imshow(ascent_, cmap='gray')
ax_orig.set_title('Original Image')
ax_orig.set_axis_off()
ax_mag.imshow(np.absolute(grad_), cmap='gray')
ax_mag.set_title('Gradient Magnitude')
ax_mag.set_axis_off()
ax_ang.imshow(np.angle(grad_), cmap='hsv')
ax_ang.set_title('Gradient Orientation')
ax_ang.set_axis_off()
fig.show()
Python Scipy Convolve 2d
Python Scipy Convolve 2d

This is how to convolve the 2d array into one array using the method covolve2d() of Python Scipy.

Read Scipy Signal

Python Scipy Convolve 2d Gaussian

We are going to use the gaussian filter on the convolved array, so for that, we will use the method gaussian_filter() of Python Scipy. For example, we will take the same example that we have used in the above-subsection “Python Scipy Convolve 2d”.

Import the required libraries or methods using the below python code.

from scipy.signal import convolve2d
import numpy as np
from scipy import misc
import matplotlib.pyplot as plt

Make a 2D convolution using a complex Scharr operator to calculate an image’s gradient. If you don’t want to create edges at the edges of the image, use symmetric boundary conditions.

ascent_ = misc.ascent()
scharr_ = np.array([[ -2-3j, 0-10j,  +2 -3j],
                   [-5+0j, 0+ 0j, +5 +0j],
                   [ -2+3j, 0+10j,  +2 +3j]])

grad_ = convolve2d(ascent_, scharr_, boundary='symm', mode='same')

Now apply the gaussian filter to the data grad_ using the below code.

from scipy.ndimage import gaussian_filter

conv_gauss = gaussian_filter(grad_,sigma = 1)

Again plot the image with gaussian filter applied using the below code.

fig, (ax_orig, ax_mag, ax_ang) = plt.subplots(1, 3, figsize=(10, 20))
ax_orig.imshow(ascent_, cmap='gray')
ax_orig.set_title('Original Image')
ax_orig.set_axis_off()
ax_mag.imshow(np.absolute(conv_gauss), cmap='gray')
ax_mag.set_title('Gradient Magnitude')
ax_mag.set_axis_off()
ax_ang.imshow(np.angle(conv_gauss), cmap='hsv')
ax_ang.set_title('Gradient Orientation')
ax_ang.set_axis_off()
fig.show()
Python Scipy Convolve 2d Gaussian
Python Scipy Convolve 2d Gaussian

From the above output, if we compare both images such as one without applying a gaussian filter on convolved data and the other with a gaussian filter on convolve data, there is a difference.

READ:  Python Tkinter Button – How to use

Read Scipy Integrate

Python Scipy Convolve 2d Boundary

The method convolve2d() of Python Scipy in a module scipy.signal that accepts a parameter boundary that is a flag showing the proper way to deal with borders while convolving the 2d arrays.

Below are the following values accepted by the parameter boundary:

  • fill: Fillvalue with pad input arrays. (default).
  • wrap: Boundaries with a circle.
  • symm: symmetric boundary circumstances.

Let’s see with an example by following the below steps:

Import the required libraries or methods using the below python code.

from scipy.signal import convolve2d
import numpy as np
from scipy import misc
import matplotlib.pyplot as plt

Calculate the gradient of an image using a sophisticated Scharr operator and a 2D convolution. Use symmetric boundary conditions if you don’t want the image to have edges around the edges.

ascent_ = misc.ascent()
scharr_ = np.array([[ -2-3j, 0-10j,  +2 -3j],
                   [-5+0j, 0+ 0j, +5 +0j],
                   [ -2+3j, 0+10j,  +2 +3j]])

grad_ = convolve2d(ascent_, scharr_, boundary='symm', mode='same')
grad_
Python Scipy Convolve 2d Boundary Example
Python Scipy Convolve 2d Boundary Example

Again compute the gradient of an image with wrap boundary using the below code.

grad_b = convolve2d(ascent_, scharr_, boundary='wrap', mode='same')
grad_b
Python Scipy Convolve 2d Boundary
Python Scipy Convolve 2d Boundary

This is how to deal with the boundaries of the arrays or images using the parameter boundary of method convolve2d() of Python Scipy.

Read Scipy Misc

Python Scipy Convolve 2d Symm

We have already learned about the parameter boundary to deal with the boundaries of the arrays while convolving. The parameter accepts a value symm to keeping the boundaries symmetrical.

Let’s take an example by following the below steps:

Import the required libraries or methods using the below python code.

from scipy.signal import convolve2d
import numpy as np

Create a two-dimensional array using the below code.

a1 = np.array([[2,3,4,5],[3,4,5,6]])
a2 = np.array([[3,2,4,6],[4,3,7,6]])

Apply the convolve2d method with the default boundaries value using the below code.

convolve2d(a1, a2)

Again apply the method with boundaries equal to symm using the below code.

convolve2d(a1, a2, boundary = 'symm')
Python Scipy Convolve 2d Symm
Python Scipy Convolve 2d Symm

This is how to use the value symmetrical to handle the boundaries of the arrays while convolving the two arrays in Scipy.

Read Scipy Stats

Python Scipy Convolve 2d Padding

We already know how to deal with the array boundaries when convolving because we learned about the parameter boundary. To pad the input arrays with fillvalue, the parameter accepts a value fill. Also, use the parameter fillvalue while using the boundary equal to fill.

Let’s take an example by following the below steps:

READ:  Python Turtle Clock - How to Draw

Import the required libraries or methods using the below python code.

from scipy.signal import convolve2d
import numpy as np

Create a two-dimensional array using the below code.

a1 = np.array([[2,3,4,5],[3,4,5,6]])
a2 = np.array([[3,2,4,6],[4,3,7,6]])

Apply the convolve2d method with the default boundaries value using the below code.

convolve2d(a1, a2)

Again apply the method with boundaries equal to fill using the below code.

convolve2d(a1, a2, boundary = 'fill', fillvalue = 2)
Python Scipy Convolve 2d Padding
Python Scipy Convolve 2d Padding

This is how to use the value pad to handle the boundaries of the arrays while convolving the two arrays in Python Scipy.

Read Scipy Rotate Image

Python Scipy Convolve 2d Wrap

In order to deal with the boundaries of the arrays during convolving, we have already learned about the parameter boundary. The parameter accepts a value wrap to maintain circular boundaries.

Let’s take an example by following the below steps:

Import the required libraries or methods using the below python code.

from scipy.signal import convolve2d
import numpy as np

Create a two-dimensional array using the below code.

a1 = np.array([[2,3,4,5],[3,4,5,6]])
a2 = np.array([[3,2,4,6],[4,3,7,6]])

Apply the convolve2d method with the default boundaries value using the below code.

convolve2d(a1, a2)

Again apply the method with boundaries equal to wrap using the below code.

convolve2d(a1, a2, boundary = 'wrap')
Python Scipy Convolve 2d Wrap
Python Scipy Convolve 2d Wrap

This is how to use the value wrap to handle the boundaries of the arrays while convolving the two arrays in Python Scipy.

We have learned how to convolve the two-dimensional arrays, and how to fill the boundaries of the input arrays using the values such as fill, wrap, and symm by covering the following topics.

  • What is convolution?
  • Python Scipy Convolve 2d
  • How to apply the gaussian filter on the convolved data
  • How to deal with boundaries of the input arrays in convolving
  • Python Scipy Convolve 2d Symm
  • How to pad the input arrays while convolution
  • Python Scipy Convolve 2d Wrap

You may like the following python scipy tutorials: