forked from e-koch/ewky_scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ndimage_to_opencv_tests.py
45 lines (30 loc) · 941 Bytes
/
ndimage_to_opencv_tests.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
'''
OpenCV is nice and optimized. ndimage is a little less so.
I just want to test if the outputs match, and the data types needed for that.
'''
import cv2
from scipy import ndimage as nd
import numpy as np
from skimage.morphology import disk
def tophat_test(plot=True):
arr = np.ones((100, 100), dtype="float64")
yy, xx = np.mgrid[-50:50, -50:50]
arr[xx**2 + yy**2 <= 30**2] = 0.0
struct = disk(32).astype("uint8")
bth_nd = nd.black_tophat(arr, structure=struct)
bth_cv = cv2.morphologyEx(arr, cv2.MORPH_BLACKHAT, struct)
if plot:
import matplotlib.pyplot as p
p.subplot(131)
p.imshow(bth_nd)
p.colorbar()
p.subplot(132)
p.imshow(bth_cv)
p.colorbar()
p.subplot(133)
p.imshow(np.abs(bth_nd-bth_cv))
p.colorbar()
p.show()
np.testing.assert_equal(bth_nd, bth_cv)
if __name__ == "__main__":
tophat_test()