forked from Kazuhito00/Image-Processing-Node-Editor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_adaptive_threshold.py
More file actions
409 lines (340 loc) · 15.6 KB
/
Copy pathnode_adaptive_threshold.py
File metadata and controls
409 lines (340 loc) · 15.6 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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import time
import cv2
import numpy as np
import dearpygui.dearpygui as dpg
from node_editor.util import dpg_get_value, dpg_set_value
from node.node_abc import DpgNodeABC
from node.basenode import Node
def image_process(image, method, threshold_type, block_size, c_value):
"""Apply adaptive thresholding for varying lighting conditions.
Adaptive thresholding is crucial for object detection in images with uneven
lighting, shadows, or varying illumination. It calculates threshold values
locally for each pixel region, making it superior to fixed thresholding.
Args:
image: Input BGR image
method: Adaptive method (0=Mean, 1=Gaussian)
threshold_type: Threshold type (0=Binary, 1=Binary Inverted)
block_size: Size of pixel neighborhood (must be odd)
c_value: Constant subtracted from weighted mean
Returns:
Thresholded image
"""
# Ensure block size is odd
if block_size % 2 == 0:
block_size += 1
# Convert to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Adaptive method mapping
methods = {
0: cv2.ADAPTIVE_THRESH_MEAN_C,
1: cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
}
# Threshold type mapping
threshold_types = {
0: cv2.THRESH_BINARY,
1: cv2.THRESH_BINARY_INV,
}
# Apply adaptive threshold
result = cv2.adaptiveThreshold(
gray,
255,
methods[method],
threshold_types[threshold_type],
block_size,
c_value
)
# Convert back to BGR for display
result = cv2.cvtColor(result, cv2.COLOR_GRAY2BGR)
return result
class FactoryNode:
node_label = 'Adaptive Threshold'
node_tag = 'AdaptiveThreshold'
def __init__(self):
pass
def add_node(
self,
parent,
node_id,
pos=[0, 0],
opencv_setting_dict=None,
callback=None,
):
node = Node()
node.tag_node_name = str(node_id) + ':' + node.node_tag
node.tag_node_input01_name = node.tag_node_name + ':' + node.TYPE_IMAGE + ':Input01'
node.tag_node_input01_value_name = node.tag_node_name + ':' + node.TYPE_IMAGE + ':Input01Value'
node.tag_node_input02_name = node.tag_node_name + ':' + node.TYPE_INT + ':Input02'
node.tag_node_input02_value_name = node.tag_node_name + ':' + node.TYPE_INT + ':Input02Value'
node.tag_node_input03_name = node.tag_node_name + ':' + node.TYPE_FLOAT + ':Input03'
node.tag_node_input03_value_name = node.tag_node_name + ':' + node.TYPE_FLOAT + ':Input03Value'
node.tag_node_input_enable_name = node.tag_node_name + ':' + node.TYPE_JSON + ':InputEnable'
node.tag_node_input_enable_value_name = node.tag_node_name + ':' + node.TYPE_JSON + ':InputEnableValue'
node.tag_node_enable_checkbox_name = node.tag_node_name + ':EnableCheckbox'
node.tag_node_enable_checkbox_value_name = node.tag_node_name + ':EnableCheckboxValue'
node.tag_node_combo_method_name = node.tag_node_name + ':MethodCombo'
node.tag_node_combo_method_value_name = node.tag_node_name + ':MethodComboValue'
node.tag_node_combo_type_name = node.tag_node_name + ':TypeCombo'
node.tag_node_combo_type_value_name = node.tag_node_name + ':TypeComboValue'
node.tag_node_output01_name = node.tag_node_name + ':' + node.TYPE_IMAGE + ':Output01'
node.tag_node_output01_value_name = node.tag_node_name + ':' + node.TYPE_IMAGE + ':Output01Value'
node.tag_node_output02_name = node.tag_node_name + ':' + node.TYPE_TIME_MS + ':Output02'
node.tag_node_output02_value_name = node.tag_node_name + ':' + node.TYPE_TIME_MS + ':Output02Value'
node._opencv_setting_dict = opencv_setting_dict
small_window_w = node._opencv_setting_dict['process_width']
small_window_h = node._opencv_setting_dict['process_height']
use_pref_counter = node._opencv_setting_dict['use_pref_counter']
black_image = np.zeros((small_window_w, small_window_h, 3))
black_texture = node.convert_cv_to_dpg(
black_image,
small_window_w,
small_window_h,
)
with dpg.texture_registry(show=False):
dpg.add_raw_texture(
small_window_w,
small_window_h,
black_texture,
tag=node.tag_node_output01_value_name,
format=dpg.mvFormat_Float_rgb,
)
with dpg.node(
tag=node.tag_node_name,
parent=parent,
label=node.node_label,
pos=pos,
):
with dpg.node_attribute(
tag=node.tag_node_input01_name,
attribute_type=dpg.mvNode_Attr_Input,
):
dpg.add_text(
tag=node.tag_node_input01_value_name,
default_value='Input BGR image',
)
# Boolean enable/disable input
with dpg.node_attribute(
tag=node.tag_node_input_enable_name,
attribute_type=dpg.mvNode_Attr_Input,
):
dpg.add_text(
tag=node.tag_node_input_enable_value_name,
default_value='Enable (JSON BOOL)',
)
# Enable checkbox (default True)
with dpg.node_attribute(
tag=node.tag_node_enable_checkbox_name,
attribute_type=dpg.mvNode_Attr_Static,
):
dpg.add_checkbox(
tag=node.tag_node_enable_checkbox_value_name,
label='Enable processing',
default_value=True,
)
with dpg.node_attribute(
tag=node.tag_node_output01_name,
attribute_type=dpg.mvNode_Attr_Output,
):
dpg.add_image(node.tag_node_output01_value_name)
# Method combo
with dpg.node_attribute(
tag=node.tag_node_combo_method_name,
attribute_type=dpg.mvNode_Attr_Static,
):
dpg.add_combo(
tag=node.tag_node_combo_method_value_name,
label='Method',
items=node._methods,
default_value=node._methods[1], # Gaussian by default
width=small_window_w - 80,
)
# Threshold type combo
with dpg.node_attribute(
tag=node.tag_node_combo_type_name,
attribute_type=dpg.mvNode_Attr_Static,
):
dpg.add_combo(
tag=node.tag_node_combo_type_value_name,
label='Type',
items=node._threshold_types,
default_value=node._threshold_types[0],
width=small_window_w - 80,
)
# Block size slider
with dpg.node_attribute(
tag=node.tag_node_input02_name,
attribute_type=dpg.mvNode_Attr_Input,
):
dpg.add_slider_int(
tag=node.tag_node_input02_value_name,
label="Block Size",
width=small_window_w - 80,
default_value=11,
min_value=node._min_block_size,
max_value=node._max_block_size,
callback=None,
)
# C value slider
with dpg.node_attribute(
tag=node.tag_node_input03_name,
attribute_type=dpg.mvNode_Attr_Input,
):
dpg.add_slider_float(
tag=node.tag_node_input03_value_name,
label="C Value",
width=small_window_w - 80,
default_value=2.0,
min_value=node._min_c,
max_value=node._max_c,
callback=None,
)
if use_pref_counter:
with dpg.node_attribute(
tag=node.tag_node_output02_name,
attribute_type=dpg.mvNode_Attr_Output,
):
dpg.add_text(
tag=node.tag_node_output02_value_name,
default_value='elapsed time(ms)',
)
return node
class Node(Node):
_ver = '0.0.1'
node_label = 'Adaptive Threshold'
node_tag = 'AdaptiveThreshold'
_methods = ['Mean', 'Gaussian']
_threshold_types = ['Binary', 'Binary Inverted']
_min_block_size = 3
_max_block_size = 99
_min_c = -20.0
_max_c = 20.0
_opencv_setting_dict = None
def __init__(self):
pass
def update(
self,
node_id,
connection_list,
node_image_dict,
node_result_dict,
node_audio_dict,
):
tag_node_name = str(node_id) + ':' + self.node_tag
combo_method_tag = tag_node_name + ':MethodComboValue'
combo_type_tag = tag_node_name + ':TypeComboValue'
input_value02_tag = tag_node_name + ':' + self.TYPE_INT + ':Input02Value'
input_value03_tag = tag_node_name + ':' + self.TYPE_FLOAT + ':Input03Value'
enable_checkbox_tag = tag_node_name + ':EnableCheckboxValue'
output_value01_tag = tag_node_name + ':' + self.TYPE_IMAGE + ':Output01Value'
output_value02_tag = tag_node_name + ':' + self.TYPE_TIME_MS + ':Output02Value'
small_window_w = self._opencv_setting_dict['process_width']
small_window_h = self._opencv_setting_dict['process_height']
use_pref_counter = self._opencv_setting_dict['use_pref_counter']
# Check if processing is enabled via checkbox (default) or JSON input
enable_processing = dpg_get_value(enable_checkbox_tag)
# Check for JSON boolean input (overrides checkbox if connected)
enable_from_json = None
for connection_info in connection_list:
connection_type = connection_info[0].split(":")[2]
if connection_type.upper() == self.TYPE_JSON.upper():
# Check if this is the enable input
if ":InputEnable" in connection_info[1]:
connection_info_src = connection_info[0]
connection_info_src = connection_info_src.split(':')[:2]
connection_info_src = ':'.join(connection_info_src)
json_data = node_result_dict.get(connection_info_src, None)
if json_data is not None and isinstance(json_data, dict):
enable_from_json = json_data.get('BOOL', None)
break
# JSON input overrides checkbox if connected
if enable_from_json is not None:
enable_processing = enable_from_json
# Handle connections
for connection_info in connection_list:
connection_type = connection_info[0].split(':')[2]
if connection_type == self.TYPE_INT:
source_tag = connection_info[0] + 'Value'
destination_tag = connection_info[1] + 'Value'
input_value = int(dpg_get_value(source_tag))
input_value = max(self._min_block_size, input_value)
input_value = min(self._max_block_size, input_value)
dpg_set_value(destination_tag, input_value)
if connection_type == self.TYPE_FLOAT:
source_tag = connection_info[0] + 'Value'
destination_tag = connection_info[1] + 'Value'
input_value = round(float(dpg_get_value(source_tag)), 3)
input_value = max(self._min_c, input_value)
input_value = min(self._max_c, input_value)
dpg_set_value(destination_tag, input_value)
frame = self.get_input_frame(connection_list, node_image_dict, node_audio_dict)
method_str = dpg_get_value(combo_method_tag)
method = self._methods.index(method_str)
threshold_type_str = dpg_get_value(combo_type_tag)
threshold_type = self._threshold_types.index(threshold_type_str)
block_size = int(dpg_get_value(input_value02_tag))
c_value = float(dpg_get_value(input_value03_tag))
if frame is not None and use_pref_counter:
start_time = time.monotonic()
# Only process if enabled, otherwise pass-through
if frame is not None and enable_processing:
frame = image_process(frame, method, threshold_type, block_size, c_value)
if frame is not None and use_pref_counter:
elapsed_time = time.monotonic() - start_time
elapsed_time = int(elapsed_time * 1000)
dpg_set_value(output_value02_tag,
str(elapsed_time).zfill(4) + 'ms')
if frame is not None:
texture = self.convert_cv_to_dpg(
frame,
small_window_w,
small_window_h,
)
dpg_set_value(output_value01_tag, texture)
return {"image": frame, "json": None, "audio": None}
def close(self, node_id):
pass
def get_setting_dict(self, node_id):
tag_node_name = str(node_id) + ':' + self.node_tag
combo_method_tag = tag_node_name + ':MethodComboValue'
combo_type_tag = tag_node_name + ':TypeComboValue'
input_value02_tag = tag_node_name + ':' + self.TYPE_INT + ':Input02Value'
input_value03_tag = tag_node_name + ':' + self.TYPE_FLOAT + ':Input03Value'
enable_checkbox_tag = tag_node_name + ':EnableCheckboxValue'
pos = dpg.get_item_pos(tag_node_name)
method = dpg_get_value(combo_method_tag)
threshold_type = dpg_get_value(combo_type_tag)
block_size = dpg_get_value(input_value02_tag)
c_value = dpg_get_value(input_value03_tag)
enable_value = dpg_get_value(enable_checkbox_tag)
setting_dict = {}
setting_dict['ver'] = self._ver
setting_dict['pos'] = pos
setting_dict[combo_method_tag] = method
setting_dict[combo_type_tag] = threshold_type
setting_dict[input_value02_tag] = block_size
setting_dict[input_value03_tag] = c_value
setting_dict[enable_checkbox_tag] = enable_value
return setting_dict
def set_setting_dict(self, node_id, setting_dict):
tag_node_name = str(node_id) + ':' + self.node_tag
combo_method_tag = tag_node_name + ':MethodComboValue'
combo_type_tag = tag_node_name + ':TypeComboValue'
input_value02_tag = tag_node_name + ':' + self.TYPE_INT + ':Input02Value'
input_value03_tag = tag_node_name + ':' + self.TYPE_FLOAT + ':Input03Value'
enable_checkbox_tag = tag_node_name + ':EnableCheckboxValue'
if combo_method_tag in setting_dict:
method = setting_dict[combo_method_tag]
dpg_set_value(combo_method_tag, method)
if combo_type_tag in setting_dict:
threshold_type = setting_dict[combo_type_tag]
dpg_set_value(combo_type_tag, threshold_type)
if input_value02_tag in setting_dict:
block_size = int(setting_dict[input_value02_tag])
dpg_set_value(input_value02_tag, block_size)
if input_value03_tag in setting_dict:
c_value = float(setting_dict[input_value03_tag])
dpg_set_value(input_value03_tag, c_value)
if enable_checkbox_tag in setting_dict:
enable_value = setting_dict[enable_checkbox_tag]
dpg_set_value(enable_checkbox_tag, enable_value)