Skip to content

Commit 00557cf

Browse files
author
Nic Bricknell
committed
mxSampleDetect: Minor readability improvement using a decorator
1 parent f63e1cb commit 00557cf

File tree

1 file changed

+38
-31
lines changed

1 file changed

+38
-31
lines changed

adPythonApp/scripts/adPythonMxSampleDetect.py

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,94 @@
11
#!/usr/bin/env dls-python
22
from adPythonPlugin import AdPythonPlugin
33

4+
from inspect import getargspec
45
from itertools import chain
56
import numpy as np
67
import cv2
78

89

9-
# Use thin wrapper functions around cv2 operations because we want a unified
10-
# interface (fn(arr, params)) to more easily expose the functions to the user.
10+
# Unify the interface to morphological operations using a decorator such that
11+
# all operations are called as `fn(arr, (param1, param2, ...))`. This makes it
12+
# easier to expose functions with different numbers of arguments to the end
13+
# user in a generic way.
1114

12-
def erode(arr, params):
13-
ksize, iterations = params[:2]
15+
def unified_interface(function):
16+
def wrapper(arr, params):
17+
n_params = len(getargspec(function).args) - 1 # (arr is not a param.)
18+
required_params = params[:n_params] # Throw away unwanted params.
19+
return function(arr, *required_params)
20+
return wrapper
21+
22+
23+
@unified_interface
24+
def erode(arr, ksize, iterations):
1425
element = cv2.getStructuringElement(cv2.MORPH_RECT, (ksize, ksize))
1526
return cv2.erode(arr, element, iterations=iterations)
1627

1728

18-
def dilate(arr, params):
19-
ksize, iterations = params[:2]
29+
@unified_interface
30+
def dilate(arr, ksize, iterations):
2031
element = cv2.getStructuringElement(cv2.MORPH_RECT, (ksize, ksize))
2132
return cv2.dilate(arr, element, iterations=iterations)
2233

2334

2435
# `_morph` suffix to avoid name collision.
25-
def open_morph(arr, params):
26-
ksize, iterations = params[:2]
36+
@unified_interface
37+
def open_morph(arr, ksize, iterations):
2738
element = cv2.getStructuringElement(cv2.MORPH_RECT, (ksize, ksize))
2839
return cv2.morphologyEx(
2940
arr, cv2.MORPH_OPEN, element, iterations=iterations)
3041

3142

32-
def close(arr, params):
33-
ksize, iterations = params[:2]
43+
@unified_interface
44+
def close(arr, ksize, iterations):
3445
element = cv2.getStructuringElement(cv2.MORPH_RECT, (ksize, ksize))
3546
return cv2.morphologyEx(
3647
arr, cv2.MORPH_CLOSE, element, iterations=iterations)
3748

3849

39-
def gradient(arr, params):
40-
ksize, iterations = params[:2]
50+
@unified_interface
51+
def gradient(arr, ksize, iterations):
4152
element = cv2.getStructuringElement(cv2.MORPH_RECT, (ksize, ksize))
4253
return cv2.morphologyEx(
4354
arr, cv2.MORPH_GRADIENT, element, iterations=iterations)
4455

4556

46-
def top_hat(arr, params):
47-
ksize, iterations = params[:2]
57+
@unified_interface
58+
def top_hat(arr, ksize, iterations):
4859
element = cv2.getStructuringElement(cv2.MORPH_RECT, (ksize, ksize))
4960
return cv2.morphologyEx(
5061
arr, cv2.MORPH_TOPHAT, element, iterations=iterations)
5162

5263

53-
def black_hat(arr, params):
54-
ksize, iterations = params[:2]
64+
@unified_interface
65+
def black_hat(arr, ksize, iterations):
5566
element = cv2.getStructuringElement(cv2.MORPH_RECT, (ksize, ksize))
5667
return cv2.morphologyEx(
5768
arr, cv2.MORPH_BLACKHAT, element, iterations=iterations)
5869

5970

60-
def blur(arr, params):
61-
# The comma is necessary. (It unpacks the tuple.)
62-
ksize, = params[:1]
71+
@unified_interface
72+
def blur(arr, ksize):
6373
return cv2.blur(arr, (ksize, ksize))
6474

6575

66-
def gaussian_blur(arr, params):
67-
ksize, = params[:1]
76+
@unified_interface
77+
def gaussian_blur(arr, ksize):
6878
# Kernel size should be odd.
69-
if not ksize % 2:
70-
ksize += 1
79+
if not ksize % 2: ksize += 1
7180
return cv2.GaussianBlur(arr, (ksize, ksize), 0)
7281

7382

74-
def median_blur(arr, params):
75-
ksize, = params[:1]
76-
if not ksize % 2:
77-
ksize += 1
83+
@unified_interface
84+
def median_blur(arr, ksize):
85+
if not ksize % 2: ksize += 1
7886
return cv2.medianBlur(arr, ksize)
7987

8088

81-
def canny_edge_detect(arr, params):
82-
upper_threshold, lower_threshold = params[:2]
83-
84-
# Upper and lower threshold arguments commute.
89+
@unified_interface
90+
def canny_edge_detect(arr, upper_threshold, lower_threshold):
91+
# (Upper and lower threshold arguments commute.)
8592
return cv2.Canny(arr, upper_threshold, lower_threshold)
8693

8794

0 commit comments

Comments
 (0)