Skip to content

Commit 208465d

Browse files
committed
Adding medfilt1 and medfilt2 and necessary tests
1 parent fe18684 commit 208465d

File tree

3 files changed

+96
-32
lines changed

3 files changed

+96
-32
lines changed

arrayfire/image.py

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from .library import *
1515
from .array import *
1616
from .data import constant
17+
from .signal import medfilt
1718
import os
1819

1920
def gradient(image):
@@ -619,38 +620,6 @@ def mean_shift(image, s_sigma, c_sigma, n_iter, is_color = False):
619620
ct.c_uint(n_iter), is_color))
620621
return output
621622

622-
def medfilt(image, w0 = 3, w1 = 3, edge_pad = PAD.ZERO):
623-
"""
624-
Apply median filter for the image.
625-
626-
Parameters
627-
----------
628-
image : af.Array
629-
- A 2 D arrayfire array representing an image, or
630-
- A multi dimensional array representing batch of images.
631-
632-
w0 : optional: int. default: 3.
633-
- The length of the filter along the first dimension.
634-
635-
w1 : optional: int. default: 3.
636-
- The length of the filter along the second dimension.
637-
638-
edge_pad : optional: af.PAD. default: af.PAD.ZERO
639-
- Flag specifying how the median at the edge should be treated.
640-
641-
Returns
642-
---------
643-
644-
output : af.Array
645-
- The image after median filter is applied.
646-
647-
"""
648-
output = Array()
649-
safe_call(backend.get().af_medfilt(ct.pointer(output.arr),
650-
image.arr, c_dim_t(w0),
651-
c_dim_t(w1), edge_pad.value))
652-
return output
653-
654623
def minfilt(image, w_len = 3, w_wid = 3, edge_pad = PAD.ZERO):
655624
"""
656625
Apply min filter for the image.

arrayfire/signal.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1265,6 +1265,97 @@ def iir(B, A, X):
12651265
safe_call(backend.get().af_iir(ct.pointer(Y.arr), B.arr, A.arr, X.arr))
12661266
return Y
12671267

1268+
def medfilt(signal, w0 = 3, w1 = 3, edge_pad = PAD.ZERO):
1269+
"""
1270+
Apply median filter for the signal.
1271+
1272+
Parameters
1273+
----------
1274+
signal : af.Array
1275+
- A 2 D arrayfire array representing a signal, or
1276+
- A multi dimensional array representing batch of signals.
1277+
1278+
w0 : optional: int. default: 3.
1279+
- The length of the filter along the first dimension.
1280+
1281+
w1 : optional: int. default: 3.
1282+
- The length of the filter along the second dimension.
1283+
1284+
edge_pad : optional: af.PAD. default: af.PAD.ZERO
1285+
- Flag specifying how the median at the edge should be treated.
1286+
1287+
Returns
1288+
---------
1289+
1290+
output : af.Array
1291+
- The signal after median filter is applied.
1292+
1293+
"""
1294+
output = Array()
1295+
safe_call(backend.get().af_medfilt(ct.pointer(output.arr),
1296+
signal.arr, c_dim_t(w0),
1297+
c_dim_t(w1), edge_pad.value))
1298+
return output
1299+
1300+
def medfilt1(signal, length = 3, edge_pad = PAD.ZERO):
1301+
"""
1302+
Apply median filter for the signal.
1303+
1304+
Parameters
1305+
----------
1306+
signal : af.Array
1307+
- A 1 D arrayfire array representing a signal, or
1308+
- A multi dimensional array representing batch of signals.
1309+
1310+
length : optional: int. default: 3.
1311+
- The length of the filter.
1312+
1313+
edge_pad : optional: af.PAD. default: af.PAD.ZERO
1314+
- Flag specifying how the median at the edge should be treated.
1315+
1316+
Returns
1317+
---------
1318+
1319+
output : af.Array
1320+
- The signal after median filter is applied.
1321+
1322+
"""
1323+
output = Array()
1324+
safe_call(backend.get().af_medfilt1(ct.pointer(output.arr), signal.arr, c_dim_t(length), edge_pad.value))
1325+
return output
1326+
1327+
def medfilt2(signal, w0 = 3, w1 = 3, edge_pad = PAD.ZERO):
1328+
"""
1329+
Apply median filter for the signal.
1330+
1331+
Parameters
1332+
----------
1333+
signal : af.Array
1334+
- A 2 D arrayfire array representing a signal, or
1335+
- A multi dimensional array representing batch of signals.
1336+
1337+
w0 : optional: int. default: 3.
1338+
- The length of the filter along the first dimension.
1339+
1340+
w1 : optional: int. default: 3.
1341+
- The length of the filter along the second dimension.
1342+
1343+
edge_pad : optional: af.PAD. default: af.PAD.ZERO
1344+
- Flag specifying how the median at the edge should be treated.
1345+
1346+
Returns
1347+
---------
1348+
1349+
output : af.Array
1350+
- The signal after median filter is applied.
1351+
1352+
"""
1353+
output = Array()
1354+
safe_call(backend.get().af_medfilt2(ct.pointer(output.arr),
1355+
signal.arr, c_dim_t(w0),
1356+
c_dim_t(w1), edge_pad.value))
1357+
return output
1358+
12681359
def set_fft_plan_cache_size(cache_size):
12691360
"""
12701361
Sets plan cache size.

arrayfire/tests/simple/signal.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,8 @@ def simple_signal(verbose=False):
110110
display_func(af.fir(b, x))
111111
display_func(af.iir(b, a, x))
112112

113+
display_func(af.medfilt1(a))
114+
display_func(af.medfilt2(a))
115+
display_func(af.medfilt(a))
116+
113117
_util.tests['signal'] = simple_signal

0 commit comments

Comments
 (0)