-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrewitt_5x5.py
86 lines (77 loc) · 5.01 KB
/
Prewitt_5x5.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
#sobel algorithm
# Open the image
# Sobel Operator
class Prewitt_5x5:
def __init__(self, image_path):
self.img = np.array(Image.open(image_path)).astype(np.uint8)
def prewitt_5x5(self):
gray_img = np.round(0.299 * self.img[:, :, 0] +
0.587 * self.img[:, :, 1] +
0.114 * self.img[:, :, 2]).astype(np.uint8)
h, w = gray_img.shape
horizontal = np.array([[-2, -1, 0, 1, 2], [-2, -1, 0, 1, 2], [-2, -1, 0, 1, 2], [-2, -1, 0, 1, 2],[-2, -1, 0, 1, 2]])
vertical = np.array([[-2, -2, -2, -2, -2],[-1, -1, -1, -1, -1], [0, 0, 0, 0, 0], [1, 1, 1, 1, 1], [2, 2, 2, 2, 2]])
newhorizontalImage = np.zeros((h, w))
newverticalImage = np.zeros((h, w))
newgradientImage = np.zeros((h, w))
for i in range(1, h - 2):
for j in range(1, w - 2):
horizontalGrad = (horizontal[0, 0] * gray_img[i - 2, j - 2]) + \
(horizontal[0, 1] * gray_img[i - 2, j - 1]) + \
(horizontal[0, 2] * gray_img[i - 2, j]) + \
(horizontal[0, 3] * gray_img[i - 2, j + 1]) + \
(horizontal[0, 4] * gray_img[i - 2, j + 2]) + \
(horizontal[1, 0] * gray_img[i - 1, j - 2]) + \
(horizontal[1, 1] * gray_img[i - 1, j + 1]) + \
(horizontal[1, 2] * gray_img[i - 1, j]) + \
(horizontal[1, 3] * gray_img[i - 1, j + 1]) + \
(horizontal[1, 4] * gray_img[i - 1, j + 2]) + \
(horizontal[2, 0] * gray_img[i, j - 2]) + \
(horizontal[2, 1] * gray_img[i, j - 1]) + \
(horizontal[2, 2] * gray_img[i, j]) + \
(horizontal[2, 3] * gray_img[i, j + 1]) + \
(horizontal[2, 4] * gray_img[i, j + 2]) + \
(horizontal[3, 0] * gray_img[i + 1, j - 2]) + \
(horizontal[3, 1] * gray_img[i + 1, j - 1]) + \
(horizontal[3, 2] * gray_img[i + 1, j]) + \
(horizontal[3, 3] * gray_img[i + 1, j + 1]) + \
(horizontal[3, 4] * gray_img[i + 1, j + 2]) + \
(horizontal[4, 0] * gray_img[i + 2, j - 2]) + \
(horizontal[4, 1] * gray_img[i + 2, j - 1]) + \
(horizontal[4, 2] * gray_img[i + 2, j]) + \
(horizontal[4, 3] * gray_img[i + 2, j + 1]) + \
(horizontal[4, 4] * gray_img[i + 2, j + 2])
newhorizontalImage[i, j] = abs(horizontalGrad)
verticalGrad = (vertical[0, 0] * gray_img[i - 2, j - 2]) + \
(vertical[0, 1] * gray_img[i - 2, j - 1]) + \
(vertical[0, 2] * gray_img[i - 2, j]) + \
(vertical[0, 3] * gray_img[i - 2, j + 1]) + \
(vertical[0, 4] * gray_img[i - 2, j + 2]) + \
(vertical[1, 0] * gray_img[i - 1, j - 2]) + \
(vertical[1, 1] * gray_img[i - 1, j + 1]) + \
(vertical[1, 2] * gray_img[i - 1, j]) + \
(vertical[1, 3] * gray_img[i - 1, j + 1]) + \
(vertical[1, 4] * gray_img[i - 1, j + 2]) + \
(vertical[2, 0] * gray_img[i, j - 2]) + \
(vertical[2, 1] * gray_img[i, j - 1]) + \
(vertical[2, 2] * gray_img[i, j]) + \
(vertical[2, 3] * gray_img[i, j + 1]) + \
(vertical[2, 4] * gray_img[i, j + 2]) + \
(vertical[3, 0] * gray_img[i + 1, j - 2]) + \
(vertical[3, 1] * gray_img[i + 1, j - 1]) + \
(vertical[3, 2] * gray_img[i + 1, j]) + \
(vertical[3, 3] * gray_img[i + 1, j + 1]) + \
(vertical[3, 4] * gray_img[i + 1, j + 2]) + \
(vertical[4, 0] * gray_img[i + 2, j - 2]) + \
(vertical[4, 1] * gray_img[i + 2, j - 1]) + \
(vertical[4, 2] * gray_img[i + 2, j]) + \
(vertical[4, 3] * gray_img[i + 2, j + 1]) + \
(vertical[4, 4] * gray_img[i + 2, j + 2])
newverticalImage[i, j] = abs(verticalGrad)
# G=sqrt(Gx^2+Gy^2)
mag = np.sqrt(pow(horizontalGrad, 2.0) + pow(verticalGrad, 2.0))
newgradientImage[i, j] = mag
return newgradientImage