/
filters.py
125 lines (90 loc) · 3.75 KB
/
filters.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
"""The openpiv.filters module contains some filtering/smoothing routines."""
__licence_ = """
Copyright (C) 2011 www.openpiv.net
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
from openpiv.lib import replace_nans
import numpy as np
from scipy.signal import convolve
def _gaussian_kernel( size ):
"""A normalized 2D Gaussian kernel array
Parameters
----------
size : int
the half width of the kernel. Kernel
has shape 2*size+1
Examples
--------
>>> from openpiv.filters import _gaussian_kernel
>>> _gaussian_kernel(1)
array([[ 0.04491922, 0.12210311, 0.04491922],
[ 0.12210311, 0.33191066, 0.12210311],
[ 0.04491922, 0.12210311, 0.04491922]])
"""
size = int(size)
x, y = np.mgrid[-size:size+1, -size:size+1]
g = np.exp(-(x**2/float(size)+y**2/float(size)))
return g / g.sum()
def gaussian( u, v, size) :
"""Smooths the velocity field with a Gaussian kernel.
Parameters
----------
u : 2d np.ndarray
the u velocity component field
v : 2d np.ndarray
the v velocity component field
size : int
the half width of the kernel. Kernel
has shape 2*size+1
Returns
-------
uf : 2d np.ndarray
the smoothed u velocity component field
vf : 2d np.ndarray
the smoothed v velocity component field
"""
g = _gaussian_kernel( size=size )
uf = convolve( u, g, mode='same')
vf = convolve( v, g, mode='same')
return uf, vf
def replace_outliers( u, v, method='localmean', max_iter=5, tol=1e-3, kernel_size=1):
"""Replace invalid vectors in an velocity field using an iterative image inpainting algorithm.
The algorithm is the following:
1) For each element in the arrays of the ``u`` and ``v`` components, replace it by a weighted average
of the neighbouring elements which are not invalid themselves. The weights depends
of the method type. If ``method=localmean`` weight are equal to 1/( (2*kernel_size+1)**2 -1 )
2) Several iterations are needed if there are adjacent invalid elements.
If this is the case, inforation is "spread" from the edges of the missing
regions iteratively, until the variation is below a certain threshold.
Parameters
----------
u : 2d np.ndarray
the u velocity component field
v : 2d np.ndarray
the v velocity component field
max_iter : int
the number of iterations
fil
kernel_size : int
the size of the kernel, default is 1
method : str
the type of kernel used for repairing missing vectors
Returns
-------
uf : 2d np.ndarray
the smoothed u velocity component field, where invalid vectors have been replaced
vf : 2d np.ndarray
the smoothed v velocity component field, where invalid vectors have been replaced
"""
u = replace_nans( u, method=method, max_iter=max_iter, tol=tol, kernel_size=kernel_size )
v = replace_nans( v, method=method, max_iter=max_iter, tol=tol, kernel_size=kernel_size )
return u, v