-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path22.Granulometry mathametical morphology.py
More file actions
73 lines (63 loc) · 2.01 KB
/
22.Granulometry mathametical morphology.py
File metadata and controls
73 lines (63 loc) · 2.01 KB
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
import numpy as np
from scipy import ndimage
import matplotlib.pyplot as plt
def disk_structure(n):
# n = [2 6 10 14 18]
'''
struct 5 matrixes:
[5*5],[13*13],[21*21],[29*29],[37*37]
'''
struct = np.zeros((2 * n + 1, 2 * n + 1))
# print(struct)
# get 5 matrix or same as struct but np.indices provide number in earch x and y from
# like as x = [0:] and y = [0:]
x, y = np.indices((2 * n + 1, 2 * n + 1))
# print(y)
# exit()
mask = (x - n)**2 + (y - n)**2 <= n**2
struct[mask] = 1
return struct.astype(np.bool)
# granulometry recived 2 arg
def granulometry(data, sizes=None):
# s have 256*256 sahpe
s = max(data.shape)
# newsize = sizes
# print(newsize)
# in case sizes have no value then genereate a sizes value by this if
if sizes is None:
sizes = range(1, s/2, 2)
# \ use for line breack
'''
granulo one by one get these numbers
0:23849 = 2
1:23287 = 6
2:14171 = 10
3:4834 = 14
4:0 =18
'''
granulo = [ndimage.binary_opening(data, \
structure=disk_structure(n)).sum() for n in sizes]
return granulo
np.random.seed(1)
n = 10
l = 256
im = np.zeros((l, l))
points = l*np.random.random((2, n**2))
im[(points[0]).astype(np.int), (points[1]).astype(np.int)] = 1
im = ndimage.gaussian_filter(im, sigma=l/(4.*n))
mask = im > im.mean()
# in range of 2 to 19 get 5 numbers from 0 to 4 = [2 6 10 14 18]
granulo = granulometry(mask, sizes=np.arange(2, 19, 4))
# exit()
plt.figure(figsize=(6, 2.2))
plt.subplot(121)
plt.imshow(mask, cmap=plt.cm.gray)
opened = ndimage.binary_opening(mask, structure=disk_structure(10))
opened_more = ndimage.binary_opening(mask, structure=disk_structure(14))
plt.contour(opened, [0.5], colors='b', linewidths=2)
plt.contour(opened_more, [0.5], colors='r', linewidths=2)
plt.axis('off')
plt.subplot(122)
plt.plot(np.arange(2, 19, 4), granulo, 'ok', ms=8)
plt.subplots_adjust(wspace=0.02, hspace=0.15, top=0.95, bottom=0.15, left=0, right=0.95)
plt.show()