-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcolor_interaction.py
230 lines (205 loc) · 7.84 KB
/
color_interaction.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
# -*- coding: utf-8 -*-
"""
@brief color_interaction
This program is free software under the GNU General Public License
(>=v2). Read the file COPYING that comes with GRASS for details.
@author: Anna Petrasova ([email protected])
"""
import wx
from gui_core.gselect import Select
import grass.script as gscript
from grass.pydispatch.signal import Signal
from grass.exceptions import CalledModuleError
from tangible_utils import get_environment
class ColorInteractionPanel(wx.Panel):
def __init__(self, parent, giface, settings, scaniface):
wx.Panel.__init__(self, parent)
self.group = None
self.segment = "segment"
self.segment_clump = "segment_clump"
self.signature = "signature"
self.classification = "classification"
self.filtered_classification = "fclassification"
self.reject = "reject"
self.output = "objects"
self.hasSuperpixels = gscript.find_program("i.superpixels.slic", "--help")
self.env = None
self.giface = giface
self.parent = parent
self.settings = settings
self.scaniface = scaniface
self.settingsChanged = Signal("ColorInteractionPanel.settingsChanged")
if "color" not in self.settings:
self.settings["color"] = {}
self.settings["color"]["active"] = False
self.settings["color"]["name"] = ""
self.settings["color"]["training"] = ""
self.hide = []
self.ifColor = wx.CheckBox(self, label=_("Save color rasters:"))
self.ifColor.SetValue(self.settings["color"]["active"])
self.ifColor.Bind(wx.EVT_CHECKBOX, self.OnChange)
self.exportColor = Select(self, size=(-1, -1), type="raster")
self.exportColor.SetValue(self.settings["color"]["name"])
self.exportColor.Bind(wx.EVT_TEXT, self.OnChange)
self.hide.append(self.exportColor)
if self.settings["color"]["name"]:
self.group = self.settings["color"]["name"]
self.trainingAreas = Select(self, size=(-1, -1), type="raster")
self.trainingAreas.SetValue(self.settings["color"]["training"])
self.trainingAreas.Bind(wx.EVT_TEXT, self.OnChange)
labelTraining = wx.StaticText(self, label=_("Training areas:"))
self.hide.append(self.trainingAreas)
self.hide.append(labelTraining)
calibrateBtn = wx.Button(self, label=_("Calibrate"))
calibrateBtn.Bind(wx.EVT_BUTTON, self.OnCalibration)
self.hide.append(calibrateBtn)
analyzeBtn = wx.Button(self, label=_("Scan and process"))
analyzeBtn.Bind(wx.EVT_BUTTON, self.OnAnalysis)
self.hide.append(analyzeBtn)
self.mainSizer = wx.BoxSizer(wx.VERTICAL)
sizer = wx.BoxSizer(wx.HORIZONTAL)
sizer.Add(self.ifColor, flag=wx.ALIGN_CENTER_VERTICAL, border=5)
sizer.Add(
self.exportColor, proportion=1, flag=wx.ALIGN_CENTER_VERTICAL, border=5
)
self.mainSizer.Add(sizer, flag=wx.EXPAND | wx.ALL, border=5)
sizer = wx.BoxSizer(wx.HORIZONTAL)
sizer.Add(labelTraining, flag=wx.ALIGN_CENTER_VERTICAL, border=5)
sizer.Add(
self.trainingAreas, proportion=1, flag=wx.ALIGN_CENTER_VERTICAL, border=5
)
sizer.Add(calibrateBtn, flag=wx.ALIGN_CENTER_VERTICAL, border=5)
self.mainSizer.Add(sizer, flag=wx.EXPAND | wx.ALL, border=5)
sizer = wx.BoxSizer(wx.HORIZONTAL)
sizer.AddStretchSpacer()
sizer.Add(analyzeBtn, flag=wx.ALIGN_CENTER_VERTICAL, border=5)
self.mainSizer.Add(sizer, flag=wx.EXPAND | wx.ALL, border=5)
self.SetSizer(self.mainSizer)
self.mainSizer.Fit(self)
self._enable()
def OnChange(self, event):
self.settings["color"]["training"] = self.trainingAreas.GetValue()
self.settings["color"]["active"] = self.ifColor.IsChecked()
self.settings["color"]["name"] = self.exportColor.GetValue()
self.group = self.settings["color"]["name"]
self._enable()
def _enable(self):
for each in self.hide:
each.Enable(self.ifColor.IsChecked())
def _defineEnvironment(self):
try:
gscript.read_command(
"i.group",
flags="g",
group=self.group,
subgroup=self.group,
env=self.env,
)
except CalledModuleError:
gscript.run_command(
"i.group",
group=self.group,
subgroup=self.group,
input=[self.group + "_" + ext for ext in ("r", "g", "b")],
env=self.env,
)
maps = gscript.read_command(
"i.group", flags="g", group=self.group, subgroup=self.group
).strip()
if maps:
self.env = get_environment(raster=maps.splitlines()[0])
def OnAnalysis(self, event):
self._defineEnvironment()
self.Run(self.Analyze)
def OnCalibration(self, event):
self._defineEnvironment()
training = self.trainingAreas.GetValue()
if not training:
return
self.Run(self.Calibrate)
def Run(self, func):
ll = self.giface.GetLayerList()
checked = []
for l in ll:
if ll.IsLayerChecked(l):
checked.append(l.cmd)
ll.CheckLayer(l, False)
wx.Yield()
if not self.scaniface.IsScanning():
self.scaniface.Scan(continuous=False)
self.scaniface.process.wait()
self.scaniface.process = None
self.scaniface.status.SetLabel("Done.")
self.Done(func, checked)
elif self.scaniface.pause:
pass
else:
wx.CallLater(3000, self.Done, func, checked)
def Done(self, func, checked):
func()
ll = self.giface.GetLayerList()
for l in ll:
if l.cmd in checked:
ll.CheckLayer(l, True)
def Calibrate(self):
gscript.run_command(
"i.gensigset",
trainingmap=self.settings["color"]["training"],
group=self.group,
subgroup=self.group,
signaturefile=self.signature,
env=self.env,
overwrite=True,
) # we need here overwrite=True
def Analyze(self):
if self.hasSuperpixels:
gscript.run_command(
"i.superpixels.slic",
group=self.group,
output=self.segment,
compactness=2,
minsize=50,
env=self.env,
)
else:
gscript.run_command(
"i.segment",
group=self.group,
output=self.segment,
threshold=0.3,
minsize=50,
env=self.env,
)
gscript.run_command(
"r.clump", input=self.segment, output=self.segment_clump, env=self.env
)
gscript.run_command(
"i.smap",
group=self.group,
subgroup=self.group,
signaturefile=self.signature,
output=self.classification,
goodness=self.reject,
env=self.env,
)
percentile = float(
gscript.parse_command(
"r.univar", flags="ge", map=self.reject, env=self.env
)["percentile_90"]
)
gscript.mapcalc(
"{new} = if({classif} < {thres}, {classif}, null())".format(
new=self.filtered_classification,
classif=self.classification,
thres=percentile,
),
env=self.env,
)
segments = self.segment if self.hasSuperpixels else self.segment_clump
gscript.run_command(
"r.stats.quantile",
base=segments,
cover=self.filtered_classification,
output=self.output,
env=self.env,
)