-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path2-convolve_grayscale_padding.py
executable file
·40 lines (37 loc) · 1.6 KB
/
2-convolve_grayscale_padding.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/usr/bin/env python3
"""Function that performs a valid convolution
on grayscale images with custom padding"""
import numpy as np
def convolve_grayscale_padding(images, kernel, padding):
"""Performs a convolution on grayscale images custom padding
Args:
images: `numpy.ndarray` with shape (m, h, w)
containing multiple grayscale images
m: `int`, is the number of images
h: `int`, is the height in pixels of the images
w: `int`, is the width in pixels of the images
kernel: `numpy.ndarray` with shape (kh, kw)
containing the kernel for the convolution
kh: `int`, is the height of the kernel
kw: `int`, is the width of the kernel
padding: `tuple` of (ph, pw)
ph: `int` is the padding for the height of the image
pw: `int` is the padding for the width of the image
Returns:
output: `numpy.ndarray` containing the convolved images
"""
m, h, w = images.shape[0], images.shape[1], images.shape[2]
kh, kw = kernel.shape[0], kernel.shape[1]
ph, pw = padding[0], padding[1]
nw = int(w - kw + (2 * pw) + 1)
nh = int(h - kh + (2 * ph) + 1)
convolved = np.zeros((m, nh, nw))
npad = ((0, 0), (ph, ph), (pw, pw))
imagesp = np.pad(images, pad_width=npad,
mode='constant', constant_values=0)
for i in range(nh):
for j in range(nw):
image = imagesp[:, i:i + kh, j:j + kw]
convolved[:, i, j] = np.sum(np.multiply(image, kernel),
axis=(1, 2))
return convolved