@rosepetal/node-red-contrib-image-tools 1.1.3
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/.gitattributes +6 -0
- package/.github/workflows/build-prebuild.yml +326 -0
- package/LICENSE +13 -0
- package/README.md +161 -0
- package/artifacts/darwin-arm64/package.json +25 -0
- package/artifacts/linux-arm64/package.json +26 -0
- package/artifacts/linux-x64/package.json +26 -0
- package/artifacts/linuxmusl-x64/package.json +26 -0
- package/assets/flow_time_example.gif +0 -0
- package/assets/nodes/io/array-in-demo.gif +0 -0
- package/assets/nodes/io/array-out-demo.gif +0 -0
- package/assets/nodes/io/array-select-demo.gif +0 -0
- package/assets/nodes/io/image-in-demo.gif +0 -0
- package/assets/nodes/mix/concat-demo.gif +0 -0
- package/assets/nodes/mix/mosaic-demo.gif +0 -0
- package/assets/nodes/transform/crop-demo.gif +0 -0
- package/assets/nodes/transform/filter-demo.gif +0 -0
- package/assets/nodes/transform/padding-demo.gif +0 -0
- package/assets/nodes/transform/resize-demo.gif +0 -0
- package/assets/nodes/transform/rotate-demo.gif +0 -0
- package/docs/nodes/blend/add-mask.md +275 -0
- package/docs/nodes/blend/add-masks.md +284 -0
- package/docs/nodes/blend/blend.md +248 -0
- package/docs/nodes/io/array-in.md +119 -0
- package/docs/nodes/io/array-out.md +150 -0
- package/docs/nodes/io/array-select.md +157 -0
- package/docs/nodes/io/image-in.md +131 -0
- package/docs/nodes/io/image-out.md +179 -0
- package/docs/nodes/io/queue.md +57 -0
- package/docs/nodes/io/save-file.md +30 -0
- package/docs/nodes/mix/advanced-mosaic.md +108 -0
- package/docs/nodes/mix/concat.md +127 -0
- package/docs/nodes/mix/mosaic.md +146 -0
- package/docs/nodes/specialized/cropBB.md +271 -0
- package/docs/nodes/specialized/image-align.md +426 -0
- package/docs/nodes/transform/crop.md +132 -0
- package/docs/nodes/transform/draw.md +77 -0
- package/docs/nodes/transform/filter.md +84 -0
- package/docs/nodes/transform/padding.md +133 -0
- package/docs/nodes/transform/resize.md +158 -0
- package/docs/nodes/transform/rotate.md +121 -0
- package/docs/nodes/util/block-detect.md +46 -0
- package/install.sh +144 -0
- package/node-red-contrib-image-tools/lib/cpp-bridge.js +224 -0
- package/node-red-contrib-image-tools/lib/node-utils.js +529 -0
- package/node-red-contrib-image-tools/nodes/blend/add-bbs.html +590 -0
- package/node-red-contrib-image-tools/nodes/blend/add-bbs.js +241 -0
- package/node-red-contrib-image-tools/nodes/blend/add-mask.html +449 -0
- package/node-red-contrib-image-tools/nodes/blend/add-mask.js +170 -0
- package/node-red-contrib-image-tools/nodes/blend/add-masks.html +703 -0
- package/node-red-contrib-image-tools/nodes/blend/add-masks.js +321 -0
- package/node-red-contrib-image-tools/nodes/blend/blend.html +552 -0
- package/node-red-contrib-image-tools/nodes/blend/blend.js +124 -0
- package/node-red-contrib-image-tools/nodes/io/array-in.html +110 -0
- package/node-red-contrib-image-tools/nodes/io/array-in.js +113 -0
- package/node-red-contrib-image-tools/nodes/io/array-out.html +125 -0
- package/node-red-contrib-image-tools/nodes/io/array-out.js +213 -0
- package/node-red-contrib-image-tools/nodes/io/array-select.html +156 -0
- package/node-red-contrib-image-tools/nodes/io/array-select.js +187 -0
- package/node-red-contrib-image-tools/nodes/io/folder-in.html +248 -0
- package/node-red-contrib-image-tools/nodes/io/folder-in.js +335 -0
- package/node-red-contrib-image-tools/nodes/io/image-in.html +146 -0
- package/node-red-contrib-image-tools/nodes/io/image-in.js +143 -0
- package/node-red-contrib-image-tools/nodes/io/image-out.html +506 -0
- package/node-red-contrib-image-tools/nodes/io/image-out.js +545 -0
- package/node-red-contrib-image-tools/nodes/io/queue.html +119 -0
- package/node-red-contrib-image-tools/nodes/io/queue.js +191 -0
- package/node-red-contrib-image-tools/nodes/io/save-file.html +243 -0
- package/node-red-contrib-image-tools/nodes/io/save-file.js +440 -0
- package/node-red-contrib-image-tools/nodes/mix/advanced-mosaic.html +2149 -0
- package/node-red-contrib-image-tools/nodes/mix/advanced-mosaic.js +195 -0
- package/node-red-contrib-image-tools/nodes/mix/concat.html +390 -0
- package/node-red-contrib-image-tools/nodes/mix/concat.js +112 -0
- package/node-red-contrib-image-tools/nodes/mix/mosaic.html +550 -0
- package/node-red-contrib-image-tools/nodes/mix/mosaic.js +151 -0
- package/node-red-contrib-image-tools/nodes/specialized/cropBB.html +268 -0
- package/node-red-contrib-image-tools/nodes/specialized/cropBB.js +277 -0
- package/node-red-contrib-image-tools/nodes/specialized/image-align.html +528 -0
- package/node-red-contrib-image-tools/nodes/specialized/image-align.js +306 -0
- package/node-red-contrib-image-tools/nodes/transform/crop.html +410 -0
- package/node-red-contrib-image-tools/nodes/transform/crop.js +149 -0
- package/node-red-contrib-image-tools/nodes/transform/draw.html +719 -0
- package/node-red-contrib-image-tools/nodes/transform/draw.js +509 -0
- package/node-red-contrib-image-tools/nodes/transform/filter.html +578 -0
- package/node-red-contrib-image-tools/nodes/transform/filter.js +177 -0
- package/node-red-contrib-image-tools/nodes/transform/padding.html +475 -0
- package/node-red-contrib-image-tools/nodes/transform/padding.js +142 -0
- package/node-red-contrib-image-tools/nodes/transform/resize.html +429 -0
- package/node-red-contrib-image-tools/nodes/transform/resize.js +162 -0
- package/node-red-contrib-image-tools/nodes/transform/rotate.html +299 -0
- package/node-red-contrib-image-tools/nodes/transform/rotate.js +141 -0
- package/node-red-contrib-image-tools/nodes/util/block-detect.html +85 -0
- package/node-red-contrib-image-tools/nodes/util/block-detect.js +136 -0
- package/node-red-contrib-image-tools/nodes/util/clean-debug.html +201 -0
- package/node-red-contrib-image-tools/nodes/util/clean-debug.js +393 -0
- package/node-red-contrib-image-tools/package.json +64 -0
- package/npm/darwin-arm64/lib/.gitkeep +1 -0
- package/npm/darwin-arm64/package.json +25 -0
- package/npm/darwin-x64/lib/.gitkeep +1 -0
- package/npm/darwin-x64/package.json +25 -0
- package/npm/linux-arm64/lib/.gitkeep +1 -0
- package/npm/linux-arm64/package.json +26 -0
- package/npm/linux-x64/lib/.gitkeep +1 -0
- package/npm/linux-x64/package.json +26 -0
- package/npm/linuxmusl-x64/lib/.gitkeep +1 -0
- package/npm/linuxmusl-x64/package.json +26 -0
- package/npm/win32-x64/lib/.gitkeep +1 -0
- package/npm/win32-x64/package.json +25 -0
- package/package.json +74 -0
- package/rosepetal-image-engine/binding.gyp +153 -0
- package/rosepetal-image-engine/package.json +19 -0
- package/rosepetal-image-engine/src/add-bbs.cpp +433 -0
- package/rosepetal-image-engine/src/add-mask.cpp +240 -0
- package/rosepetal-image-engine/src/add-masks.cpp +638 -0
- package/rosepetal-image-engine/src/advanced-mosaic.cpp +740 -0
- package/rosepetal-image-engine/src/blend.cpp +200 -0
- package/rosepetal-image-engine/src/concat.cpp +329 -0
- package/rosepetal-image-engine/src/crop.cpp +139 -0
- package/rosepetal-image-engine/src/draw.cpp +414 -0
- package/rosepetal-image-engine/src/filter.cpp +230 -0
- package/rosepetal-image-engine/src/filters/kernels.h +171 -0
- package/rosepetal-image-engine/src/image-align.cpp +551 -0
- package/rosepetal-image-engine/src/main.cpp +36 -0
- package/rosepetal-image-engine/src/mosaic.cpp +373 -0
- package/rosepetal-image-engine/src/padding.cpp +124 -0
- package/rosepetal-image-engine/src/resize.cpp +210 -0
- package/rosepetal-image-engine/src/rotate.cpp +177 -0
- package/rosepetal-image-engine/src/utils.h +563 -0
- package/rosepetal-image-engine/test-formats.js +119 -0
- package/scripts/build-opencv-static.ps1 +166 -0
- package/scripts/build-opencv-static.sh +147 -0
|
@@ -0,0 +1,551 @@
|
|
|
1
|
+
// File: src/image-align.cpp
|
|
2
|
+
// Ultra-fast image alignment using ECC algorithm for Node-RED
|
|
3
|
+
|
|
4
|
+
#include <napi.h>
|
|
5
|
+
#include <opencv2/opencv.hpp>
|
|
6
|
+
#include <chrono>
|
|
7
|
+
#include <cstring>
|
|
8
|
+
#include <string>
|
|
9
|
+
#include <vector>
|
|
10
|
+
#include <cmath>
|
|
11
|
+
#include <algorithm>
|
|
12
|
+
#include <utility>
|
|
13
|
+
#include "utils.h"
|
|
14
|
+
|
|
15
|
+
class ImageAlignWorker : public Napi::AsyncWorker {
|
|
16
|
+
public:
|
|
17
|
+
ImageAlignWorker(Napi::Function& callback,
|
|
18
|
+
const Napi::Value& referenceImage,
|
|
19
|
+
const Napi::Value& targetImage,
|
|
20
|
+
double scale,
|
|
21
|
+
int maxIterations,
|
|
22
|
+
double terminationEps,
|
|
23
|
+
std::string outputFormat,
|
|
24
|
+
int quality = 90,
|
|
25
|
+
bool pngOptimize = false,
|
|
26
|
+
bool returnMatrix = false,
|
|
27
|
+
const Napi::Value& polygonValue = Napi::Value())
|
|
28
|
+
: Napi::AsyncWorker(callback),
|
|
29
|
+
scale(scale),
|
|
30
|
+
maxIterations(maxIterations),
|
|
31
|
+
terminationEps(terminationEps),
|
|
32
|
+
outputFormat(std::move(outputFormat)),
|
|
33
|
+
quality(quality),
|
|
34
|
+
pngOptimize(pngOptimize),
|
|
35
|
+
returnMatrix(returnMatrix),
|
|
36
|
+
alignmentSuccess(false),
|
|
37
|
+
hasPolygon(false),
|
|
38
|
+
isPolygonArray(false) {
|
|
39
|
+
|
|
40
|
+
try {
|
|
41
|
+
auto t0 = std::chrono::steady_clock::now();
|
|
42
|
+
|
|
43
|
+
// Convert input images to OpenCV Mat
|
|
44
|
+
referenceMat = ConvertToMat(referenceImage);
|
|
45
|
+
targetMat = ConvertToMat(targetImage);
|
|
46
|
+
|
|
47
|
+
// Parse polygon(s) if provided
|
|
48
|
+
if (!polygonValue.IsNull() && !polygonValue.IsUndefined() && polygonValue.IsArray()) {
|
|
49
|
+
Napi::Array polygonArray = polygonValue.As<Napi::Array>();
|
|
50
|
+
if (polygonArray.Length() > 0) {
|
|
51
|
+
// Check if first element is a coordinate pair or another array
|
|
52
|
+
Napi::Value firstElement = polygonArray[0u];
|
|
53
|
+
|
|
54
|
+
if (firstElement.IsArray()) {
|
|
55
|
+
Napi::Array firstArray = firstElement.As<Napi::Array>();
|
|
56
|
+
|
|
57
|
+
// Check if it's a single polygon [[x,y], [x,y], ...]
|
|
58
|
+
// or array of polygons [[[x,y], [x,y], ...], ...]
|
|
59
|
+
if (firstArray.Length() >= 2 &&
|
|
60
|
+
firstArray.Get(0u).IsNumber() &&
|
|
61
|
+
firstArray.Get(1u).IsNumber()) {
|
|
62
|
+
// Single polygon
|
|
63
|
+
hasPolygon = true;
|
|
64
|
+
isPolygonArray = false;
|
|
65
|
+
originalPolygon.reserve(polygonArray.Length());
|
|
66
|
+
|
|
67
|
+
for (uint32_t i = 0; i < polygonArray.Length(); i++) {
|
|
68
|
+
Napi::Value point = polygonArray[i];
|
|
69
|
+
if (point.IsArray()) {
|
|
70
|
+
Napi::Array pointArray = point.As<Napi::Array>();
|
|
71
|
+
if (pointArray.Length() >= 2) {
|
|
72
|
+
double x = pointArray.Get(0u).As<Napi::Number>().DoubleValue();
|
|
73
|
+
double y = pointArray.Get(1u).As<Napi::Number>().DoubleValue();
|
|
74
|
+
originalPolygon.push_back(cv::Point2f(x, y));
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
} else if (firstArray.Length() > 0 && firstArray.Get(0u).IsArray()) {
|
|
79
|
+
// Array of polygons
|
|
80
|
+
hasPolygon = true;
|
|
81
|
+
isPolygonArray = true;
|
|
82
|
+
originalPolygons.reserve(polygonArray.Length());
|
|
83
|
+
|
|
84
|
+
for (uint32_t i = 0; i < polygonArray.Length(); i++) {
|
|
85
|
+
Napi::Value polyValue = polygonArray[i];
|
|
86
|
+
if (polyValue.IsArray()) {
|
|
87
|
+
Napi::Array singlePolygon = polyValue.As<Napi::Array>();
|
|
88
|
+
std::vector<cv::Point2f> polygon;
|
|
89
|
+
polygon.reserve(singlePolygon.Length());
|
|
90
|
+
|
|
91
|
+
for (uint32_t j = 0; j < singlePolygon.Length(); j++) {
|
|
92
|
+
Napi::Value point = singlePolygon[j];
|
|
93
|
+
if (point.IsArray()) {
|
|
94
|
+
Napi::Array pointArray = point.As<Napi::Array>();
|
|
95
|
+
if (pointArray.Length() >= 2) {
|
|
96
|
+
double x = pointArray.Get(0u).As<Napi::Number>().DoubleValue();
|
|
97
|
+
double y = pointArray.Get(1u).As<Napi::Number>().DoubleValue();
|
|
98
|
+
polygon.push_back(cv::Point2f(x, y));
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
if (!polygon.empty()) {
|
|
104
|
+
originalPolygons.push_back(polygon);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// Detect channel format for output
|
|
114
|
+
referenceChannelOrder = DetectChannelFormat(referenceImage, referenceMat);
|
|
115
|
+
targetChannelOrder = DetectChannelFormat(targetImage, targetMat);
|
|
116
|
+
|
|
117
|
+
// Use reference image format for output
|
|
118
|
+
outputChannelOrder = referenceChannelOrder;
|
|
119
|
+
|
|
120
|
+
auto t1 = std::chrono::steady_clock::now();
|
|
121
|
+
convertMs = std::chrono::duration<double, std::milli>(t1 - t0).count();
|
|
122
|
+
|
|
123
|
+
} catch (const Napi::Error& e) {
|
|
124
|
+
SetError(e.Message());
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
protected:
|
|
129
|
+
void Execute() override {
|
|
130
|
+
try {
|
|
131
|
+
auto taskStart = std::chrono::steady_clock::now();
|
|
132
|
+
|
|
133
|
+
// Get reference dimensions
|
|
134
|
+
int refHeight = referenceMat.rows;
|
|
135
|
+
int refWidth = referenceMat.cols;
|
|
136
|
+
|
|
137
|
+
// Resize target image to match reference dimensions
|
|
138
|
+
cv::Mat targetResized;
|
|
139
|
+
if (targetMat.size() != referenceMat.size()) {
|
|
140
|
+
cv::resize(targetMat, targetResized, cv::Size(refWidth, refHeight));
|
|
141
|
+
} else {
|
|
142
|
+
targetResized = targetMat;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// Convert images to grayscale for alignment
|
|
146
|
+
cv::Mat refGray, targetGray;
|
|
147
|
+
ConvertToGray(referenceMat, refGray);
|
|
148
|
+
ConvertToGray(targetResized, targetGray);
|
|
149
|
+
|
|
150
|
+
// Find transformation matrix
|
|
151
|
+
cv::Mat transformMatrix;
|
|
152
|
+
alignmentSuccess = FindTransformation(refGray, targetGray, transformMatrix);
|
|
153
|
+
|
|
154
|
+
if (alignmentSuccess) {
|
|
155
|
+
// Store transformation matrix if requested
|
|
156
|
+
if (returnMatrix) {
|
|
157
|
+
transformationMatrix = transformMatrix.clone();
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// Transform polygon(s) if provided
|
|
161
|
+
if (hasPolygon) {
|
|
162
|
+
if (isPolygonArray) {
|
|
163
|
+
// Transform array of polygons
|
|
164
|
+
TransformPolygons(originalPolygons, transformMatrix, refWidth, refHeight);
|
|
165
|
+
} else if (!originalPolygon.empty()) {
|
|
166
|
+
// Transform single polygon
|
|
167
|
+
TransformPolygon(originalPolygon, transformMatrix, refWidth, refHeight);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// Apply transformation to the color target image
|
|
172
|
+
ApplyTransformation(targetResized, transformMatrix, cv::Size(refWidth, refHeight));
|
|
173
|
+
} else {
|
|
174
|
+
// If alignment fails, use the resized target as-is
|
|
175
|
+
alignedImage = targetResized;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
auto taskEnd = std::chrono::steady_clock::now();
|
|
179
|
+
taskMs = std::chrono::duration<double, std::milli>(taskEnd - taskStart).count();
|
|
180
|
+
|
|
181
|
+
} catch (const std::exception& e) {
|
|
182
|
+
SetError(std::string("Image alignment failed: ") + e.what());
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
void OnOK() override {
|
|
187
|
+
Napi::Env env = Env();
|
|
188
|
+
|
|
189
|
+
try {
|
|
190
|
+
auto encodeStart = std::chrono::steady_clock::now();
|
|
191
|
+
Napi::Value result;
|
|
192
|
+
|
|
193
|
+
if (outputFormat == "raw") {
|
|
194
|
+
// Return raw image object
|
|
195
|
+
result = MatToRawJS(env, alignedImage, outputChannelOrder);
|
|
196
|
+
} else {
|
|
197
|
+
// Encode to specified format
|
|
198
|
+
std::vector<uchar> encoded;
|
|
199
|
+
EncodeToFormat(alignedImage, encoded, outputFormat, quality, pngOptimize);
|
|
200
|
+
result = VectorToBuffer(env, std::move(encoded));
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
auto encodeEnd = std::chrono::steady_clock::now();
|
|
204
|
+
encodeMs = std::chrono::duration<double, std::milli>(encodeEnd - encodeStart).count();
|
|
205
|
+
|
|
206
|
+
// Create response object
|
|
207
|
+
Napi::Object response = Napi::Object::New(env);
|
|
208
|
+
response.Set("image", result);
|
|
209
|
+
response.Set("timing", MakeTimingJS(env, convertMs, taskMs, encodeMs));
|
|
210
|
+
response.Set("success", Napi::Boolean::New(env, alignmentSuccess));
|
|
211
|
+
|
|
212
|
+
// Add transformation matrix if requested and alignment succeeded
|
|
213
|
+
if (returnMatrix && alignmentSuccess && !transformationMatrix.empty()) {
|
|
214
|
+
Napi::Object matrix = Napi::Object::New(env);
|
|
215
|
+
matrix.Set("dx", Napi::Number::New(env, transformationMatrix.at<float>(0, 2)));
|
|
216
|
+
matrix.Set("dy", Napi::Number::New(env, transformationMatrix.at<float>(1, 2)));
|
|
217
|
+
|
|
218
|
+
// Provide both 2x3 OpenCV matrix and standard 3x3 homogeneous matrix
|
|
219
|
+
Napi::Array matrix2x3 = Napi::Array::New(env, 6);
|
|
220
|
+
matrix2x3.Set(0u, Napi::Number::New(env, transformationMatrix.at<float>(0, 0)));
|
|
221
|
+
matrix2x3.Set(1u, Napi::Number::New(env, transformationMatrix.at<float>(0, 1)));
|
|
222
|
+
matrix2x3.Set(2u, Napi::Number::New(env, transformationMatrix.at<float>(0, 2)));
|
|
223
|
+
matrix2x3.Set(3u, Napi::Number::New(env, transformationMatrix.at<float>(1, 0)));
|
|
224
|
+
matrix2x3.Set(4u, Napi::Number::New(env, transformationMatrix.at<float>(1, 1)));
|
|
225
|
+
matrix2x3.Set(5u, Napi::Number::New(env, transformationMatrix.at<float>(1, 2)));
|
|
226
|
+
|
|
227
|
+
// Standard 3x3 homogeneous transformation matrix
|
|
228
|
+
Napi::Array matrix3x3 = Napi::Array::New(env, 9);
|
|
229
|
+
matrix3x3.Set(0u, Napi::Number::New(env, transformationMatrix.at<float>(0, 0))); // m00
|
|
230
|
+
matrix3x3.Set(1u, Napi::Number::New(env, transformationMatrix.at<float>(0, 1))); // m01
|
|
231
|
+
matrix3x3.Set(2u, Napi::Number::New(env, transformationMatrix.at<float>(0, 2))); // m02 (dx)
|
|
232
|
+
matrix3x3.Set(3u, Napi::Number::New(env, transformationMatrix.at<float>(1, 0))); // m10
|
|
233
|
+
matrix3x3.Set(4u, Napi::Number::New(env, transformationMatrix.at<float>(1, 1))); // m11
|
|
234
|
+
matrix3x3.Set(5u, Napi::Number::New(env, transformationMatrix.at<float>(1, 2))); // m12 (dy)
|
|
235
|
+
matrix3x3.Set(6u, Napi::Number::New(env, 0.0)); // m20 (always 0)
|
|
236
|
+
matrix3x3.Set(7u, Napi::Number::New(env, 0.0)); // m21 (always 0)
|
|
237
|
+
matrix3x3.Set(8u, Napi::Number::New(env, 1.0)); // m22 (always 1)
|
|
238
|
+
|
|
239
|
+
matrix.Set("matrix2x3", matrix2x3); // OpenCV format [a,b,dx,c,d,dy]
|
|
240
|
+
matrix.Set("matrix3x3", matrix3x3); // Standard homogeneous format
|
|
241
|
+
matrix.Set("transform", matrix3x3); // Alias for backward compatibility
|
|
242
|
+
response.Set("transformMatrix", matrix);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
// Add transformed polygon(s) if provided and transformed
|
|
246
|
+
if (hasPolygon && alignmentSuccess) {
|
|
247
|
+
if (isPolygonArray) {
|
|
248
|
+
// Return array of polygons
|
|
249
|
+
if (!transformedPolygons.empty()) {
|
|
250
|
+
Napi::Array polygonsArray = Napi::Array::New(env, transformedPolygons.size());
|
|
251
|
+
for (size_t i = 0; i < transformedPolygons.size(); i++) {
|
|
252
|
+
const auto& polygon = transformedPolygons[i];
|
|
253
|
+
Napi::Array polygonArray = Napi::Array::New(env, polygon.size());
|
|
254
|
+
for (size_t j = 0; j < polygon.size(); j++) {
|
|
255
|
+
Napi::Array point = Napi::Array::New(env, 2);
|
|
256
|
+
point.Set(0u, Napi::Number::New(env, polygon[j].x));
|
|
257
|
+
point.Set(1u, Napi::Number::New(env, polygon[j].y));
|
|
258
|
+
polygonArray.Set(static_cast<uint32_t>(j), point);
|
|
259
|
+
}
|
|
260
|
+
polygonsArray.Set(static_cast<uint32_t>(i), polygonArray);
|
|
261
|
+
}
|
|
262
|
+
response.Set("transformedPolygons", polygonsArray);
|
|
263
|
+
}
|
|
264
|
+
} else {
|
|
265
|
+
// Return single polygon
|
|
266
|
+
if (!transformedPolygon.empty()) {
|
|
267
|
+
Napi::Array polygonArray = Napi::Array::New(env, transformedPolygon.size());
|
|
268
|
+
for (size_t i = 0; i < transformedPolygon.size(); i++) {
|
|
269
|
+
Napi::Array point = Napi::Array::New(env, 2);
|
|
270
|
+
point.Set(0u, Napi::Number::New(env, transformedPolygon[i].x));
|
|
271
|
+
point.Set(1u, Napi::Number::New(env, transformedPolygon[i].y));
|
|
272
|
+
polygonArray.Set(static_cast<uint32_t>(i), point);
|
|
273
|
+
}
|
|
274
|
+
response.Set("transformedPolygon", polygonArray);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
Callback().Call({env.Null(), response});
|
|
280
|
+
|
|
281
|
+
} catch (const std::exception& e) {
|
|
282
|
+
Callback().Call({Napi::Error::New(env, e.what()).Value()});
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
private:
|
|
287
|
+
// Input parameters
|
|
288
|
+
double scale;
|
|
289
|
+
int maxIterations;
|
|
290
|
+
double terminationEps;
|
|
291
|
+
std::string outputFormat;
|
|
292
|
+
int quality;
|
|
293
|
+
bool pngOptimize;
|
|
294
|
+
bool returnMatrix;
|
|
295
|
+
|
|
296
|
+
// Image data
|
|
297
|
+
cv::Mat referenceMat, targetMat, alignedImage;
|
|
298
|
+
cv::Mat transformationMatrix;
|
|
299
|
+
std::string referenceChannelOrder, targetChannelOrder, outputChannelOrder;
|
|
300
|
+
bool alignmentSuccess;
|
|
301
|
+
|
|
302
|
+
// Polygon data
|
|
303
|
+
bool hasPolygon;
|
|
304
|
+
bool isPolygonArray;
|
|
305
|
+
std::vector<cv::Point2f> originalPolygon; // For single polygon
|
|
306
|
+
std::vector<cv::Point2f> transformedPolygon; // For single polygon
|
|
307
|
+
std::vector<std::vector<cv::Point2f>> originalPolygons; // For array of polygons
|
|
308
|
+
std::vector<std::vector<cv::Point2f>> transformedPolygons; // For array of polygons
|
|
309
|
+
|
|
310
|
+
// Timing
|
|
311
|
+
double convertMs = 0.0;
|
|
312
|
+
double taskMs = 0.0;
|
|
313
|
+
double encodeMs = 0.0;
|
|
314
|
+
|
|
315
|
+
// Helper to detect channel format
|
|
316
|
+
std::string DetectChannelFormat(const Napi::Value& jsImg, const cv::Mat& mat) {
|
|
317
|
+
if (jsImg.IsObject() && !jsImg.IsBuffer()) {
|
|
318
|
+
Napi::Object obj = jsImg.As<Napi::Object>();
|
|
319
|
+
if (obj.Has("colorSpace")) {
|
|
320
|
+
return obj.Get("colorSpace").As<Napi::String>().Utf8Value();
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
// Default based on channel count
|
|
324
|
+
const int channels = mat.channels();
|
|
325
|
+
return (channels == 4) ? "BGRA" : (channels == 3) ? "BGR" : "GRAY";
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
// Convert image to grayscale
|
|
329
|
+
void ConvertToGray(const cv::Mat& src, cv::Mat& gray) {
|
|
330
|
+
if (src.channels() == 1) {
|
|
331
|
+
gray = src;
|
|
332
|
+
} else if (src.channels() == 3) {
|
|
333
|
+
cv::cvtColor(src, gray, cv::COLOR_BGR2GRAY);
|
|
334
|
+
} else if (src.channels() == 4) {
|
|
335
|
+
cv::cvtColor(src, gray, cv::COLOR_BGRA2GRAY);
|
|
336
|
+
} else {
|
|
337
|
+
throw std::runtime_error("Unsupported image format for alignment");
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
// Prepare grayscale image for ECC by normalizing mean/variance
|
|
342
|
+
bool PrepareAlignmentImage(const cv::Mat& src, cv::Mat& dst) {
|
|
343
|
+
constexpr double eps = 1e-6;
|
|
344
|
+
|
|
345
|
+
src.convertTo(dst, CV_32F);
|
|
346
|
+
cv::Scalar mean, stddev;
|
|
347
|
+
cv::meanStdDev(dst, mean, stddev);
|
|
348
|
+
|
|
349
|
+
dst -= static_cast<float>(mean[0]);
|
|
350
|
+
|
|
351
|
+
double stdVal = stddev[0];
|
|
352
|
+
if (!std::isfinite(stdVal) || stdVal <= eps) {
|
|
353
|
+
return false;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
float scaleFactor = static_cast<float>(1.0 / stdVal);
|
|
357
|
+
dst *= scaleFactor;
|
|
358
|
+
return true;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
// Find transformation matrix using ECC algorithm
|
|
362
|
+
bool FindTransformation(const cv::Mat& refGray, const cv::Mat& targetGray, cv::Mat& transformMatrix) {
|
|
363
|
+
try {
|
|
364
|
+
int height = refGray.rows;
|
|
365
|
+
int width = refGray.cols;
|
|
366
|
+
|
|
367
|
+
// Downsample images for faster alignment
|
|
368
|
+
int scaledWidth = std::max(1, static_cast<int>(std::round(width * scale)));
|
|
369
|
+
int scaledHeight = std::max(1, static_cast<int>(std::round(height * scale)));
|
|
370
|
+
|
|
371
|
+
cv::Mat refSmall, targetSmall;
|
|
372
|
+
cv::resize(refGray, refSmall, cv::Size(scaledWidth, scaledHeight));
|
|
373
|
+
cv::resize(targetGray, targetSmall, cv::Size(scaledWidth, scaledHeight));
|
|
374
|
+
|
|
375
|
+
// Normalize exposure/contrast differences so ECC is less sensitive to lighting changes
|
|
376
|
+
cv::Mat refPrepared, targetPrepared;
|
|
377
|
+
bool refPreparedOk = PrepareAlignmentImage(refSmall, refPrepared);
|
|
378
|
+
bool targetPreparedOk = PrepareAlignmentImage(targetSmall, targetPrepared);
|
|
379
|
+
|
|
380
|
+
if (!refPreparedOk || !targetPreparedOk) {
|
|
381
|
+
// Fallback to min-max normalization when variance is extremely low
|
|
382
|
+
cv::normalize(refPrepared, refPrepared, 0.0f, 1.0f, cv::NORM_MINMAX);
|
|
383
|
+
cv::normalize(targetPrepared, targetPrepared, 0.0f, 1.0f, cv::NORM_MINMAX);
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
// Initialize transformation matrix for translation-only motion
|
|
387
|
+
transformMatrix = cv::Mat::eye(2, 3, CV_32F);
|
|
388
|
+
|
|
389
|
+
// Set up termination criteria
|
|
390
|
+
cv::TermCriteria criteria(cv::TermCriteria::COUNT | cv::TermCriteria::EPS,
|
|
391
|
+
maxIterations, terminationEps);
|
|
392
|
+
|
|
393
|
+
// Find transformation using ECC
|
|
394
|
+
double correlation = cv::findTransformECC(refPrepared, targetPrepared, transformMatrix,
|
|
395
|
+
cv::MOTION_TRANSLATION, criteria);
|
|
396
|
+
|
|
397
|
+
// Scale transformation matrix back to full resolution
|
|
398
|
+
transformMatrix.at<float>(0, 2) /= scale; // x translation
|
|
399
|
+
transformMatrix.at<float>(1, 2) /= scale; // y translation
|
|
400
|
+
|
|
401
|
+
return correlation > 0.1; // Simple success threshold
|
|
402
|
+
|
|
403
|
+
} catch (const cv::Exception& e) {
|
|
404
|
+
return false;
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
// Apply transformation to image
|
|
409
|
+
void ApplyTransformation(const cv::Mat& target, const cv::Mat& transformMatrix, const cv::Size& size) {
|
|
410
|
+
try {
|
|
411
|
+
cv::warpAffine(target, alignedImage, transformMatrix, size,
|
|
412
|
+
cv::INTER_LINEAR + cv::WARP_INVERSE_MAP);
|
|
413
|
+
} catch (const cv::Exception& e) {
|
|
414
|
+
// If transformation fails, use original
|
|
415
|
+
alignedImage = target;
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
// Transform single polygon coordinates
|
|
420
|
+
void TransformPolygon(const std::vector<cv::Point2f>& polygon, const cv::Mat& transformMatrix, int width, int height) {
|
|
421
|
+
transformedPolygon.clear();
|
|
422
|
+
transformedPolygon.reserve(polygon.size());
|
|
423
|
+
|
|
424
|
+
for (const auto& point : polygon) {
|
|
425
|
+
// Convert from normalized (0-1) to pixel coordinates
|
|
426
|
+
float px = point.x * width;
|
|
427
|
+
float py = point.y * height;
|
|
428
|
+
|
|
429
|
+
// Apply the transformation matrix
|
|
430
|
+
// Since we're using WARP_INVERSE_MAP, we need to apply the inverse transform
|
|
431
|
+
// For translation-only (2x3 matrix with identity rotation), the inverse is just negating the translation
|
|
432
|
+
float transformedX = px - transformMatrix.at<float>(0, 2);
|
|
433
|
+
float transformedY = py - transformMatrix.at<float>(1, 2);
|
|
434
|
+
|
|
435
|
+
// Convert back to normalized coordinates
|
|
436
|
+
float normalizedX = transformedX / width;
|
|
437
|
+
float normalizedY = transformedY / height;
|
|
438
|
+
|
|
439
|
+
// Clamp to valid range [0, 1]
|
|
440
|
+
normalizedX = std::max(0.0f, std::min(1.0f, normalizedX));
|
|
441
|
+
normalizedY = std::max(0.0f, std::min(1.0f, normalizedY));
|
|
442
|
+
|
|
443
|
+
transformedPolygon.push_back(cv::Point2f(normalizedX, normalizedY));
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
// Transform multiple polygons coordinates
|
|
448
|
+
void TransformPolygons(const std::vector<std::vector<cv::Point2f>>& polygons, const cv::Mat& transformMatrix, int width, int height) {
|
|
449
|
+
transformedPolygons.clear();
|
|
450
|
+
transformedPolygons.reserve(polygons.size());
|
|
451
|
+
|
|
452
|
+
for (const auto& polygon : polygons) {
|
|
453
|
+
std::vector<cv::Point2f> transformedPoly;
|
|
454
|
+
transformedPoly.reserve(polygon.size());
|
|
455
|
+
|
|
456
|
+
for (const auto& point : polygon) {
|
|
457
|
+
// Convert from normalized (0-1) to pixel coordinates
|
|
458
|
+
float px = point.x * width;
|
|
459
|
+
float py = point.y * height;
|
|
460
|
+
|
|
461
|
+
// Apply the transformation matrix
|
|
462
|
+
// Since we're using WARP_INVERSE_MAP, we need to apply the inverse transform
|
|
463
|
+
// For translation-only (2x3 matrix with identity rotation), the inverse is just negating the translation
|
|
464
|
+
float transformedX = px - transformMatrix.at<float>(0, 2);
|
|
465
|
+
float transformedY = py - transformMatrix.at<float>(1, 2);
|
|
466
|
+
|
|
467
|
+
// Convert back to normalized coordinates
|
|
468
|
+
float normalizedX = transformedX / width;
|
|
469
|
+
float normalizedY = transformedY / height;
|
|
470
|
+
|
|
471
|
+
// Clamp to valid range [0, 1]
|
|
472
|
+
normalizedX = std::max(0.0f, std::min(1.0f, normalizedX));
|
|
473
|
+
normalizedY = std::max(0.0f, std::min(1.0f, normalizedY));
|
|
474
|
+
|
|
475
|
+
transformedPoly.push_back(cv::Point2f(normalizedX, normalizedY));
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
transformedPolygons.push_back(transformedPoly);
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
};
|
|
482
|
+
|
|
483
|
+
// Main function exported to Node.js
|
|
484
|
+
Napi::Value ImageAlign(const Napi::CallbackInfo& info) {
|
|
485
|
+
Napi::Env env = info.Env();
|
|
486
|
+
|
|
487
|
+
// Validate arguments - expect callback as last argument
|
|
488
|
+
if (info.Length() < 3 || info.Length() > 11 || !info[info.Length() - 1].IsFunction()) {
|
|
489
|
+
Napi::TypeError::New(env, "imageAlign(referenceImage, targetImage, [scale], [maxIterations], [terminationEps], [outputFormat], [quality], [pngOptimize], [returnMatrix], [polygon], callback)")
|
|
490
|
+
.ThrowAsJavaScriptException();
|
|
491
|
+
return env.Null();
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
// Extract required arguments
|
|
495
|
+
Napi::Value referenceImage = info[0];
|
|
496
|
+
Napi::Value targetImage = info[1];
|
|
497
|
+
Napi::Function callback = info[info.Length() - 1].As<Napi::Function>();
|
|
498
|
+
|
|
499
|
+
// Handle optional parameters with defaults
|
|
500
|
+
double scale = 0.2; // Ultra-fast preset default
|
|
501
|
+
int maxIterations = 10;
|
|
502
|
+
double terminationEps = 1e-1;
|
|
503
|
+
std::string outputFormat = "raw";
|
|
504
|
+
int quality = 90;
|
|
505
|
+
bool pngOptimize = false;
|
|
506
|
+
bool returnMatrix = false;
|
|
507
|
+
Napi::Value polygon = env.Null();
|
|
508
|
+
size_t cbIdx = 2;
|
|
509
|
+
|
|
510
|
+
// Parse optional parameters (before callback)
|
|
511
|
+
if (info.Length() >= 4) {
|
|
512
|
+
scale = info[2].As<Napi::Number>().DoubleValue();
|
|
513
|
+
cbIdx = 3;
|
|
514
|
+
}
|
|
515
|
+
if (info.Length() >= 5) {
|
|
516
|
+
maxIterations = info[3].As<Napi::Number>().Int32Value();
|
|
517
|
+
cbIdx = 4;
|
|
518
|
+
}
|
|
519
|
+
if (info.Length() >= 6) {
|
|
520
|
+
terminationEps = info[4].As<Napi::Number>().DoubleValue();
|
|
521
|
+
cbIdx = 5;
|
|
522
|
+
}
|
|
523
|
+
if (info.Length() >= 7) {
|
|
524
|
+
outputFormat = info[5].As<Napi::String>().Utf8Value();
|
|
525
|
+
cbIdx = 6;
|
|
526
|
+
}
|
|
527
|
+
if (info.Length() >= 8) {
|
|
528
|
+
quality = info[6].As<Napi::Number>().Int32Value();
|
|
529
|
+
cbIdx = 7;
|
|
530
|
+
}
|
|
531
|
+
if (info.Length() >= 9) {
|
|
532
|
+
pngOptimize = info[7].As<Napi::Boolean>().Value();
|
|
533
|
+
cbIdx = 8;
|
|
534
|
+
}
|
|
535
|
+
if (info.Length() >= 10) {
|
|
536
|
+
returnMatrix = info[8].As<Napi::Boolean>().Value();
|
|
537
|
+
cbIdx = 9;
|
|
538
|
+
}
|
|
539
|
+
if (info.Length() >= 11) {
|
|
540
|
+
polygon = info[9];
|
|
541
|
+
cbIdx = 10;
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
// Create and queue worker
|
|
545
|
+
ImageAlignWorker* worker = new ImageAlignWorker(callback, referenceImage, targetImage,
|
|
546
|
+
scale, maxIterations, terminationEps,
|
|
547
|
+
outputFormat, quality, pngOptimize, returnMatrix, polygon);
|
|
548
|
+
worker->Queue();
|
|
549
|
+
|
|
550
|
+
return env.Undefined();
|
|
551
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
#include <napi.h>
|
|
2
|
+
|
|
3
|
+
Napi::Value Resize(const Napi::CallbackInfo& info);
|
|
4
|
+
Napi::Value Rotate(const Napi::CallbackInfo& info);
|
|
5
|
+
Napi::Value Crop(const Napi::CallbackInfo& info);
|
|
6
|
+
Napi::Value Concat(const Napi::CallbackInfo& info);
|
|
7
|
+
Napi::Value Padding(const Napi::CallbackInfo& info);
|
|
8
|
+
Napi::Value Filter(const Napi::CallbackInfo& info);
|
|
9
|
+
Napi::Value Mosaic(const Napi::CallbackInfo& info);
|
|
10
|
+
Napi::Value AdvancedMosaic(const Napi::CallbackInfo& info);
|
|
11
|
+
Napi::Value Blend(const Napi::CallbackInfo& info);
|
|
12
|
+
Napi::Value AddMask(const Napi::CallbackInfo& info);
|
|
13
|
+
Napi::Value AddMasks(const Napi::CallbackInfo& info);
|
|
14
|
+
Napi::Value AddBBs(const Napi::CallbackInfo& info);
|
|
15
|
+
Napi::Value ImageAlign(const Napi::CallbackInfo& info);
|
|
16
|
+
Napi::Value Draw(const Napi::CallbackInfo& info);
|
|
17
|
+
|
|
18
|
+
Napi::Object Init(Napi::Env env, Napi::Object exports) {
|
|
19
|
+
exports.Set(Napi::String::New(env, "resize"), Napi::Function::New(env, Resize));
|
|
20
|
+
exports.Set(Napi::String::New(env, "rotate"), Napi::Function::New(env, Rotate));
|
|
21
|
+
exports.Set(Napi::String::New(env, "crop"), Napi::Function::New(env, Crop));
|
|
22
|
+
exports.Set(Napi::String::New(env, "concat"), Napi::Function::New(env, Concat));
|
|
23
|
+
exports.Set(Napi::String::New(env, "padding"), Napi::Function::New(env, Padding));
|
|
24
|
+
exports.Set(Napi::String::New(env, "filter"), Napi::Function::New(env, Filter));
|
|
25
|
+
exports.Set(Napi::String::New(env, "mosaic"), Napi::Function::New(env, Mosaic));
|
|
26
|
+
exports.Set(Napi::String::New(env, "advancedMosaic"), Napi::Function::New(env, AdvancedMosaic));
|
|
27
|
+
exports.Set(Napi::String::New(env, "blend"), Napi::Function::New(env, Blend));
|
|
28
|
+
exports.Set(Napi::String::New(env, "addMask"), Napi::Function::New(env, AddMask));
|
|
29
|
+
exports.Set(Napi::String::New(env, "addMasks"), Napi::Function::New(env, AddMasks));
|
|
30
|
+
exports.Set(Napi::String::New(env, "addBBs"), Napi::Function::New(env, AddBBs));
|
|
31
|
+
exports.Set(Napi::String::New(env, "imageAlign"), Napi::Function::New(env, ImageAlign));
|
|
32
|
+
exports.Set(Napi::String::New(env, "draw"), Napi::Function::New(env, Draw));
|
|
33
|
+
return exports;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
NODE_API_MODULE(addon, Init)
|