-
Notifications
You must be signed in to change notification settings - Fork 16
/
dgs.py
230 lines (182 loc) · 7.7 KB
/
dgs.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
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
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
171
172
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# encoding: utf-8
"""
pyDGS - a Python framework for wavelet-based digital grain size analysis
pyDGS is an open-source project dedicated to provide a Python framework to
compute estimates of grain size distribution using the continuous wavelet transform method
of Buscombe (2013) from an image of sediment where grains are clearly resolved.
This program implements the algorithm of:
Buscombe, D. (2013)
Transferable Wavelet Method for Grain-Size Distribution from Images of Sediment Surfaces and Thin Sections,
and Other Natural Granular Patterns. Sedimentology 60, 1709-1732
http://dbuscombe-usgs.github.io/docs/Buscombe2013_Sedimentology_sed12049.pdf
Author: Daniel Buscombe
Marda Science, LLC
Flagstaff, AZ
First Revision January 18 2013
For more information visit https://github.com/dbuscombe-usgs/pyDGS
"""
# Written by Dr Daniel Buscombe, Marda Science LLC
#
# MIT License
#
# Copyright (c) 2020-22, Marda Science LLC
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import numpy as np
import sys, os
from imageio import imread
import pywt
from tqdm import tqdm
from skimage.restoration import denoise_wavelet, estimate_sigma
from functools import partial
# rescale_sigma=True required to silence deprecation warnings
_denoise_wavelet = partial(denoise_wavelet, rescale_sigma=True)
import scipy.stats as stats
# =========================================================
def rescale(dat,mn,mx):
"""
rescales an input dat between mn and mx
"""
m = min(dat.flatten())
M = max(dat.flatten())
return (mx-mn)*(dat-m)/(M-m)+mn
##====================================
def standardize(img):
img = np.array(img)
#standardization using adjusted standard deviation
N = np.shape(img)[0] * np.shape(img)[1]
s = np.maximum(np.std(img), 1.0/np.sqrt(N))
m = np.mean(img)
img = (img - m) / s
img = rescale(img, 0, 1)
del m, s, N
return img
# =========================================================
# =========================================================
def dgs(image, resolution=1, maxscale=4, verbose=1, x=-0.5, f=0):
if verbose==1:
print("===========================================")
print("======DIGITAL GRAIN SIZE: WAVELET==========")
print("===========================================")
print("=CALCULATE GRAIN SIZE-DISTRIBUTION FROM AN=")
print("====IMAGE OF SEDIMENT/GRANULAR MATERIAL====")
print("===========================================")
print("======A PROGRAM BY DANIEL BUSCOMBE=========")
print("====MARDASCIENCE, FLAGSTAFF, ARIZONA=======")
print("========REVISION 4.3, NOV 2022===========")
print("===========================================")
# exit program if no input folder given
if not image:
print('An image file is required!!!!!!')
sys.exit(2)
# print given arguments to screen and convert data type where necessary
if image:
print('Input image is '+image)
# #
# if resolution:
# resolution = np.asarray(resolution,float)
# print('Resolution is '+str(resolution))
# if maxscale:
# maxscale = np.asarray(maxscale,int)
# print('Max scale as inverse fraction of data length: '+str(maxscale))
# if x:
# x = np.asarray(x, float)
# print('Empirical constant = '+str(x))
# ======= stage 1 ==========================
# read image
if verbose==1:
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
print('Processing image '+image)
try:
im = imread(image) # read the image straight with imread
im = np.squeeze(im) # squeeze singleton dimensions
if len(np.shape(im))>3:
im = im[:, :, :3] # only keep the first 3 bands
if len(np.shape(im))==3: # if rgb, convert to grey
im = (0.299 * im[:,:,0] + 0.5870*im[:,:,1] + 0.114*im[:,:,2]).astype('uint8')
nx,ny = np.shape(im)
if nx>ny:
im=im.T
im = standardize(im)
except: # IOError:
print('cannot open '+image)
sys.exit(2)
# # ======= stage 2 ==========================
# Denoised image using default parameters of `denoise_wavelet`
# filter=False
if f==1:
sigma_est = estimate_sigma(im, multichannel=False, average_sigmas=True)
region = denoise_wavelet(im, multichannel=False, rescale_sigma=True,
method='VisuShrink', mode='soft', sigma=sigma_est*2)
else:
region = im.copy()
original = rescale(region,0,255)
nx, ny = original.shape
# ======= stage 3 ==========================
# call cwt to get particle size distribution
P = []; M = []
for k in np.linspace(1,nx-1,100):
[cfs, frequencies] = pywt.cwt(original[int(k),:], np.arange(3, np.minimum(nx,ny)/maxscale, 1), 'morl' , .5)
period = 1. / frequencies
power =(abs(cfs)) ** 2
power = np.mean(np.abs(power), axis=1)/(period**2)
P.append(power)
M.append(period[np.argmax(power)])
p = np.mean(np.vstack(P), axis=0)
p = np.array(p/np.sum(p))
scales = np.array(period)#*resolution
srt = np.sqrt(np.sum(p*((scales-np.mean(M))**2)))
# plt.plot(scales, p,'m', lw=2)
p = p+stats.norm.pdf(scales, np.mean(M), srt/2)
p = np.hstack([0,p])
scales = np.hstack([0,scales])
p = p/np.sum(p)
ind = np.where(p>0)
p = p[ind]
scales = scales[ind]
# area-by-number to volume-by-number
r_v = (p*scales**x) / np.sum(p*scales**x) #volume-by-weight proportion
# get real scales by multiplying by resolution (mm/pixel)
scales = np.array(period)*resolution
# ======= stage 5 ==========================
# calc particle size stats
pd = np.interp([.05,.1,.16,.25,.3,.5,.75,.84,.9,.95],np.hstack((0,np.cumsum(r_v))), np.hstack((0,scales)) )
if verbose==1:
print("d50 = "+str(pd[5]))
mnsz = np.sum(r_v*scales)
if verbose==1:
print("mean size = "+str(mnsz))
srt = np.sqrt(np.sum(r_v*((scales-mnsz)**2)))
if verbose==1:
print("stdev = "+str(srt))
sk = (sum(r_v*((scales-mnsz)**3)))/(100*srt**3)
if verbose==1:
print("skewness = "+str(sk))
kurt = (sum(r_v*((scales-mnsz)**4)))/(100*srt**4)
if verbose==1:
print("kurtosis = "+str(kurt))
# plt.plot(scales, r_v,'k', lw=2); plt.show()
# ======= stage 6 ==========================
# return a dict object of stats
return {'mean grain size': mnsz, 'grain size sorting': srt, 'grain size skewness': sk, 'grain size kurtosis': kurt, 'percentiles': [.05,.1,.16,.25,.3,.5,.75,.84,.9,.95], 'percentile_values': pd, 'grain size frequencies': r_v, 'grain size bins': scales}
# =========================================================
# =========================================================
if __name__ == '__main__':
dgs(image, resolution=1, maxscale=8, verbose=0, x=-1, f=False)