-
Notifications
You must be signed in to change notification settings - Fork 1
/
compute_gradient.py
96 lines (74 loc) · 2.83 KB
/
compute_gradient.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
85
86
87
88
89
90
91
92
93
94
95
import math
import numpy as np
import cv2
def compute_gradient(img):
# Lists for storing prewitt's operator for Gx and Gy for finding gradient at each pixel location
Gx = [[-1,0,1],
[-1,0,1],
[-1,0,1]]
Gy = [[1,1,1],
[0,0,0],
[-1,-1,-1]]
# setting startI and startJ to 4, 4 assuming gaussian filter size of 7 X 7
startI, startJ = 4, 4
# finding height and width of image
height, width = img.shape
# create two lists for storing gradient values of image for X and Y; gradient theta and final gradient of image
imgGX = np.zeros((height, width))
imgGY = np.zeros((height, width))
imgTheta = np.zeros((height, width))
imgOut = np.zeros((height, width))
# create i and j for iterating over the image
i = startI
j = startJ
# loop for iterating over each pixel of the image and computing its X and Y gradient
while i < height - startI:
j = startJ
while j < width - startJ:
# finding gradient for X-axis i.e. Gx and finding the absolute value of gradient
imgGX[i][j] = multiply_matrices(img, Gx, i, j, int(len(Gx) / 2), int(len(Gx) / 2))
if imgGX[i][j] < 0:
imgGX[i][j] = abs(imgGX[i][j])
# finding gradient for y-axis i.e. Gy and finding the absolute value of gradient
imgGY[i][j] = multiply_matrices(img, Gy, i, j, int(len(Gy) / 2), int(len(Gy) / 2))
if imgGY[i][j] < 0:
imgGY[i][j] = abs(imgGY[i][j])
# normalize X and Y gradient
imgGX[i][j] = imgGX[i][j] / 3.0
imgGY[i][j] = imgGY[i][j] / 3.0
#calculating gradient for final image and dividing the gradient by root of 2 to normalize the image to [0,255]
imgOut[i][j] = np.sqrt(np.power(imgGX[i][j], 2) + np.power(imgGY[i][j], 2)) / np.sqrt(2) # compute normalized gradient for whole image
# if X gradient is zero set theta to 90 or -90 depending upon Y gradient
if imgGX[i][j] == 0:
if imgGY[i][j] > 0:
imgTheta[i][j] = 90
else:
imgTheta[i][j] = -90
else:
imgTheta[i][j] = math.degrees(math.atan((imgGY[i][j] / imgGX[i][j])))
# if theta value is less than 0 then we need to round it to positive value to ease up the process of finding non-maxima suppression
if imgTheta[i][j] < 0:
imgTheta[i][j] = imgTheta[i][j] + 360
j = j + 1
i = i + 1
# return gradients of image and theta values of each pixel
return imgGX, imgGY, imgOut, imgTheta
def multiply_matrices(img, gradient, i, j, startI, startJ):
#variables for iterating over gradient matrix
gI, gJ = 0, 0
#taking (i, j) as center of convolution, we shift left and up by half length of gradient
i = i - startI
j = j - startJ
saveJ = j
sum = 0
# loop for multiplying sub-matrix with gaussian matrix over given [i-startI...i+startI, j-startJ...j+startJ] range
while gI < len(gradient):
gJ = 0
j = saveJ
while gJ < len(gradient[0]):
sum = sum + (gradient[gI][gJ] * img[i][j])
j = j + 1
gJ = gJ + 1
gI = gI + 1
i = i + 1
return sum