Skip to content

filters

filters

adjust_gamma(image, gamma=1.0)

Adjusts the gamma of an image.

image (numpy.ndarray): Input image. gamma (float, optional): Gamma value. Defaults to 1.0.

numpy.ndarray: Gamma-adjusted image.

Source code in redesign_pipeline/signal_processing/filters.py
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
def adjust_gamma(image, gamma=1.0):
    '''
    Adjusts the gamma of an image.

    Parameters:
    image (numpy.ndarray): Input image.
    gamma (float, optional): Gamma value. Defaults to 1.0.

    Returns:
    numpy.ndarray: Gamma-adjusted image.

    '''
    invGamma = 1.0 / gamma
    table = np.array([((i / 255.0) ** invGamma) * 255
        for i in np.arange(0, 256)]).astype("uint8")

    return cv2.LUT(image, table)

butter_bandpass(lowcut, highcut, fs, order=5)

Butterworth band-pass filter

Parameters

lowcut: float, the lower cutoff frequency in Hz highcut: float, the upper cutoff frequency in Hz fs: float, the sample rate in Hz order: int (optional), the order of the filter. The default is 5.

Returns

b, a: ndarray, the filter coefficients for the Butterworth band-pass filter

Source code in redesign_pipeline/signal_processing/filters.py
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
def butter_bandpass(lowcut, highcut, fs, order=5):
    """
    Butterworth band-pass filter

    Parameters
    ----------

    lowcut: float, the lower cutoff frequency in Hz
    highcut: float, the upper cutoff frequency in Hz
    fs: float, the sample rate in Hz
    order: int (optional), the order of the filter. The default is 5.

    Returns
    -------

    b, a: ndarray, the filter coefficients for the Butterworth band-pass filter

    """
    nyq = 0.5 * fs
    low = lowcut / nyq
    high = highcut / nyq
    b, a = signal.butter(order, [low,high], btype='band')
    return b, a

butter_bandpass_filter(src_signal, lowcut, highcut, order, fps)

Butterworth band-pass filter in Python. The filter is designed to pass a band of frequencies between the lower and upper cutoff frequencies, while rejecting frequencies outside of the band.

Parameters

src_signal: ndarray, the source signal to be filtered fps: float, the sample rate in Hz of the source signal. May need to be changed.

Returns

y: ndarray, the filtered signal

Source code in redesign_pipeline/signal_processing/filters.py
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
def butter_bandpass_filter(src_signal, lowcut, highcut, order, fps):
    """
    Butterworth band-pass filter in Python. The filter is designed to pass a band of frequencies between the lower and upper cutoff frequencies, while rejecting frequencies outside of the band.

    Parameters
    ----------
    src_signal: ndarray, the source signal to be filtered
    fps: float, the sample rate in Hz of the source signal. May need to be changed.

    Returns
    -------

    y: ndarray, the filtered signal

    """

    # lowcut = 0.7
    # highcut = 4
    # order = 5

    # lowcut = 0.15
    # highcut = 0.4
    # order = 5

    b, a = butter_bandpass(lowcut, highcut, fps, order=order)

    if isinstance(src_signal,dict):
        filtered = {}
        for k in src_signal:
            filtered[k] = signal.lfilter(b, a, src_signal[k]) 
        return filtered

    elif len(src_signal.shape) == 1:
        filtered = signal.lfilter(b, a, src_signal)

        return filtered

    elif len(src_signal.shape) == 2:
        filtered = np.zeros_like(src_signal)
        for i in range(src_signal.shape[0]):
            filtered[i] = signal.lfilter(b, a, src_signal[i])

        return filtered

    elif len(src_signal.shape) == 3:
        lm,ch,rows = src_signal.shape
        signals = src_signal.reshape(lm*ch,rows)
        filtered = np.zeros_like(signals)

        for i in range(signals.shape[0]):
            filtered[i] = signal.lfilter(b, a, signals[i])

        return filtered.reshape(lm,ch,rows)

moving_average(src_signal, window_size, padding=True)

Calculates the moving average of a signal.

src_signal (ndarray): Source signal. window_size (int): Size of the moving window. padding (bool, optional): Flag to indicate whether padding should be applied. Defaults to True.

ndarray: Moving average of the signal.

Source code in redesign_pipeline/signal_processing/filters.py
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
def moving_average(src_signal, window_size, padding=True):
    '''
    Calculates the moving average of a signal.

    Parameters:
    src_signal (ndarray): Source signal.
    window_size (int): Size of the moving window.
    padding (bool, optional): Flag to indicate whether padding should be applied. Defaults to True.

    Returns:
    ndarray: Moving average of the signal.

    '''

    if src_signal.ndim == 3:
        lm,ch,rows = src_signal.shape
    elif src_signal.ndim == 2:
        ch,rows = src_signal.shape
        lm = 1

    signals = src_signal.reshape(lm*ch,rows)

    if not padding:
        rows = rows - window_size + 1


    averaged_signals = np.zeros((lm*ch,rows))
    window = np.ones(window_size, dtype=src_signal.dtype) / window_size
    for i in range(signals.shape[0]):
        averaged_signals[i] = np.convolve(np.pad(signals[i],(window_size-1,0),'constant', constant_values=(0,)) if padding else signals[i], window, mode='valid')

    if src_signal.ndim == 3:
        return averaged_signals.reshape(lm,ch,rows)
    elif src_signal.ndim == 2:
        return averaged_signals.reshape(ch,rows)

moving_average_bvp(src_signal, window_size, padding=True)

Calculates the moving average of BVP (Blood Volume Pulse) signal data for different methods.

src_signal (dict): Dictionary containing BVP signal data for different methods. window_size (int): Size of the moving window. padding (bool, optional): Flag to indicate whether padding should be applied. Defaults to True.

dict: Dictionary containing moving average of BVP signal data for each method.

Source code in redesign_pipeline/signal_processing/filters.py
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
def moving_average_bvp(src_signal, window_size, padding=True):
    '''
    Calculates the moving average of BVP (Blood Volume Pulse) signal data for different methods.

    Parameters:
    src_signal (dict): Dictionary containing BVP signal data for different methods.
    window_size (int): Size of the moving window.
    padding (bool, optional): Flag to indicate whether padding should be applied. Defaults to True.

    Returns:
    dict: Dictionary containing moving average of BVP signal data for each method.

    '''

    data_for_method = {}

    for k,data_rois in src_signal.items():

        data_for_method[k] = moving_average(data_rois, window_size, padding)

    return data_for_method

scale_rgb(src_signal)

Normalizes an RGB image signal. The purpose of normalizing an image signal is to bring its values within a specified range.

src_signal (numpy.ndarray): Input RGB image signal. This is a numpy array with shape (height, width, 3) where height and width are the dimensions of the image and 3 represents the RGB channels.

numpy.ndarray: Normalized RGB image signal.This is a numpy array with the same shape as the input signal, where all values are divided by 255.

Source code in redesign_pipeline/signal_processing/filters.py
79
80
81
82
83
84
85
86
87
88
89
90
91
92
def scale_rgb(src_signal):
    '''
    Normalizes an RGB image signal. The purpose of normalizing an image signal is to bring its values within a specified range.

    Parameters:
    src_signal (numpy.ndarray): Input RGB image signal. This is a numpy array with shape (height, width, 3) where height and width are the dimensions of the image and 3 represents the RGB channels.

    Returns:
    numpy.ndarray: Normalized RGB image signal.This is a numpy array with the same shape as the input signal, where all values are divided by 255.

    '''

    out_signal = src_signal / 255
    return out_signal

standardize(src_signal)

Returns a standardized signal

Parameters

src_signal : signal to standardize

Source code in redesign_pipeline/signal_processing/filters.py
42
43
44
45
46
47
48
49
50
51
52
53
54
55
def standardize(src_signal):
    """
    Returns a standardized signal

    Parameters
    ----------

    src_signal : signal to standardize

    """

    out_signal = (src_signal - src_signal.mean(axis=2)[:,:,None])/src_signal.std(axis=2)[:,:,None]

    return out_signal

standardize_bvp(src_signal)

Standardizes BVP (Blood Volume Pulse) signal data for different methods.

src_signal (dict): Dictionary containing BVP signal data for different methods.

dict: Dictionary containing standardized BVP signal data for each method.

Source code in redesign_pipeline/signal_processing/filters.py
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
def standardize_bvp(src_signal):
    '''
    Standardizes BVP (Blood Volume Pulse) signal data for different methods.

    Parameters:
    src_signal (dict): Dictionary containing BVP signal data for different methods.

    Returns:
    dict: Dictionary containing standardized BVP signal data for each method.

    '''

    data_for_method = {}

    for k,data_rois in src_signal.items():

        data_for_method[k] = (data_rois - data_rois.mean(axis=1)[:,None])/data_rois.std(axis=1)[:,None]

    return data_for_method

threshold_mask(img, RGB_LOW_TH, RGB_HIGH_TH)

Creates a binary mask by thresholding an image.

img (numpy.ndarray): Input image. RGB_LOW_TH (tuple): Lower threshold values for RGB channels. RGB_HIGH_TH (tuple): Upper threshold values for RGB channels.

numpy.ndarray: Binary mask.

Source code in redesign_pipeline/signal_processing/filters.py
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
def threshold_mask(img, RGB_LOW_TH, RGB_HIGH_TH):
    '''
    Creates a binary mask by thresholding an image.

    Parameters:
    img (numpy.ndarray): Input image.
    RGB_LOW_TH (tuple): Lower threshold values for RGB channels.
    RGB_HIGH_TH (tuple): Upper threshold values for RGB channels.

    Returns:
    numpy.ndarray: Binary mask.

    '''
    boolean_mask_arr = (img > RGB_LOW_TH) & (img < RGB_HIGH_TH)
    boolean_mask = (boolean_mask_arr[:,:,0] & boolean_mask_arr[:,:,1] & boolean_mask_arr[:,:,2]).astype(np.uint8)*255
    return boolean_mask