rn-opencv-doc-perspective-correction 1.0.18 → 1.0.19

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/dist/index.js CHANGED
@@ -56,7 +56,7 @@ class DocumentScanner {
56
56
  ];
57
57
  }
58
58
  static detectPageCorners(imageBase64) {
59
- var _a, _b;
59
+ var _a, _b, _c;
60
60
  let src = null;
61
61
  let resized = null;
62
62
  let gray = null;
@@ -163,6 +163,7 @@ class DocumentScanner {
163
163
  let foundPts = null;
164
164
  let foundVerts = 0;
165
165
  let foundAngle = 999;
166
+ let foundApproxArea = 0;
166
167
  for (let epPx = 10; epPx <= 120; epPx += 5) {
167
168
  const approx = react_native_fast_opencv_1.OpenCV.createObject(react_native_fast_opencv_1.ObjectType.PointVector);
168
169
  react_native_fast_opencv_1.OpenCV.invoke('approxPolyDP', contour, approx, epPx, true);
@@ -172,25 +173,32 @@ class DocumentScanner {
172
173
  const pts = approxJS.array;
173
174
  const sorted = this.sortCorners([...pts]);
174
175
  const angleRange = this.getAngleRange(sorted);
176
+ // ⚠️ QUAN TRỌNG: Đúng như Python gốc — kiểm tra area của APPROX POLYGON (4 điểm),
177
+ // KHÔNG phải area của raw contour (edge mỏng). Edge contour chỉ có area nhỏ
178
+ // (perimeter * lineWidth) nhưng polygon 4-điểm có area = diện tích tài liệu.
179
+ const approxAreaObj = react_native_fast_opencv_1.OpenCV.invoke('contourArea', approx);
180
+ const approxArea = approxAreaObj ? ((_c = approxAreaObj.value) !== null && _c !== void 0 ? _c : 0) : 0;
175
181
  if (angleRange < foundAngle) {
176
182
  foundAngle = angleRange;
177
183
  foundPts = pts;
178
184
  foundVerts = verts;
185
+ foundApproxArea = approxArea;
179
186
  }
180
- // is_valid_contour: area > MIN_QUAD_AREA_RATIO angle_range < 40
181
- if (metric.area > imgArea * MIN_QUAD_AREA_RATIO && angleRange < 40) {
182
- if (metric.area > bestArea || (metric.area === bestArea && angleRange < bestAngleRange)) {
187
+ // is_valid_contour(approx): approxArea > MIN_QUAD_AREA_RATIO && angleRange < 40
188
+ if (approxArea > imgArea * MIN_QUAD_AREA_RATIO && angleRange < 40) {
189
+ if (approxArea > bestArea || (approxArea === bestArea && angleRange < bestAngleRange)) {
183
190
  bestPoly = pts;
184
- bestArea = metric.area;
191
+ bestArea = approxArea;
185
192
  bestAngleRange = angleRange;
186
193
  }
187
194
  break;
188
195
  }
189
196
  }
190
197
  }
191
- debugCandidates.push(` [C${i}] area=${metric.area.toFixed(0)}px²(${(metric.area / imgArea * 100).toFixed(1)}%) ` +
192
- `foundVerts=${foundVerts} bestAngle=${foundAngle === 999 ? 'N/A' : foundAngle.toFixed(1)}° ` +
193
- `valid=${metric.area > imgArea * MIN_QUAD_AREA_RATIO && foundAngle < 40}`);
198
+ debugCandidates.push(` [C${i}] rawArea=${metric.area.toFixed(0)}px²(${(metric.area / imgArea * 100).toFixed(1)}%) ` +
199
+ `approxArea=${foundApproxArea > 0 ? foundApproxArea.toFixed(0) + 'px²(' + ((foundApproxArea / imgArea * 100).toFixed(1)) + '%)' : 'N/A'} ` +
200
+ `verts=${foundVerts} angle=${foundAngle === 999 ? 'N/A' : foundAngle.toFixed(1)}° ` +
201
+ `valid=${foundApproxArea > imgArea * MIN_QUAD_AREA_RATIO && foundAngle < 40}`);
194
202
  }
195
203
  console.log(`[DocDetect] Candidates:\n${debugCandidates.join('\n')}`);
196
204
  console.log(`[DocDetect] Result: bestPoly=${bestPoly ? 'found' : 'NOT FOUND'} area=${bestArea.toFixed(0)} angleRange=${bestAngleRange === 999 ? 'N/A' : bestAngleRange.toFixed(1)}°`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rn-opencv-doc-perspective-correction",
3
- "version": "1.0.18",
3
+ "version": "1.0.19",
4
4
  "description": "A React Native library for document corner detection and perspective correction using react-native-fast-opencv",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
package/src/index.ts CHANGED
@@ -193,6 +193,7 @@ export class DocumentScanner {
193
193
  let foundPts: Point[] | null = null;
194
194
  let foundVerts = 0;
195
195
  let foundAngle = 999;
196
+ let foundApproxArea = 0;
196
197
 
197
198
  for (let epPx = 10; epPx <= 120; epPx += 5) {
198
199
  const approx = OpenCV.createObject(ObjectType.PointVector);
@@ -205,17 +206,24 @@ export class DocumentScanner {
205
206
  const sorted = this.sortCorners([...pts]);
206
207
  const angleRange = this.getAngleRange(sorted);
207
208
 
209
+ // ⚠️ QUAN TRỌNG: Đúng như Python gốc — kiểm tra area của APPROX POLYGON (4 điểm),
210
+ // KHÔNG phải area của raw contour (edge mỏng). Edge contour chỉ có area nhỏ
211
+ // (perimeter * lineWidth) nhưng polygon 4-điểm có area = diện tích tài liệu.
212
+ const approxAreaObj = OpenCV.invoke('contourArea', approx);
213
+ const approxArea = approxAreaObj ? (approxAreaObj.value ?? 0) : 0;
214
+
208
215
  if (angleRange < foundAngle) {
209
216
  foundAngle = angleRange;
210
217
  foundPts = pts;
211
218
  foundVerts = verts;
219
+ foundApproxArea = approxArea;
212
220
  }
213
221
 
214
- // is_valid_contour: area > MIN_QUAD_AREA_RATIO angle_range < 40
215
- if (metric.area > imgArea * MIN_QUAD_AREA_RATIO && angleRange < 40) {
216
- if (metric.area > bestArea || (metric.area === bestArea && angleRange < bestAngleRange)) {
222
+ // is_valid_contour(approx): approxArea > MIN_QUAD_AREA_RATIO && angleRange < 40
223
+ if (approxArea > imgArea * MIN_QUAD_AREA_RATIO && angleRange < 40) {
224
+ if (approxArea > bestArea || (approxArea === bestArea && angleRange < bestAngleRange)) {
217
225
  bestPoly = pts;
218
- bestArea = metric.area;
226
+ bestArea = approxArea;
219
227
  bestAngleRange = angleRange;
220
228
  }
221
229
  break;
@@ -224,9 +232,10 @@ export class DocumentScanner {
224
232
  }
225
233
 
226
234
  debugCandidates.push(
227
- ` [C${i}] area=${metric.area.toFixed(0)}px²(${(metric.area/imgArea*100).toFixed(1)}%) ` +
228
- `foundVerts=${foundVerts} bestAngle=${foundAngle === 999 ? 'N/A' : foundAngle.toFixed(1)}° ` +
229
- `valid=${metric.area > imgArea * MIN_QUAD_AREA_RATIO && foundAngle < 40}`
235
+ ` [C${i}] rawArea=${metric.area.toFixed(0)}px²(${(metric.area/imgArea*100).toFixed(1)}%) ` +
236
+ `approxArea=${foundApproxArea > 0 ? foundApproxArea.toFixed(0)+'px²('+((foundApproxArea/imgArea*100).toFixed(1))+'%)' : 'N/A'} ` +
237
+ `verts=${foundVerts} angle=${foundAngle === 999 ? 'N/A' : foundAngle.toFixed(1)}° ` +
238
+ `valid=${foundApproxArea > imgArea * MIN_QUAD_AREA_RATIO && foundAngle < 40}`
230
239
  );
231
240
  }
232
241