-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathspect.py
106 lines (89 loc) · 2.8 KB
/
spect.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
import cv2
import numpy as np
import matplotlib
#matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
from scipy.signal import find_peaks
elements={
'Orthorhombic sulphur':[265,285],
'Polymeric sulphur':[360],
'Sulphur Dioxide':[280],
'Iron-sulphur clusters':[450,620],
'Phosphate':[370,436,690,827],
'Nitrate':[203,302],
'Manganese':[336,358,401,436,530],
'Zinc':[302,455],
'Citric Acid':[200],
'Butyric Acid':[206],
'Iron Oxide Nanoparticles':[295],
'Fe203 NP':[320,420],
'Al203':[491,562,650],
'Silica NP':[206]
}
def check(elements,peaks,thresh):
for element, ref_peaks in elements.items():
diff = all(abs(peak-ref_peak) <=thresh for peak, ref_peak in zip(peaks,ref_peaks))
print(element,' : ',diff)
if diff:
print(element)
def graph(simg):
image = cv2.imread(simg, cv2.IMREAD_GRAYSCALE)
pixel_positions = np.arange(0, image.shape[1])
#print(pixel_positions)
calibration_factor=2.9
wavelengths = pixel_positions * calibration_factor
# Extract the spectrum
spectrum = np.sum(image, axis=0) # Sum along columns
#print(len(spectrum),len(pixel_positions))
peaks=find_peaks(spectrum,height=10,threshold=5,distance=5)
print(wavelengths[peaks[0]])
plt.style.use('Solarize_Light2')
fig=plt.figure(figsize=(10, 5))
plt.plot(wavelengths, spectrum, color='b', linewidth=2)
plt.xlabel('Wavelength (nm)')
plt.ylabel('Intensity')
plt.title('Wavelength Spectrum')
plt.grid(True)
plt.show()
#plt.draw()
#plt.waitforbuttonpress(0)
#input()
#plt.close(fig)
check(elements,wavelengths[peaks[0]],10)
fig,ax=plt.subplots(4,4)
r,c=0,0
for i in elements:
#ax[r,c].figure(figsize=(10, 5))
mini,maxi=elements[i][0]-50,elements[i][-1]+50
ax[r,c].plot(wavelengths, spectrum, color='b', linewidth=2)
ax[r,c].set_xlim([mini,maxi])
for j in elements[i]:
ax[r,c].axvline(x=j,color='red',linestyle='dashed')
#ax[r,c].set(xlabel='Wavelength (nm)')
#ax[r,c].set(ylabel='Intensity')
ax[r,c].set_title(i)
#ax[r,c].grid(True)
r+=1
if r>3:
r=0
c+=1
if c>3:
break
plt.subplots_adjust(top=0.94,
bottom=0.07,
left=0.125,
right=0.9,
hspace=0.315,
wspace=0.2)
#plt.subplot_tool()
#print(plt.style.available)
mng = plt.get_current_fig_manager()
mng.full_screen_toggle()
plt.show()
if __name__ == "__main__":
import argparse
ap = argparse.ArgumentParser()
ap.add_argument("--image", type=str, default="samples/1.png",
help="image to test")
args = vars(ap.parse_args())
graph(args["image"])