-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvolution.py
49 lines (34 loc) · 916 Bytes
/
convolution.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
41
42
43
44
import numpy as np
from PIL import Image
from matplotlib import pyplot as plt
# Load the image
img = Image.open('image.jpg')
plt.imshow(img)
plt.show()
# Ubah image ke grayscale
img = img.convert('L')
# Convert image ke array
img = np.array(img)
def convolve2D(image, kernel):
# Dimensi image dan kernel
i_h, i_w = image.shape
k_h, k_w = kernel.shape
# Perhitungan dimensi matrix
o_h = i_h - k_h + 1
o_w = i_w - k_w + 1
# Initialize the output array
output = np.zeros((o_h, o_w))
# Proses Filter Kernel
for y in range(o_h):
for x in range(o_w):
output[y][x] = np.sum(image[y:y+k_h, x:x+k_w] * kernel)
return output
#Identitas
print("Nama: Lazuardi Raihan")
print("NRP: 5009201006")
# Define the mean kernel
kernel = np.ones((3,3)) / 9
# Apply the convolution
convolved_img = convolve2D(img, kernel)
plt.imshow(convolved_img)
plt.show()