forked from salaee/pegbis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter.py
47 lines (36 loc) · 1.16 KB
/
filter.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
import numpy as np
import math
np.seterr(over='ignore')
# some constants
WIDTH = 4.0
# convolve image with gaussian filter
def smooth(src, sigma):
mask = make_fgauss(sigma)
mask = normalize(mask)
tmp = convolve_even(src, mask)
dst = convolve_even(tmp, mask)
return dst
# gaussian filter
def make_fgauss(sigma):
sigma = max(sigma, 0.01)
length = int(math.ceil(sigma * WIDTH)) + 1
mask = np.zeros(shape=length, dtype=float)
for i in range(length):
mask[i] = math.exp(-0.5 * math.pow(i / sigma, i / sigma))
return mask
# normalize mask so it integrates to one
def normalize(mask):
sum = 2 * np.sum(np.absolute(mask)) + abs(mask[0])
return np.divide(mask, sum)
# convolve src with mask. output is flipped!
def convolve_even(src, mask):
output = np.zeros(shape=src.shape, dtype=float)
height, width = src.shape
length = len(mask)
for y in range(height):
for x in range(width):
sum = float(mask[0] * src[y, x])
for i in range(1, length):
sum += mask[i] * (src[y, max(x - i, 0)] + src[y, min(x + i, width - 1)])
output[y, x] = sum
return output