@rosepetal/node-red-contrib-image-tools 1.5.0 → 1.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/artifacts/darwin-arm64/package.json +1 -1
- package/artifacts/linux-arm64/package.json +1 -1
- package/artifacts/linux-x64/package.json +1 -1
- package/artifacts/linuxmusl-x64/package.json +1 -1
- package/node-red-contrib-image-tools/nodes/io/image-out.html +19 -0
- package/node-red-contrib-image-tools/nodes/io/image-out.js +8 -4
- package/node-red-contrib-image-tools/nodes/specialized/heat-diff.html +218 -0
- package/node-red-contrib-image-tools/nodes/specialized/heat-diff.js +177 -0
- package/node-red-contrib-image-tools/nodes/transform/filter.html +111 -2
- package/node-red-contrib-image-tools/nodes/transform/filter.js +15 -4
- package/npm/darwin-arm64/package.json +1 -1
- package/npm/darwin-x64/package.json +1 -1
- package/npm/linux-arm64/package.json +1 -1
- package/npm/linux-x64/package.json +1 -1
- package/npm/linuxmusl-x64/package.json +1 -1
- package/npm/win32-x64/package.json +1 -1
- package/package.json +8 -7
- package/rosepetal-image-engine/binding.gyp +2 -1
- package/rosepetal-image-engine/src/filter.cpp +144 -0
- package/rosepetal-image-engine/src/heat-diff.cpp +146 -0
- package/rosepetal-image-engine/src/main.cpp +2 -0
|
@@ -29,6 +29,7 @@
|
|
|
29
29
|
overwriteProtection: { value: true },
|
|
30
30
|
maxImages: { value: "" },
|
|
31
31
|
maxImagesType: { value: "num" },
|
|
32
|
+
diskThreshold: { value: 90 },
|
|
32
33
|
active: { value: true },
|
|
33
34
|
debugEnabled: { value: false },
|
|
34
35
|
debugWidth: { value: 200 },
|
|
@@ -501,6 +502,12 @@
|
|
|
501
502
|
<span style="margin-left: 10px; color: #666;">0 or blank keeps all files.</span>
|
|
502
503
|
</div>
|
|
503
504
|
|
|
505
|
+
<div class="form-row">
|
|
506
|
+
<label for="node-input-diskThreshold"><i class="fa fa-hdd-o"></i> Disk Limit</label>
|
|
507
|
+
<input type="number" id="node-input-diskThreshold" min="0" max="100" step="1" style="width: 70px;">
|
|
508
|
+
<span style="margin-left: 10px; color: #666;">% full — skip saving above this. 0 disables check.</span>
|
|
509
|
+
</div>
|
|
510
|
+
|
|
504
511
|
<!-- Debug Configuration -->
|
|
505
512
|
<div class="form-row">
|
|
506
513
|
<label for="node-input-debugEnabled"><i class="fa fa-bug"></i> Debug</label>
|
|
@@ -658,6 +665,12 @@
|
|
|
658
665
|
<span style="margin-left: 10px; color: #666;">0 or blank keeps all files.</span>
|
|
659
666
|
</div>
|
|
660
667
|
|
|
668
|
+
<div class="form-row">
|
|
669
|
+
<label for="node-input-diskThreshold"><i class="fa fa-hdd-o"></i> Disk Limit</label>
|
|
670
|
+
<input type="number" id="node-input-diskThreshold" min="0" max="100" step="1" style="width: 70px;">
|
|
671
|
+
<span style="margin-left: 10px; color: #666;">% full — skip saving above this. 0 disables check.</span>
|
|
672
|
+
</div>
|
|
673
|
+
|
|
661
674
|
<!-- Debug Configuration -->
|
|
662
675
|
<div class="form-row">
|
|
663
676
|
<label for="node-input-debugEnabled"><i class="fa fa-bug"></i> Debug</label>
|
|
@@ -736,6 +749,9 @@
|
|
|
736
749
|
<dt>Max Images <span class="property-type">number | msg | flow | global</span></dt>
|
|
737
750
|
<dd>FIFO retention: deletes oldest files to keep at most this many.</dd>
|
|
738
751
|
|
|
752
|
+
<dt>Disk Limit <span class="property-type">number</span></dt>
|
|
753
|
+
<dd>Percentage of disk usage above which saving is skipped (default 90). Set to 0 to disable the check.</dd>
|
|
754
|
+
|
|
739
755
|
<dt>Active Button <span class="property-type">button</span></dt>
|
|
740
756
|
<dd>Enable/disable saving without modifying the flow.</dd>
|
|
741
757
|
</dl>
|
|
@@ -819,6 +835,9 @@
|
|
|
819
835
|
<dt>Max Images <span class="property-type">number | msg | flow | global</span></dt>
|
|
820
836
|
<dd>FIFO retention: deletes oldest files to keep at most this many.</dd>
|
|
821
837
|
|
|
838
|
+
<dt>Disk Limit <span class="property-type">number</span></dt>
|
|
839
|
+
<dd>Percentage of disk usage above which saving is skipped (default 90). Set to 0 to disable the check.</dd>
|
|
840
|
+
|
|
822
841
|
<dt>Active Button <span class="property-type">button</span></dt>
|
|
823
842
|
<dd>Enable/disable saving without modifying the flow.</dd>
|
|
824
843
|
</dl>
|
|
@@ -44,6 +44,10 @@ module.exports = function(RED) {
|
|
|
44
44
|
|
|
45
45
|
// Initialize node state
|
|
46
46
|
node.active = config.active !== false;
|
|
47
|
+
const diskThresholdPct = parseInt(config.diskThreshold, 10);
|
|
48
|
+
const diskThreshold = (Number.isInteger(diskThresholdPct) && diskThresholdPct > 0 && diskThresholdPct <= 100)
|
|
49
|
+
? diskThresholdPct / 100
|
|
50
|
+
: 0;
|
|
47
51
|
let diskCheckErrorLogged = false;
|
|
48
52
|
|
|
49
53
|
updateNodeStatus();
|
|
@@ -268,8 +272,8 @@ module.exports = function(RED) {
|
|
|
268
272
|
await fs.mkdir(folderPath, { recursive: true });
|
|
269
273
|
|
|
270
274
|
// Disk check
|
|
271
|
-
const diskInfo = await getDiskUsageInfo(folderPath);
|
|
272
|
-
if (diskInfo && diskInfo.usedRatio >=
|
|
275
|
+
const diskInfo = diskThreshold > 0 ? await getDiskUsageInfo(folderPath) : null;
|
|
276
|
+
if (diskInfo && diskInfo.usedRatio >= diskThreshold) {
|
|
273
277
|
const pct = (diskInfo.usedRatio * 100).toFixed(1);
|
|
274
278
|
node.warn(`Storage ${pct}% full, skipping remaining items.`);
|
|
275
279
|
node.status({ fill: 'yellow', shape: 'ring', text: `disk ${pct}% full` });
|
|
@@ -486,8 +490,8 @@ module.exports = function(RED) {
|
|
|
486
490
|
}
|
|
487
491
|
}
|
|
488
492
|
|
|
489
|
-
const diskInfo = await getDiskUsageInfo(folderPath);
|
|
490
|
-
if (diskInfo && diskInfo.usedRatio >=
|
|
493
|
+
const diskInfo = diskThreshold > 0 ? await getDiskUsageInfo(folderPath) : null;
|
|
494
|
+
if (diskInfo && diskInfo.usedRatio >= diskThreshold) {
|
|
491
495
|
const usedPercent = (diskInfo.usedRatio * 100).toFixed(1);
|
|
492
496
|
node.warn(`Storage at "${folderPath}" is ${usedPercent}% full. Skipping image save to avoid exhausting disk space.`);
|
|
493
497
|
node.status({ fill: "yellow", shape: "ring", text: `disk ${usedPercent}% full` });
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
<!-- nodes/specialized/heat-diff.html -->
|
|
2
|
+
|
|
3
|
+
<script type="text/javascript">
|
|
4
|
+
RED.nodes.registerType('rp-heat-diff',{
|
|
5
|
+
category:'RP Image',
|
|
6
|
+
color:'#FF6B6B',
|
|
7
|
+
icon:'font-awesome/fa-fire',
|
|
8
|
+
defaults:{
|
|
9
|
+
name:{value:""},
|
|
10
|
+
image1Path:{value:"payload"}, image1PathType:{value:"msg"},
|
|
11
|
+
image2Path:{value:"goldenImage"}, image2PathType:{value:"msg"},
|
|
12
|
+
outputPath:{value:"payload"},outputPathType:{value:"msg"},
|
|
13
|
+
outputFormat:{value:"raw"},
|
|
14
|
+
outputQuality:{value:90},
|
|
15
|
+
pngOptimize:{value:false},
|
|
16
|
+
webpLossless:{value:false},
|
|
17
|
+
webpSmartSubsample:{value:false},
|
|
18
|
+
webpEffort:{value:""},
|
|
19
|
+
debugEnabled:{value:false},
|
|
20
|
+
debugWidth:{value:200},
|
|
21
|
+
debugWidthType:{value:"num"},
|
|
22
|
+
colormap:{value:"JET"},
|
|
23
|
+
blurSize:{value:0},
|
|
24
|
+
threshold:{value:0}
|
|
25
|
+
},
|
|
26
|
+
inputs:1,outputs:1,
|
|
27
|
+
label:function(){return this.name||"rp-heat-diff";},
|
|
28
|
+
oneditprepare:function(){
|
|
29
|
+
$("#node-input-image1Path").typedInput({
|
|
30
|
+
default:'msg',types:['msg','flow','global'],
|
|
31
|
+
typeField:"#node-input-image1PathType"});
|
|
32
|
+
$("#node-input-image2Path").typedInput({
|
|
33
|
+
default:'msg',types:['msg','flow','global'],
|
|
34
|
+
typeField:"#node-input-image2PathType"});
|
|
35
|
+
$("#node-input-outputPath").typedInput({
|
|
36
|
+
default:'msg',types:['msg','flow','global'],
|
|
37
|
+
typeField:"#node-input-outputPathType"});
|
|
38
|
+
|
|
39
|
+
// Threshold slider
|
|
40
|
+
var $thresholdSlider = $("#node-input-threshold");
|
|
41
|
+
var $thresholdValue = $("#threshold-value");
|
|
42
|
+
function updateThresholdDisplay() {
|
|
43
|
+
$thresholdValue.text($thresholdSlider.val());
|
|
44
|
+
}
|
|
45
|
+
$thresholdSlider.on("input", updateThresholdDisplay);
|
|
46
|
+
updateThresholdDisplay();
|
|
47
|
+
|
|
48
|
+
// Output format visibility
|
|
49
|
+
function updateQualityVisibility(){
|
|
50
|
+
var format = $("#node-input-outputFormat").val();
|
|
51
|
+
$("#quality-row").toggle(format === "jpg" || format === "webp");
|
|
52
|
+
$("#png-optimize-row").toggle(format === "png");
|
|
53
|
+
var isWebp = format === 'webp';
|
|
54
|
+
$("#webp-lossless-row").toggle(isWebp);
|
|
55
|
+
$("#webp-smartsubsample-row").toggle(isWebp);
|
|
56
|
+
$("#webp-effort-row").toggle(isWebp);
|
|
57
|
+
}
|
|
58
|
+
$("#node-input-outputFormat").on("change", updateQualityVisibility);
|
|
59
|
+
updateQualityVisibility();
|
|
60
|
+
|
|
61
|
+
// Debug display
|
|
62
|
+
var debugCheckbox = $('#node-input-debugEnabled');
|
|
63
|
+
var debugWidthRow = $('#debug-width-row');
|
|
64
|
+
$("#node-input-debugWidth").typedInput({
|
|
65
|
+
default: 'num',
|
|
66
|
+
types: ['num', 'msg', 'flow', 'global'],
|
|
67
|
+
typeField: "#node-input-debugWidthType"
|
|
68
|
+
});
|
|
69
|
+
function updateDebugWidthVisibility() {
|
|
70
|
+
debugWidthRow.toggle(debugCheckbox.is(':checked'));
|
|
71
|
+
}
|
|
72
|
+
updateDebugWidthVisibility();
|
|
73
|
+
debugCheckbox.on('change', updateDebugWidthVisibility);
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
</script>
|
|
77
|
+
|
|
78
|
+
<script type="text/x-red" data-template-name="rp-heat-diff">
|
|
79
|
+
<div class="form-row">
|
|
80
|
+
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
|
|
81
|
+
<input type="text" id="node-input-name" placeholder="Name">
|
|
82
|
+
</div>
|
|
83
|
+
|
|
84
|
+
<div class="form-row">
|
|
85
|
+
<label><i class="fa fa-picture-o"></i> Image 1</label>
|
|
86
|
+
<input type="text" id="node-input-image1Path" style="width:70%;">
|
|
87
|
+
<input type="hidden" id="node-input-image1PathType">
|
|
88
|
+
</div>
|
|
89
|
+
|
|
90
|
+
<div class="form-row">
|
|
91
|
+
<label><i class="fa fa-picture-o"></i> Image 2</label>
|
|
92
|
+
<input type="text" id="node-input-image2Path" style="width:70%;">
|
|
93
|
+
<input type="hidden" id="node-input-image2PathType">
|
|
94
|
+
</div>
|
|
95
|
+
|
|
96
|
+
<div class="form-row">
|
|
97
|
+
<label><i class="fa fa-sign-out"></i> Output</label>
|
|
98
|
+
<input type="text" id="node-input-outputPath" style="width:70%;">
|
|
99
|
+
<input type="hidden" id="node-input-outputPathType">
|
|
100
|
+
</div>
|
|
101
|
+
|
|
102
|
+
<div class="form-row">
|
|
103
|
+
<label for="node-input-outputFormat"><i class="fa fa-file-image-o"></i> Output Format</label>
|
|
104
|
+
<select id="node-input-outputFormat" style="width: 200px;">
|
|
105
|
+
<option value="raw">Raw (fastest)</option>
|
|
106
|
+
<option value="jpg">JPEG</option>
|
|
107
|
+
<option value="png">PNG</option>
|
|
108
|
+
<option value="webp">WebP</option>
|
|
109
|
+
</select>
|
|
110
|
+
</div>
|
|
111
|
+
|
|
112
|
+
<div class="form-row" id="quality-row">
|
|
113
|
+
<label for="node-input-outputQuality"><i class="fa fa-sliders"></i> Quality</label>
|
|
114
|
+
<input type="number" id="node-input-outputQuality" min="1" max="100" value="90" style="width: 70px;">
|
|
115
|
+
<span style="margin-left: 10px; color: #666;">1-100 (JPG/WebP only)</span>
|
|
116
|
+
</div>
|
|
117
|
+
|
|
118
|
+
<div class="form-row" id="png-optimize-row" style="display: none;">
|
|
119
|
+
<label for="node-input-pngOptimize"><i class="fa fa-compress"></i> PNG Optimize</label>
|
|
120
|
+
<input type="checkbox" id="node-input-pngOptimize" style="display:inline-block; width:auto; vertical-align:baseline;">
|
|
121
|
+
<label for="node-input-pngOptimize" style="width:auto; margin-left:5px;">Compress PNG (slower, smaller file)</label>
|
|
122
|
+
</div>
|
|
123
|
+
<div class="form-row" id="webp-lossless-row" style="display: none;">
|
|
124
|
+
<label for="node-input-webpLossless"><i class="fa fa-lock"></i> Lossless</label>
|
|
125
|
+
<input type="checkbox" id="node-input-webpLossless" style="display:inline-block; width:auto; vertical-align:baseline;">
|
|
126
|
+
<label for="node-input-webpLossless" style="width:auto; margin-left:5px;">Encode WebP without loss</label>
|
|
127
|
+
</div>
|
|
128
|
+
<div class="form-row" id="webp-smartsubsample-row" style="display: none;">
|
|
129
|
+
<label for="node-input-webpSmartSubsample"><i class="fa fa-magic"></i> Smart Subsample</label>
|
|
130
|
+
<input type="checkbox" id="node-input-webpSmartSubsample" style="display:inline-block; width:auto; vertical-align:baseline;">
|
|
131
|
+
<label for="node-input-webpSmartSubsample" style="width:auto; margin-left:5px;">Better chroma downsampling (lossy only)</label>
|
|
132
|
+
</div>
|
|
133
|
+
<div class="form-row" id="webp-effort-row" style="display: none;">
|
|
134
|
+
<label for="node-input-webpEffort"><i class="fa fa-tachometer"></i> Effort</label>
|
|
135
|
+
<input type="number" id="node-input-webpEffort" min="0" max="6" placeholder="4" style="width: 70px;">
|
|
136
|
+
<span style="margin-left: 10px; color: #666;">0-6 (higher = smaller file, slower)</span>
|
|
137
|
+
</div>
|
|
138
|
+
|
|
139
|
+
<!-- Debug Configuration -->
|
|
140
|
+
<div class="form-row">
|
|
141
|
+
<label for="node-input-debugEnabled"><i class="fa fa-bug"></i> Debug</label>
|
|
142
|
+
<input type="checkbox" id="node-input-debugEnabled" style="display:inline-block; width:auto; vertical-align:baseline;">
|
|
143
|
+
<label for="node-input-debugEnabled" style="width:auto; margin-left:5px;">Show processed image</label>
|
|
144
|
+
</div>
|
|
145
|
+
<div class="form-row" id="debug-width-row" style="display: none;">
|
|
146
|
+
<label for="node-input-debugWidth"><i class="fa fa-arrows-h"></i> Debug Width</label>
|
|
147
|
+
<input type="text" id="node-input-debugWidth" style="width: 120px;">
|
|
148
|
+
<input type="hidden" id="node-input-debugWidthType">
|
|
149
|
+
<span style="margin-left: 10px; color: #666;">pixels</span>
|
|
150
|
+
</div>
|
|
151
|
+
|
|
152
|
+
<hr/>
|
|
153
|
+
|
|
154
|
+
<div class="form-row">
|
|
155
|
+
<label for="node-input-colormap"><i class="fa fa-fire"></i> Colormap</label>
|
|
156
|
+
<select id="node-input-colormap" style="width: 200px;">
|
|
157
|
+
<option value="JET">Jet (blue-cyan-green-yellow-red)</option>
|
|
158
|
+
<option value="HOT">Hot (black-red-yellow-white)</option>
|
|
159
|
+
<option value="INFERNO">Inferno (black-purple-red-yellow)</option>
|
|
160
|
+
<option value="TURBO">Turbo (improved rainbow)</option>
|
|
161
|
+
<option value="BONE">Bone (blue-tinted grayscale)</option>
|
|
162
|
+
<option value="COOL">Cool (cyan-magenta)</option>
|
|
163
|
+
<option value="RAINBOW">Rainbow</option>
|
|
164
|
+
<option value="OCEAN">Ocean (blue shades)</option>
|
|
165
|
+
<option value="PINK">Pink</option>
|
|
166
|
+
<option value="HSV">HSV</option>
|
|
167
|
+
</select>
|
|
168
|
+
</div>
|
|
169
|
+
|
|
170
|
+
<div class="form-row">
|
|
171
|
+
<label for="node-input-blurSize"><i class="fa fa-circle-o"></i> Blur</label>
|
|
172
|
+
<input type="number" id="node-input-blurSize" min="0" max="99" value="0" style="width: 70px;">
|
|
173
|
+
<span style="margin-left: 10px; color: #666;">Gaussian blur kernel size (0 = off, odd values)</span>
|
|
174
|
+
</div>
|
|
175
|
+
|
|
176
|
+
<div class="form-row">
|
|
177
|
+
<label for="node-input-threshold"><i class="fa fa-filter"></i> Threshold</label>
|
|
178
|
+
<input type="range" id="node-input-threshold" min="0" max="255" value="0" style="width: 200px;">
|
|
179
|
+
<span id="threshold-value" style="margin-left: 10px; font-weight: bold;">0</span>
|
|
180
|
+
<div style="margin-top: 5px; font-size: 10px; color: #999;">
|
|
181
|
+
Pixel differences below this value are suppressed (0 = show all)
|
|
182
|
+
</div>
|
|
183
|
+
</div>
|
|
184
|
+
</script>
|
|
185
|
+
|
|
186
|
+
<script type="text/x-red" data-help-name="rp-heat-diff">
|
|
187
|
+
<p><b>rosepetal-heat-diff</b> computes the absolute pixel difference between two images and visualizes the result as a color-mapped heat image. Useful for comparing a scanned image against a golden reference after alignment.</p>
|
|
188
|
+
|
|
189
|
+
<h3>Details</h3>
|
|
190
|
+
<p>Both input images are converted to grayscale, then the per-pixel absolute difference is computed. The difference map is passed through a configurable colormap to produce a heat visualization where brighter/warmer colors indicate larger differences.</p>
|
|
191
|
+
|
|
192
|
+
<h3>Properties</h3>
|
|
193
|
+
<dl class="message-properties">
|
|
194
|
+
<dt>Image 1 <span class="property-type">string</span></dt>
|
|
195
|
+
<dd>Path to the first image (e.g. the scanned/test image).</dd>
|
|
196
|
+
|
|
197
|
+
<dt>Image 2 <span class="property-type">string</span></dt>
|
|
198
|
+
<dd>Path to the second image (e.g. the golden/reference image).</dd>
|
|
199
|
+
|
|
200
|
+
<dt>Output to <span class="property-type">string</span></dt>
|
|
201
|
+
<dd>Message property where the heat map image will be stored.</dd>
|
|
202
|
+
|
|
203
|
+
<dt>Colormap <span class="property-type">string</span></dt>
|
|
204
|
+
<dd>Color mapping applied to the difference: JET, HOT, INFERNO, TURBO, BONE, COOL, RAINBOW, OCEAN, PINK, HSV.</dd>
|
|
205
|
+
|
|
206
|
+
<dt>Blur <span class="property-type">number</span></dt>
|
|
207
|
+
<dd>Gaussian blur kernel size applied to the difference before colormapping. Smooths noise. 0 to disable.</dd>
|
|
208
|
+
|
|
209
|
+
<dt>Threshold <span class="property-type">number</span></dt>
|
|
210
|
+
<dd>Pixel differences below this value (0-255) are set to zero before colormapping. Suppresses minor noise.</dd>
|
|
211
|
+
</dl>
|
|
212
|
+
|
|
213
|
+
<h3>Inputs</h3>
|
|
214
|
+
<p>Two images in the standard format (raw image object or encoded buffer). Images of different sizes are resized to match the larger one.</p>
|
|
215
|
+
|
|
216
|
+
<h3>Outputs</h3>
|
|
217
|
+
<p>A 3-channel BGR heat map image in the selected output format.</p>
|
|
218
|
+
</script>
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Node-RED logic for *rosepetal-heat-diff* (C++ backend).
|
|
3
|
+
* Computes absolute difference between two images and applies a colormap
|
|
4
|
+
* to produce a heat map visualization of the differences.
|
|
5
|
+
*
|
|
6
|
+
* Timing fields returned by C++:
|
|
7
|
+
* timing.convertMs . timing.taskMs . timing.encodeMs
|
|
8
|
+
*/
|
|
9
|
+
const { performance } = require('perf_hooks');
|
|
10
|
+
const Cpp = require('../../lib/cpp-bridge.js');
|
|
11
|
+
|
|
12
|
+
// OpenCV COLORMAP constants
|
|
13
|
+
const COLORMAP = {
|
|
14
|
+
'JET': 2,
|
|
15
|
+
'HOT': 11,
|
|
16
|
+
'INFERNO': 14,
|
|
17
|
+
'TURBO': 20,
|
|
18
|
+
'BONE': 1,
|
|
19
|
+
'COOL': 8,
|
|
20
|
+
'RAINBOW': 4,
|
|
21
|
+
'OCEAN': 5,
|
|
22
|
+
'PINK': 10,
|
|
23
|
+
'HSV': 9
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
module.exports = function (RED) {
|
|
27
|
+
const NodeUtils = require('../../lib/node-utils.js')(RED);
|
|
28
|
+
|
|
29
|
+
function HeatDiffNode (config) {
|
|
30
|
+
RED.nodes.createNode(this, config);
|
|
31
|
+
const node = this;
|
|
32
|
+
|
|
33
|
+
node.on('input', async (msg, send, done) => {
|
|
34
|
+
const outputPath = config.outputPath || 'payload';
|
|
35
|
+
const image1Path = config.image1Path || 'payload';
|
|
36
|
+
const image2Path = config.image2Path || 'payload.image2';
|
|
37
|
+
|
|
38
|
+
const { value: image1, error: image1Err } =
|
|
39
|
+
NodeUtils.safeGetMessageProperty(msg, image1Path);
|
|
40
|
+
if (image1Err) {
|
|
41
|
+
return NodeUtils.handleValidationErrorWithPassthrough(
|
|
42
|
+
node,
|
|
43
|
+
{
|
|
44
|
+
message: `Invalid image1Path "${image1Path}": ${image1Err.message}`,
|
|
45
|
+
hint: `Ensure "${image1Path}" exists on msg before this node.`,
|
|
46
|
+
details: { image1Path, image2Path, outputPath }
|
|
47
|
+
},
|
|
48
|
+
msg, send, done,
|
|
49
|
+
{ originalPayload: undefined, outputPath: null, outputType: 'preserve' }
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const baseImageForPassthrough = image1;
|
|
54
|
+
|
|
55
|
+
try {
|
|
56
|
+
const t0 = performance.now();
|
|
57
|
+
node.status({});
|
|
58
|
+
|
|
59
|
+
const { value: image2, error: image2Err } =
|
|
60
|
+
NodeUtils.safeGetMessageProperty(msg, image2Path);
|
|
61
|
+
if (image2Err) {
|
|
62
|
+
return NodeUtils.handleValidationErrorWithPassthrough(
|
|
63
|
+
node,
|
|
64
|
+
{
|
|
65
|
+
message: `Invalid image2Path "${image2Path}": ${image2Err.message}`,
|
|
66
|
+
hint: `Ensure "${image2Path}" exists on msg before this node.`,
|
|
67
|
+
details: { image1Path, image2Path, outputPath }
|
|
68
|
+
},
|
|
69
|
+
msg, send, done,
|
|
70
|
+
{ originalPayload: baseImageForPassthrough, outputPath, outputType: 'single' }
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const img1 = NodeUtils.validateImageStructure(image1, node);
|
|
75
|
+
if (!img1) {
|
|
76
|
+
return NodeUtils.handleValidationErrorWithPassthrough(
|
|
77
|
+
node,
|
|
78
|
+
{
|
|
79
|
+
message: 'First image is invalid or missing',
|
|
80
|
+
hint: `Check that "${image1Path}" contains a valid image.`,
|
|
81
|
+
details: { image1Path, image2Path, outputPath }
|
|
82
|
+
},
|
|
83
|
+
msg, send, done,
|
|
84
|
+
{ originalPayload: baseImageForPassthrough, outputPath, outputType: 'single' }
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const img2 = NodeUtils.validateImageStructure(image2, node);
|
|
89
|
+
if (!img2) {
|
|
90
|
+
return NodeUtils.handleValidationErrorWithPassthrough(
|
|
91
|
+
node,
|
|
92
|
+
{
|
|
93
|
+
message: 'Second image is invalid or missing',
|
|
94
|
+
hint: `Check that "${image2Path}" contains a valid image.`,
|
|
95
|
+
details: { image1Path, image2Path, outputPath }
|
|
96
|
+
},
|
|
97
|
+
msg, send, done,
|
|
98
|
+
{ originalPayload: baseImageForPassthrough, outputPath, outputType: 'single' }
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/* Options from the editor */
|
|
103
|
+
const colormapName = config.colormap || 'JET';
|
|
104
|
+
const colormapType = COLORMAP[colormapName] !== undefined ? COLORMAP[colormapName] : 2;
|
|
105
|
+
const blurSize = Math.max(0, parseInt(config.blurSize) || 0);
|
|
106
|
+
const threshold = Math.max(0, Math.min(255, parseInt(config.threshold) || 0));
|
|
107
|
+
const outputFormat = config.outputFormat || 'raw';
|
|
108
|
+
const outputQuality = parseInt(config.outputQuality) || 90;
|
|
109
|
+
const pngOptimize = config.pngOptimize || false;
|
|
110
|
+
const useSharpWebp = outputFormat === 'webp' && NodeUtils.hasAdvancedWebpOptions(config);
|
|
111
|
+
const cppFormat = useSharpWebp ? 'raw' : outputFormat;
|
|
112
|
+
|
|
113
|
+
/* Single call to the C++ addon */
|
|
114
|
+
let { image, timing = {} } =
|
|
115
|
+
await Cpp.heatDiff(img1, img2, colormapType, blurSize, threshold,
|
|
116
|
+
cppFormat, outputQuality, pngOptimize);
|
|
117
|
+
|
|
118
|
+
if (useSharpWebp) {
|
|
119
|
+
image = await NodeUtils.encodeWebpAdvanced(image, config);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/* Write result */
|
|
123
|
+
RED.util.setMessageProperty(msg, outputPath, image);
|
|
124
|
+
|
|
125
|
+
/* Status */
|
|
126
|
+
const total = performance.now() - t0;
|
|
127
|
+
|
|
128
|
+
let debugFormat = null;
|
|
129
|
+
const debugEnabled = config.debugEnabled === true || config.debugEnabled === 'true';
|
|
130
|
+
if (debugEnabled) {
|
|
131
|
+
try {
|
|
132
|
+
let debugWidth = NodeUtils.resolveDimension(
|
|
133
|
+
node,
|
|
134
|
+
config.debugWidthType || 'num',
|
|
135
|
+
config.debugWidth,
|
|
136
|
+
msg
|
|
137
|
+
);
|
|
138
|
+
debugWidth = Math.max(1, parseInt(debugWidth) || 200);
|
|
139
|
+
|
|
140
|
+
const debugResult = await NodeUtils.debugImageDisplay(
|
|
141
|
+
image, outputFormat, outputQuality,
|
|
142
|
+
node, debugEnabled, debugWidth
|
|
143
|
+
);
|
|
144
|
+
|
|
145
|
+
if (debugResult) {
|
|
146
|
+
debugFormat = debugResult.formatMessage;
|
|
147
|
+
NodeUtils.setSuccessStatusWithDebug(node, 1, total, timing, debugFormat);
|
|
148
|
+
}
|
|
149
|
+
} catch (debugError) {
|
|
150
|
+
node.warn(`Debug display error: ${debugError.message}`);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
if (!debugFormat) {
|
|
155
|
+
NodeUtils.setSuccessStatus(node, 1, total, timing);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
NodeUtils.recordPerformanceMetrics(node, msg, timing || {}, total);
|
|
159
|
+
|
|
160
|
+
send(msg);
|
|
161
|
+
done && done();
|
|
162
|
+
} catch (err) {
|
|
163
|
+
NodeUtils.handleNodeErrorWithPassthrough(
|
|
164
|
+
node, err, msg, send, done, 'heat-diff processing',
|
|
165
|
+
{
|
|
166
|
+
originalPayload: baseImageForPassthrough,
|
|
167
|
+
outputPath,
|
|
168
|
+
outputType: 'single',
|
|
169
|
+
context: { image1Path, image2Path, outputPath }
|
|
170
|
+
}
|
|
171
|
+
);
|
|
172
|
+
}
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
RED.nodes.registerType('rp-heat-diff', HeatDiffNode);
|
|
177
|
+
};
|
|
@@ -24,7 +24,12 @@
|
|
|
24
24
|
kernelSize: { value: "3" },
|
|
25
25
|
kernelSizeType: { value: "num" },
|
|
26
26
|
intensity: { value: "1.0" },
|
|
27
|
-
intensityType: { value: "num" }
|
|
27
|
+
intensityType: { value: "num" },
|
|
28
|
+
// Otsu-specific
|
|
29
|
+
otsuInvert: { value: false },
|
|
30
|
+
otsuPreBlur: { value: 0 },
|
|
31
|
+
// Histogram EQ-specific
|
|
32
|
+
histEqStrength: { value: 100 }
|
|
28
33
|
},
|
|
29
34
|
inputs: 1,
|
|
30
35
|
outputs: 1,
|
|
@@ -59,12 +64,37 @@
|
|
|
59
64
|
});
|
|
60
65
|
|
|
61
66
|
// === Dynamic parameter updates based on filter type ===
|
|
67
|
+
// Otsu strength display
|
|
68
|
+
var $otsuPreBlur = $("#node-input-otsuPreBlur");
|
|
69
|
+
var $otsuPreBlurValue = $("#otsu-preblur-value");
|
|
70
|
+
function updateOtsuPreBlurDisplay() {
|
|
71
|
+
var v = parseInt($otsuPreBlur.val());
|
|
72
|
+
$otsuPreBlurValue.text(v === 0 ? "Off" : v + "px");
|
|
73
|
+
}
|
|
74
|
+
$otsuPreBlur.on("input", updateOtsuPreBlurDisplay);
|
|
75
|
+
updateOtsuPreBlurDisplay();
|
|
76
|
+
|
|
77
|
+
// Histogram EQ strength display
|
|
78
|
+
var $histEqStrength = $("#node-input-histEqStrength");
|
|
79
|
+
var $histEqValue = $("#histeq-strength-value");
|
|
80
|
+
function updateHistEqDisplay() {
|
|
81
|
+
$histEqValue.text(parseInt($histEqStrength.val()) + "%");
|
|
82
|
+
}
|
|
83
|
+
$histEqStrength.on("input", updateHistEqDisplay);
|
|
84
|
+
updateHistEqDisplay();
|
|
85
|
+
|
|
62
86
|
function updateFilterParameters() {
|
|
63
87
|
const filterType = $("#node-input-filterType").val();
|
|
64
88
|
const kernelSizeRow = $("#kernel-size-row");
|
|
65
89
|
const intensityRow = $("#intensity-row");
|
|
66
90
|
const intensityLabel = $("#intensity-label");
|
|
67
|
-
|
|
91
|
+
const otsuRows = $("#otsu-params");
|
|
92
|
+
const histEqRows = $("#histeq-params");
|
|
93
|
+
|
|
94
|
+
// Hide all filter-specific rows first
|
|
95
|
+
otsuRows.hide();
|
|
96
|
+
histEqRows.hide();
|
|
97
|
+
|
|
68
98
|
// Show/hide and update labels based on filter type
|
|
69
99
|
switch (filterType) {
|
|
70
100
|
case 'blur':
|
|
@@ -92,6 +122,21 @@
|
|
|
92
122
|
intensityRow.show();
|
|
93
123
|
intensityLabel.text("Blur Radius");
|
|
94
124
|
break;
|
|
125
|
+
case 'otsu':
|
|
126
|
+
kernelSizeRow.hide();
|
|
127
|
+
intensityRow.hide();
|
|
128
|
+
otsuRows.show();
|
|
129
|
+
break;
|
|
130
|
+
case 'histogram_eq':
|
|
131
|
+
kernelSizeRow.hide();
|
|
132
|
+
intensityRow.hide();
|
|
133
|
+
histEqRows.show();
|
|
134
|
+
break;
|
|
135
|
+
case 'clahe':
|
|
136
|
+
kernelSizeRow.show();
|
|
137
|
+
intensityRow.show();
|
|
138
|
+
intensityLabel.text("Clip Limit");
|
|
139
|
+
break;
|
|
95
140
|
default:
|
|
96
141
|
kernelSizeRow.show();
|
|
97
142
|
intensityRow.show();
|
|
@@ -342,6 +387,9 @@
|
|
|
342
387
|
<option value="edge">Edge Detection</option>
|
|
343
388
|
<option value="emboss">Emboss</option>
|
|
344
389
|
<option value="gaussian">Gaussian Blur</option>
|
|
390
|
+
<option value="otsu">Otsu Threshold</option>
|
|
391
|
+
<option value="histogram_eq">Histogram Equalization</option>
|
|
392
|
+
<option value="clahe">CLAHE (Adaptive Hist. Eq.)</option>
|
|
345
393
|
</select>
|
|
346
394
|
</div>
|
|
347
395
|
|
|
@@ -357,6 +405,35 @@
|
|
|
357
405
|
<input type="hidden" id="node-input-intensityType">
|
|
358
406
|
</div>
|
|
359
407
|
|
|
408
|
+
<!-- Otsu-specific parameters -->
|
|
409
|
+
<div id="otsu-params" style="display: none;">
|
|
410
|
+
<div class="form-row">
|
|
411
|
+
<label for="node-input-otsuInvert"><i class="fa fa-exchange"></i> Invert</label>
|
|
412
|
+
<input type="checkbox" id="node-input-otsuInvert" style="display:inline-block; width:auto; vertical-align:baseline;">
|
|
413
|
+
<label for="node-input-otsuInvert" style="width:auto; margin-left:5px;">Invert binary output (white background, black foreground)</label>
|
|
414
|
+
</div>
|
|
415
|
+
<div class="form-row">
|
|
416
|
+
<label for="node-input-otsuPreBlur"><i class="fa fa-circle-o"></i> Pre-blur</label>
|
|
417
|
+
<input type="range" id="node-input-otsuPreBlur" min="0" max="15" step="2" value="0" style="width: 200px;">
|
|
418
|
+
<span id="otsu-preblur-value" style="margin-left: 10px; font-weight: bold;">Off</span>
|
|
419
|
+
<div style="margin-top: 5px; font-size: 10px; color: #999;">
|
|
420
|
+
Gaussian blur before thresholding to reduce noise (0 = off, odd values 3-15)
|
|
421
|
+
</div>
|
|
422
|
+
</div>
|
|
423
|
+
</div>
|
|
424
|
+
|
|
425
|
+
<!-- Histogram EQ-specific parameters -->
|
|
426
|
+
<div id="histeq-params" style="display: none;">
|
|
427
|
+
<div class="form-row">
|
|
428
|
+
<label for="node-input-histEqStrength"><i class="fa fa-sliders"></i> Strength</label>
|
|
429
|
+
<input type="range" id="node-input-histEqStrength" min="0" max="100" value="100" style="width: 200px;">
|
|
430
|
+
<span id="histeq-strength-value" style="margin-left: 10px; font-weight: bold;">100%</span>
|
|
431
|
+
<div style="margin-top: 5px; font-size: 10px; color: #999;">
|
|
432
|
+
Blend between original (0%) and fully equalized (100%)
|
|
433
|
+
</div>
|
|
434
|
+
</div>
|
|
435
|
+
</div>
|
|
436
|
+
|
|
360
437
|
<div class="form-tips">
|
|
361
438
|
<b>Tips:</b>
|
|
362
439
|
<ul>
|
|
@@ -445,6 +522,9 @@
|
|
|
445
522
|
<option value="edge">Edge Detection</option>
|
|
446
523
|
<option value="emboss">Emboss</option>
|
|
447
524
|
<option value="gaussian">Gaussian Blur</option>
|
|
525
|
+
<option value="otsu">Otsu Threshold</option>
|
|
526
|
+
<option value="histogram_eq">Histogram Equalization</option>
|
|
527
|
+
<option value="clahe">CLAHE (Adaptive Hist. Eq.)</option>
|
|
448
528
|
</select>
|
|
449
529
|
</div>
|
|
450
530
|
|
|
@@ -460,6 +540,35 @@
|
|
|
460
540
|
<input type="hidden" id="node-input-intensityType">
|
|
461
541
|
</div>
|
|
462
542
|
|
|
543
|
+
<!-- Otsu-specific parameters -->
|
|
544
|
+
<div id="otsu-params" style="display: none;">
|
|
545
|
+
<div class="form-row">
|
|
546
|
+
<label for="node-input-otsuInvert"><i class="fa fa-exchange"></i> Invert</label>
|
|
547
|
+
<input type="checkbox" id="node-input-otsuInvert" style="display:inline-block; width:auto; vertical-align:baseline;">
|
|
548
|
+
<label for="node-input-otsuInvert" style="width:auto; margin-left:5px;">Invert binary output (white background, black foreground)</label>
|
|
549
|
+
</div>
|
|
550
|
+
<div class="form-row">
|
|
551
|
+
<label for="node-input-otsuPreBlur"><i class="fa fa-circle-o"></i> Pre-blur</label>
|
|
552
|
+
<input type="range" id="node-input-otsuPreBlur" min="0" max="15" step="2" value="0" style="width: 200px;">
|
|
553
|
+
<span id="otsu-preblur-value" style="margin-left: 10px; font-weight: bold;">Off</span>
|
|
554
|
+
<div style="margin-top: 5px; font-size: 10px; color: #999;">
|
|
555
|
+
Gaussian blur before thresholding to reduce noise (0 = off, odd values 3-15)
|
|
556
|
+
</div>
|
|
557
|
+
</div>
|
|
558
|
+
</div>
|
|
559
|
+
|
|
560
|
+
<!-- Histogram EQ-specific parameters -->
|
|
561
|
+
<div id="histeq-params" style="display: none;">
|
|
562
|
+
<div class="form-row">
|
|
563
|
+
<label for="node-input-histEqStrength"><i class="fa fa-sliders"></i> Strength</label>
|
|
564
|
+
<input type="range" id="node-input-histEqStrength" min="0" max="100" value="100" style="width: 200px;">
|
|
565
|
+
<span id="histeq-strength-value" style="margin-left: 10px; font-weight: bold;">100%</span>
|
|
566
|
+
<div style="margin-top: 5px; font-size: 10px; color: #999;">
|
|
567
|
+
Blend between original (0%) and fully equalized (100%)
|
|
568
|
+
</div>
|
|
569
|
+
</div>
|
|
570
|
+
</div>
|
|
571
|
+
|
|
463
572
|
<div class="form-tips">
|
|
464
573
|
<b>Tips:</b>
|
|
465
574
|
<ul>
|
|
@@ -66,10 +66,21 @@ module.exports = function (RED) {
|
|
|
66
66
|
msg
|
|
67
67
|
));
|
|
68
68
|
|
|
69
|
-
//
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
69
|
+
// Override parameters for filter types with their own config
|
|
70
|
+
if (filterType === 'otsu') {
|
|
71
|
+
kernelSize = Math.max(0, Math.min(parseInt(config.otsuPreBlur) || 0, 15));
|
|
72
|
+
if (kernelSize > 0 && kernelSize < 3) kernelSize = 3;
|
|
73
|
+
if (kernelSize > 0 && kernelSize % 2 === 0) kernelSize++;
|
|
74
|
+
intensity = config.otsuInvert ? 1.0 : 0.0;
|
|
75
|
+
} else if (filterType === 'histogram_eq') {
|
|
76
|
+
kernelSize = 3; // unused but must be valid
|
|
77
|
+
intensity = Math.max(0.0, Math.min((parseInt(config.histEqStrength) || 100) / 100.0, 1.0));
|
|
78
|
+
} else {
|
|
79
|
+
// Standard filters: validate parameters
|
|
80
|
+
kernelSize = Math.max(3, Math.min(kernelSize, 15));
|
|
81
|
+
if (kernelSize % 2 === 0) kernelSize++; // Ensure odd size
|
|
82
|
+
intensity = Math.max(0.0, Math.min(intensity, 2.0));
|
|
83
|
+
}
|
|
73
84
|
|
|
74
85
|
// Validate input images
|
|
75
86
|
if (Array.isArray(originalPayload)) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rosepetal/node-red-contrib-image-tools",
|
|
3
|
-
"version": "1.5.
|
|
3
|
+
"version": "1.5.1",
|
|
4
4
|
"description": "Node-RED Image Processing Toolkit",
|
|
5
5
|
"main": "install.sh",
|
|
6
6
|
"directories": {
|
|
@@ -35,11 +35,11 @@
|
|
|
35
35
|
"sharp": "^0.34.2"
|
|
36
36
|
},
|
|
37
37
|
"optionalDependencies": {
|
|
38
|
-
"@rosepetal/node-red-contrib-image-tools-linux-x64": "1.5.
|
|
39
|
-
"@rosepetal/node-red-contrib-image-tools-linux-arm64": "1.5.
|
|
40
|
-
"@rosepetal/node-red-contrib-image-tools-linuxmusl-x64": "1.5.
|
|
41
|
-
"@rosepetal/node-red-contrib-image-tools-darwin-x64": "1.5.
|
|
42
|
-
"@rosepetal/node-red-contrib-image-tools-darwin-arm64": "1.5.
|
|
38
|
+
"@rosepetal/node-red-contrib-image-tools-linux-x64": "1.5.1",
|
|
39
|
+
"@rosepetal/node-red-contrib-image-tools-linux-arm64": "1.5.1",
|
|
40
|
+
"@rosepetal/node-red-contrib-image-tools-linuxmusl-x64": "1.5.1",
|
|
41
|
+
"@rosepetal/node-red-contrib-image-tools-darwin-x64": "1.5.1",
|
|
42
|
+
"@rosepetal/node-red-contrib-image-tools-darwin-arm64": "1.5.1"
|
|
43
43
|
},
|
|
44
44
|
"node-red": {
|
|
45
45
|
"version": ">=1.0.0",
|
|
@@ -62,7 +62,8 @@
|
|
|
62
62
|
"rp-blend": "node-red-contrib-image-tools/nodes/blend/blend.js",
|
|
63
63
|
"rp-add-mask": "node-red-contrib-image-tools/nodes/blend/add-mask.js",
|
|
64
64
|
"rp-add-masks": "node-red-contrib-image-tools/nodes/blend/add-masks.js",
|
|
65
|
-
"rp-add-bbs": "node-red-contrib-image-tools/nodes/blend/add-bbs.js"
|
|
65
|
+
"rp-add-bbs": "node-red-contrib-image-tools/nodes/blend/add-bbs.js",
|
|
66
|
+
"rp-heat-diff": "node-red-contrib-image-tools/nodes/specialized/heat-diff.js"
|
|
66
67
|
}
|
|
67
68
|
}
|
|
68
69
|
}
|
|
@@ -68,6 +68,12 @@ protected:
|
|
|
68
68
|
ApplyEmbossFilter();
|
|
69
69
|
} else if (filterType_ == "gaussian") {
|
|
70
70
|
ApplyGaussianFilter();
|
|
71
|
+
} else if (filterType_ == "otsu") {
|
|
72
|
+
ApplyOtsuFilter();
|
|
73
|
+
} else if (filterType_ == "histogram_eq") {
|
|
74
|
+
ApplyHistogramEqualization();
|
|
75
|
+
} else if (filterType_ == "clahe") {
|
|
76
|
+
ApplyCLAHE();
|
|
71
77
|
} else {
|
|
72
78
|
throw std::runtime_error("Unknown filter type: " + filterType_);
|
|
73
79
|
}
|
|
@@ -181,6 +187,144 @@ private:
|
|
|
181
187
|
double sigma = kernelSize_ / 6.0 * intensity_; // Scale sigma with intensity
|
|
182
188
|
cv::GaussianBlur(input_, result_, ksize, sigma);
|
|
183
189
|
}
|
|
190
|
+
|
|
191
|
+
void ApplyOtsuFilter() {
|
|
192
|
+
// kernelSize_ = pre-blur kernel (0 = none, 3+ = Gaussian blur)
|
|
193
|
+
// intensity_ = invert flag (0 = normal THRESH_BINARY, >0 = THRESH_BINARY_INV)
|
|
194
|
+
|
|
195
|
+
// Convert to grayscale if needed
|
|
196
|
+
cv::Mat gray;
|
|
197
|
+
bool wasColor = (input_.channels() > 1);
|
|
198
|
+
|
|
199
|
+
if (wasColor) {
|
|
200
|
+
if (channel_ == "BGR" || channel_ == "BGRA")
|
|
201
|
+
cv::cvtColor(input_, gray, input_.channels() == 4 ? cv::COLOR_BGRA2GRAY : cv::COLOR_BGR2GRAY);
|
|
202
|
+
else
|
|
203
|
+
cv::cvtColor(input_, gray, input_.channels() == 4 ? cv::COLOR_RGBA2GRAY : cv::COLOR_RGB2GRAY);
|
|
204
|
+
} else {
|
|
205
|
+
gray = input_;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// Optional pre-blur to reduce noise before thresholding
|
|
209
|
+
if (kernelSize_ >= 3) {
|
|
210
|
+
int k = kernelSize_ | 1; // ensure odd
|
|
211
|
+
cv::GaussianBlur(gray, gray, cv::Size(k, k), 0);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
int threshType = (intensity_ > 0.5) ? cv::THRESH_BINARY_INV : cv::THRESH_BINARY;
|
|
215
|
+
cv::Mat binary;
|
|
216
|
+
cv::threshold(gray, binary, 0, 255, threshType | cv::THRESH_OTSU);
|
|
217
|
+
|
|
218
|
+
// Convert back to original channel count
|
|
219
|
+
if (wasColor) {
|
|
220
|
+
if (channel_ == "BGR" || channel_ == "BGRA") {
|
|
221
|
+
cv::cvtColor(binary, result_, cv::COLOR_GRAY2BGR);
|
|
222
|
+
if (input_.channels() == 4) cv::cvtColor(result_, result_, cv::COLOR_BGR2BGRA);
|
|
223
|
+
} else {
|
|
224
|
+
cv::cvtColor(binary, result_, cv::COLOR_GRAY2RGB);
|
|
225
|
+
if (input_.channels() == 4) cv::cvtColor(result_, result_, cv::COLOR_RGB2RGBA);
|
|
226
|
+
}
|
|
227
|
+
} else {
|
|
228
|
+
result_ = binary;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
void ApplyHistogramEqualization() {
|
|
233
|
+
// intensity_ = strength blend factor (0.0 = original, 1.0 = fully equalized)
|
|
234
|
+
cv::Mat equalized;
|
|
235
|
+
|
|
236
|
+
if (input_.channels() == 1) {
|
|
237
|
+
cv::equalizeHist(input_, equalized);
|
|
238
|
+
} else {
|
|
239
|
+
// For color images, convert to YCrCb, equalize Y channel, convert back
|
|
240
|
+
cv::Mat ycrcb;
|
|
241
|
+
if (channel_ == "BGR" || channel_ == "BGRA") {
|
|
242
|
+
cv::Mat bgr = input_;
|
|
243
|
+
if (input_.channels() == 4) cv::cvtColor(input_, bgr, cv::COLOR_BGRA2BGR);
|
|
244
|
+
cv::cvtColor(bgr, ycrcb, cv::COLOR_BGR2YCrCb);
|
|
245
|
+
} else {
|
|
246
|
+
cv::Mat bgr;
|
|
247
|
+
if (input_.channels() == 4) cv::cvtColor(input_, bgr, cv::COLOR_RGBA2BGR);
|
|
248
|
+
else cv::cvtColor(input_, bgr, cv::COLOR_RGB2BGR);
|
|
249
|
+
cv::cvtColor(bgr, ycrcb, cv::COLOR_BGR2YCrCb);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
std::vector<cv::Mat> channels;
|
|
253
|
+
cv::split(ycrcb, channels);
|
|
254
|
+
cv::equalizeHist(channels[0], channels[0]);
|
|
255
|
+
cv::merge(channels, ycrcb);
|
|
256
|
+
|
|
257
|
+
cv::Mat bgr_out;
|
|
258
|
+
cv::cvtColor(ycrcb, bgr_out, cv::COLOR_YCrCb2BGR);
|
|
259
|
+
|
|
260
|
+
if (channel_ == "BGR") {
|
|
261
|
+
equalized = bgr_out;
|
|
262
|
+
} else if (channel_ == "BGRA") {
|
|
263
|
+
cv::cvtColor(bgr_out, equalized, cv::COLOR_BGR2BGRA);
|
|
264
|
+
} else if (channel_ == "RGB") {
|
|
265
|
+
cv::cvtColor(bgr_out, equalized, cv::COLOR_BGR2RGB);
|
|
266
|
+
} else if (channel_ == "RGBA") {
|
|
267
|
+
cv::cvtColor(bgr_out, equalized, cv::COLOR_BGR2RGBA);
|
|
268
|
+
} else {
|
|
269
|
+
equalized = bgr_out;
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
// Blend between original and equalized based on strength
|
|
274
|
+
double strength = std::max(0.0, std::min(intensity_, 1.0));
|
|
275
|
+
if (strength >= 0.999) {
|
|
276
|
+
result_ = equalized;
|
|
277
|
+
} else if (strength <= 0.001) {
|
|
278
|
+
result_ = input_.clone();
|
|
279
|
+
} else {
|
|
280
|
+
cv::addWeighted(equalized, strength, input_, 1.0 - strength, 0, result_);
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
void ApplyCLAHE() {
|
|
285
|
+
// Contrast Limited Adaptive Histogram Equalization
|
|
286
|
+
// intensity_ controls clip limit (default 2.0), kernelSize_ controls tile grid
|
|
287
|
+
double clipLimit = std::max(1.0, intensity_ * 4.0); // scale intensity 0-2 -> clipLimit 0-8
|
|
288
|
+
int tileSize = std::max(2, kernelSize_); // use kernel size as tile grid size
|
|
289
|
+
|
|
290
|
+
auto clahe = cv::createCLAHE(clipLimit, cv::Size(tileSize, tileSize));
|
|
291
|
+
|
|
292
|
+
if (input_.channels() == 1) {
|
|
293
|
+
clahe->apply(input_, result_);
|
|
294
|
+
} else {
|
|
295
|
+
cv::Mat ycrcb;
|
|
296
|
+
if (channel_ == "BGR" || channel_ == "BGRA") {
|
|
297
|
+
cv::Mat bgr = input_;
|
|
298
|
+
if (input_.channels() == 4) cv::cvtColor(input_, bgr, cv::COLOR_BGRA2BGR);
|
|
299
|
+
cv::cvtColor(bgr, ycrcb, cv::COLOR_BGR2YCrCb);
|
|
300
|
+
} else {
|
|
301
|
+
cv::Mat bgr;
|
|
302
|
+
if (input_.channels() == 4) cv::cvtColor(input_, bgr, cv::COLOR_RGBA2BGR);
|
|
303
|
+
else cv::cvtColor(input_, bgr, cv::COLOR_RGB2BGR);
|
|
304
|
+
cv::cvtColor(bgr, ycrcb, cv::COLOR_BGR2YCrCb);
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
std::vector<cv::Mat> channels;
|
|
308
|
+
cv::split(ycrcb, channels);
|
|
309
|
+
clahe->apply(channels[0], channels[0]);
|
|
310
|
+
cv::merge(channels, ycrcb);
|
|
311
|
+
|
|
312
|
+
cv::Mat bgr_out;
|
|
313
|
+
cv::cvtColor(ycrcb, bgr_out, cv::COLOR_YCrCb2BGR);
|
|
314
|
+
|
|
315
|
+
if (channel_ == "BGR") {
|
|
316
|
+
result_ = bgr_out;
|
|
317
|
+
} else if (channel_ == "BGRA") {
|
|
318
|
+
cv::cvtColor(bgr_out, result_, cv::COLOR_BGR2BGRA);
|
|
319
|
+
} else if (channel_ == "RGB") {
|
|
320
|
+
cv::cvtColor(bgr_out, result_, cv::COLOR_BGR2RGB);
|
|
321
|
+
} else if (channel_ == "RGBA") {
|
|
322
|
+
cv::cvtColor(bgr_out, result_, cv::COLOR_BGR2RGBA);
|
|
323
|
+
} else {
|
|
324
|
+
result_ = bgr_out;
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
}
|
|
184
328
|
};
|
|
185
329
|
|
|
186
330
|
/*──────── binding: filter(image, filterType, kernelSize, intensity, [outputFormat], [quality], [pngOptimize], callback) ─*/
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
#include <napi.h>
|
|
2
|
+
#include <opencv2/opencv.hpp>
|
|
3
|
+
#include "utils.h"
|
|
4
|
+
|
|
5
|
+
class HeatDiffWorker final : public Napi::AsyncWorker {
|
|
6
|
+
public:
|
|
7
|
+
HeatDiffWorker(Napi::Function cb,
|
|
8
|
+
const Napi::Value& jsImg1,
|
|
9
|
+
const Napi::Value& jsImg2,
|
|
10
|
+
int colormapType,
|
|
11
|
+
int blurSize,
|
|
12
|
+
int threshold,
|
|
13
|
+
std::string outputFormat,
|
|
14
|
+
int quality = 90,
|
|
15
|
+
bool pngOptimize = false)
|
|
16
|
+
: Napi::AsyncWorker(cb),
|
|
17
|
+
colormapType(colormapType),
|
|
18
|
+
blurSize(blurSize),
|
|
19
|
+
threshold(threshold),
|
|
20
|
+
outputFormat(std::move(outputFormat)),
|
|
21
|
+
quality(quality),
|
|
22
|
+
pngOptimize(pngOptimize)
|
|
23
|
+
{
|
|
24
|
+
const int64 t0 = cv::getTickCount();
|
|
25
|
+
mat1 = ConvertToMat(jsImg1);
|
|
26
|
+
mat2 = ConvertToMat(jsImg2);
|
|
27
|
+
format1 = DetectChannelFormatShared(jsImg1, mat1);
|
|
28
|
+
format2 = DetectChannelFormatShared(jsImg2, mat2);
|
|
29
|
+
convertMs = (cv::getTickCount() - t0) / cv::getTickFrequency() * 1e3;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
protected:
|
|
33
|
+
void Execute() override {
|
|
34
|
+
const int64 t0 = cv::getTickCount();
|
|
35
|
+
|
|
36
|
+
// Convert both to grayscale for diff computation
|
|
37
|
+
cv::Mat gray1, gray2;
|
|
38
|
+
|
|
39
|
+
if (mat1.channels() == 1) {
|
|
40
|
+
gray1 = mat1;
|
|
41
|
+
} else if (format1 == "BGR" || format1 == "BGRA") {
|
|
42
|
+
cv::cvtColor(mat1, gray1, mat1.channels() == 4 ? cv::COLOR_BGRA2GRAY : cv::COLOR_BGR2GRAY);
|
|
43
|
+
} else {
|
|
44
|
+
cv::cvtColor(mat1, gray1, mat1.channels() == 4 ? cv::COLOR_RGBA2GRAY : cv::COLOR_RGB2GRAY);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (mat2.channels() == 1) {
|
|
48
|
+
gray2 = mat2;
|
|
49
|
+
} else if (format2 == "BGR" || format2 == "BGRA") {
|
|
50
|
+
cv::cvtColor(mat2, gray2, mat2.channels() == 4 ? cv::COLOR_BGRA2GRAY : cv::COLOR_BGR2GRAY);
|
|
51
|
+
} else {
|
|
52
|
+
cv::cvtColor(mat2, gray2, mat2.channels() == 4 ? cv::COLOR_RGBA2GRAY : cv::COLOR_RGB2GRAY);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Resize if dimensions differ
|
|
56
|
+
if (gray1.size() != gray2.size()) {
|
|
57
|
+
cv::Size targetSize(std::max(gray1.cols, gray2.cols),
|
|
58
|
+
std::max(gray1.rows, gray2.rows));
|
|
59
|
+
if (gray1.size() != targetSize) cv::resize(gray1, gray1, targetSize);
|
|
60
|
+
if (gray2.size() != targetSize) cv::resize(gray2, gray2, targetSize);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Absolute difference
|
|
64
|
+
cv::Mat diff;
|
|
65
|
+
cv::absdiff(gray1, gray2, diff);
|
|
66
|
+
|
|
67
|
+
// Optional Gaussian blur to smooth noise
|
|
68
|
+
if (blurSize > 0) {
|
|
69
|
+
int ksize = blurSize | 1; // ensure odd
|
|
70
|
+
cv::GaussianBlur(diff, diff, cv::Size(ksize, ksize), 0);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// Optional threshold to suppress small differences
|
|
74
|
+
if (threshold > 0) {
|
|
75
|
+
cv::threshold(diff, diff, threshold, 0, cv::THRESH_TOZERO);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// Apply colormap
|
|
79
|
+
cv::applyColorMap(diff, result, colormapType);
|
|
80
|
+
// applyColorMap outputs BGR
|
|
81
|
+
outputChannel = "BGR";
|
|
82
|
+
|
|
83
|
+
taskMs = (cv::getTickCount() - t0) / cv::getTickFrequency() * 1e3;
|
|
84
|
+
|
|
85
|
+
if (outputFormat != "raw") {
|
|
86
|
+
cv::Mat tmp = PrepareForEncoding(result, outputChannel, outputFormat);
|
|
87
|
+
encodeMs = EncodeToFormat(tmp, encodedBuf, outputFormat, quality, pngOptimize);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
void OnOK() override {
|
|
92
|
+
Napi::Env env = Env();
|
|
93
|
+
Napi::Value jsImg = (outputFormat != "raw")
|
|
94
|
+
? VectorToBuffer(env, std::move(encodedBuf))
|
|
95
|
+
: MatToRawJS(env, result, outputChannel);
|
|
96
|
+
|
|
97
|
+
Napi::Object out = Napi::Object::New(env);
|
|
98
|
+
out.Set("image", jsImg);
|
|
99
|
+
out.Set("timing", MakeTimingJS(env, convertMs, taskMs, encodeMs));
|
|
100
|
+
Callback().Call({env.Null(), out});
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
private:
|
|
104
|
+
cv::Mat mat1, mat2, result;
|
|
105
|
+
std::string format1, format2, outputChannel;
|
|
106
|
+
int colormapType;
|
|
107
|
+
int blurSize;
|
|
108
|
+
int threshold;
|
|
109
|
+
std::string outputFormat;
|
|
110
|
+
int quality;
|
|
111
|
+
bool pngOptimize;
|
|
112
|
+
double convertMs = 0, taskMs = 0, encodeMs = 0;
|
|
113
|
+
std::vector<uchar> encodedBuf;
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
Napi::Value HeatDiff(const Napi::CallbackInfo& info) {
|
|
117
|
+
Napi::Env env = info.Env();
|
|
118
|
+
|
|
119
|
+
if (info.Length() < 3 || !info[info.Length() - 1].IsFunction()) {
|
|
120
|
+
Napi::TypeError::New(env,
|
|
121
|
+
"heatDiff(image1, image2, colormapType, [blurSize], [threshold], [outputFormat], [quality], [pngOptimize], callback)")
|
|
122
|
+
.ThrowAsJavaScriptException();
|
|
123
|
+
return env.Null();
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
Napi::Value jsImg1 = info[0];
|
|
127
|
+
Napi::Value jsImg2 = info[1];
|
|
128
|
+
int colormapType = info[2].As<Napi::Number>().Int32Value();
|
|
129
|
+
|
|
130
|
+
int blurSize = 0;
|
|
131
|
+
int threshold = 0;
|
|
132
|
+
std::string outputFormat = "raw";
|
|
133
|
+
int quality = 90;
|
|
134
|
+
bool pngOptimize = false;
|
|
135
|
+
size_t cbIdx = 3;
|
|
136
|
+
|
|
137
|
+
if (info.Length() >= 5) { blurSize = info[3].As<Napi::Number>().Int32Value(); cbIdx = 4; }
|
|
138
|
+
if (info.Length() >= 6) { threshold = info[4].As<Napi::Number>().Int32Value(); cbIdx = 5; }
|
|
139
|
+
if (info.Length() >= 7) { outputFormat = info[5].As<Napi::String>().Utf8Value(); cbIdx = 6; }
|
|
140
|
+
if (info.Length() >= 8) { quality = info[6].As<Napi::Number>().Int32Value(); cbIdx = 7; }
|
|
141
|
+
if (info.Length() >= 9) { pngOptimize = info[7].As<Napi::Boolean>().Value(); cbIdx = 8; }
|
|
142
|
+
|
|
143
|
+
(new HeatDiffWorker(info[cbIdx].As<Napi::Function>(),
|
|
144
|
+
jsImg1, jsImg2, colormapType, blurSize, threshold, outputFormat, quality, pngOptimize))->Queue();
|
|
145
|
+
return env.Undefined();
|
|
146
|
+
}
|
|
@@ -15,6 +15,7 @@ Napi::Value AddBBs(const Napi::CallbackInfo& info);
|
|
|
15
15
|
Napi::Value ImageAlign(const Napi::CallbackInfo& info);
|
|
16
16
|
Napi::Value Draw(const Napi::CallbackInfo& info);
|
|
17
17
|
Napi::Value ColorConvert(const Napi::CallbackInfo& info);
|
|
18
|
+
Napi::Value HeatDiff(const Napi::CallbackInfo& info);
|
|
18
19
|
|
|
19
20
|
Napi::Object Init(Napi::Env env, Napi::Object exports) {
|
|
20
21
|
exports.Set(Napi::String::New(env, "resize"), Napi::Function::New(env, Resize));
|
|
@@ -32,6 +33,7 @@ Napi::Object Init(Napi::Env env, Napi::Object exports) {
|
|
|
32
33
|
exports.Set(Napi::String::New(env, "imageAlign"), Napi::Function::New(env, ImageAlign));
|
|
33
34
|
exports.Set(Napi::String::New(env, "draw"), Napi::Function::New(env, Draw));
|
|
34
35
|
exports.Set(Napi::String::New(env, "colorConvert"), Napi::Function::New(env, ColorConvert));
|
|
36
|
+
exports.Set(Napi::String::New(env, "heatDiff"), Napi::Function::New(env, HeatDiff));
|
|
35
37
|
return exports;
|
|
36
38
|
}
|
|
37
39
|
|