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

in a module *convolve2d()*

that take two-dimensional arrays and convolve them into one array.*scipy.signal*

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.

The method

returns *convolve2d()*.

`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')
```

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()
```

This is how to convolve the 2d array into one array using the method

of Python Scipy.*covolve2d()*

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

of Python Scipy. For example, we will take the same example that we have used in the above-subsection “Python Scipy Convolve 2d”.*gaussian_filter()*

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

using the below code.*grad_*

```
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()
```

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 Scipy Integrate

## Python Scipy Convolve 2d Boundary

The method

of Python Scipy in a module *convolve2d()*

that accepts a parameter *scipy.signal*`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_
```

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
```

This is how to deal with the boundaries of the arrays or images using the parameter `boundary`

of method

of Python Scipy.*convolve2d()*

Read Scipy Misc

## Python Scipy Convolve 2d Symm

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

`boundary`

`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

using the below code.*symm*

`convolve2d(a1, a2, boundary = '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

. To pad the input arrays with fillvalue, the parameter accepts a value *boundary*

. Also, use the parameter *fill*

while using the boundary equal to *fillvalue*

.*fill*

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

using the below code.*fill*

`convolve2d(a1, a2, boundary = 'fill', fillvalue = 2)`

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

. The parameter accepts a value *boundary*

to maintain circular boundaries.*wrap*

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

using the below code.*wrap*

`convolve2d(a1, a2, boundary = '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:

- Scipy Sparse
- Python Scipy Pairwise Distance
- Python Scipy Linalg Svd
- Python Scipy Ndimage Imread Tutorial
- Python Scipy Smoothing

Python is one of the most popular languages in the United States of America. I have been working with Python for a long time and I have expertise in working with various libraries on Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… I have experience in working with various clients in countries like United States, Canada, United Kingdom, Australia, New Zealand, etc. Check out my profile.