rn-opencv-doc-perspective-correction 1.0.15 → 1.0.16

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.
Files changed (3) hide show
  1. package/dist/index.js +26 -17
  2. package/package.json +1 -1
  3. package/src/index.ts +29 -21
package/dist/index.js CHANGED
@@ -61,10 +61,10 @@ class DocumentScanner {
61
61
  edges = react_native_fast_opencv_1.OpenCV.createObject(react_native_fast_opencv_1.ObjectType.Mat, 0, 0, react_native_fast_opencv_1.DataTypes.CV_8U);
62
62
  react_native_fast_opencv_1.OpenCV.invoke('Canny', closed, edges, 0, 84);
63
63
  // BƯỚC 5: Tìm khối đa giác liên kết (Contours)
64
- // Lệnh bắt External sẽ hoạt động hoàn hảo khi Canny chỉ thấy 1 nét vành ngoài.
64
+ // Lệnh bắt RETR_LIST (1) để tóm được tài liệu nằm bên trong các viền nền lớn
65
65
  contoursObj = react_native_fast_opencv_1.OpenCV.createObject(react_native_fast_opencv_1.ObjectType.MatVector);
66
66
  hierarchyObj = react_native_fast_opencv_1.OpenCV.createObject(react_native_fast_opencv_1.ObjectType.Mat, 0, 0, react_native_fast_opencv_1.DataTypes.CV_8U);
67
- react_native_fast_opencv_1.OpenCV.invoke('findContoursWithHierarchy', edges, contoursObj, hierarchyObj, 0 /* RETR_EXTERNAL */, 2 /* CHAIN_APPROX_SIMPLE */);
67
+ react_native_fast_opencv_1.OpenCV.invoke('findContoursWithHierarchy', edges, contoursObj, hierarchyObj, 1 /* RETR_LIST */, 2 /* CHAIN_APPROX_SIMPLE */);
68
68
  const contoursJS = react_native_fast_opencv_1.OpenCV.toJSValue(contoursObj);
69
69
  const contoursArray = (contoursJS === null || contoursJS === void 0 ? void 0 : contoursJS.array) || [];
70
70
  const contoursSize = contoursArray.length;
@@ -76,8 +76,10 @@ class DocumentScanner {
76
76
  const contour = react_native_fast_opencv_1.OpenCV.copyObjectFromVector(contoursObj, i);
77
77
  const areaObj = react_native_fast_opencv_1.OpenCV.invoke('contourArea', contour);
78
78
  const area = areaObj ? areaObj.value : 0;
79
- // Mở rộng bộ lọc diện tích: Loại bỏ đi vùng nhiễu vụn vặt (< 5% diện tích mặt quét)
80
- if (area > (targetWidth * targetHeight * 0.05)) {
79
+ // Mở rộng bộ lọc diện tích: Loại bỏ đi vùng nhiễu vụn (< 5% diện tích mặt quét)
80
+ // loại bỏ những vùng quá lớn ( dụ toàn bộ ảnh nền > 98%)
81
+ const imgArea = targetWidth * targetHeight;
82
+ if (area > (imgArea * 0.05) && area < (imgArea * 0.98)) {
81
83
  contourMetrics.push({ index: i, area, contour });
82
84
  }
83
85
  }
@@ -89,21 +91,28 @@ class DocumentScanner {
89
91
  const contour = metric.contour;
90
92
  const periObj = react_native_fast_opencv_1.OpenCV.invoke('arcLength', contour, true);
91
93
  const peri = periObj ? (periObj.value || 0) : 0;
92
- const approx = react_native_fast_opencv_1.OpenCV.createObject(react_native_fast_opencv_1.ObjectType.PointVector);
93
- // Nội suy (approx) giảm bớt gấp khúc với độ dung sai epsilon 0.02 chu vi
94
- react_native_fast_opencv_1.OpenCV.invoke('approxPolyDP', contour, approx, 0.02 * peri, true);
95
- const approxJS = react_native_fast_opencv_1.OpenCV.toJSValue(approx);
96
- // Nếu quy chiếu thành công ra đúng 4 đỉnh
97
- if (approxJS && approxJS.array && approxJS.array.length === 4) {
98
- try {
99
- const isConvex = react_native_fast_opencv_1.OpenCV.invoke('isContourConvex', approx);
100
- const convexValue = (typeof isConvex === 'object' && isConvex !== null) ? isConvex.value : isConvex;
101
- if (convexValue === false) {
102
- continue;
94
+ let found4 = false;
95
+ // Vòng lặp tăng dần độ dung sai Epsilon (từ 2% -> 10% chu vi)
96
+ // Giúp bắt gọn 4 đỉnh kể cả khi đường mép giấy tờ bị nhăn nheo, lởm chởm
97
+ for (let ep = 0.02; ep <= 0.1; ep += 0.02) {
98
+ const approx = react_native_fast_opencv_1.OpenCV.createObject(react_native_fast_opencv_1.ObjectType.PointVector);
99
+ react_native_fast_opencv_1.OpenCV.invoke('approxPolyDP', contour, approx, ep * peri, true);
100
+ const approxJS = react_native_fast_opencv_1.OpenCV.toJSValue(approx);
101
+ // Nếu nội suy thành công ra đúng 4 đỉnh
102
+ if (approxJS && approxJS.array && approxJS.array.length === 4) {
103
+ try {
104
+ const isConvex = react_native_fast_opencv_1.OpenCV.invoke('isContourConvex', approx);
105
+ const convexValue = (typeof isConvex === 'object' && isConvex !== null) ? isConvex.value : isConvex;
106
+ if (convexValue !== false) {
107
+ largestPoly = approxJS.array;
108
+ found4 = true;
109
+ break;
110
+ }
103
111
  }
112
+ catch (convexErr) { }
104
113
  }
105
- catch (convexErr) { }
106
- largestPoly = approxJS.array;
114
+ }
115
+ if (found4) {
107
116
  break;
108
117
  }
109
118
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rn-opencv-doc-perspective-correction",
3
- "version": "1.0.15",
3
+ "version": "1.0.16",
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
@@ -83,10 +83,10 @@ export class DocumentScanner {
83
83
  OpenCV.invoke('Canny', closed, edges, 0, 84);
84
84
 
85
85
  // BƯỚC 5: Tìm khối đa giác liên kết (Contours)
86
- // Lệnh bắt External sẽ hoạt động hoàn hảo khi Canny chỉ thấy 1 nét vành ngoài.
86
+ // Lệnh bắt RETR_LIST (1) để tóm được tài liệu nằm bên trong các viền nền lớn
87
87
  contoursObj = OpenCV.createObject(ObjectType.MatVector);
88
88
  hierarchyObj = OpenCV.createObject(ObjectType.Mat, 0, 0, DataTypes.CV_8U);
89
- OpenCV.invoke('findContoursWithHierarchy', edges, contoursObj, hierarchyObj, 0 /* RETR_EXTERNAL */, 2 /* CHAIN_APPROX_SIMPLE */);
89
+ OpenCV.invoke('findContoursWithHierarchy', edges, contoursObj, hierarchyObj, 1 /* RETR_LIST */, 2 /* CHAIN_APPROX_SIMPLE */);
90
90
 
91
91
  const contoursJS = OpenCV.toJSValue(contoursObj);
92
92
  const contoursArray = contoursJS?.array || [];
@@ -102,8 +102,10 @@ export class DocumentScanner {
102
102
  const areaObj = OpenCV.invoke('contourArea', contour);
103
103
  const area = areaObj ? areaObj.value : 0;
104
104
 
105
- // Mở rộng bộ lọc diện tích: Loại bỏ đi vùng nhiễu vụn vặt (< 5% diện tích mặt quét)
106
- if (area > (targetWidth * targetHeight * 0.05)) {
105
+ // Mở rộng bộ lọc diện tích: Loại bỏ đi vùng nhiễu vụn (< 5% diện tích mặt quét)
106
+ // loại bỏ những vùng quá lớn ( dụ toàn bộ ảnh nền > 98%)
107
+ const imgArea = targetWidth * targetHeight;
108
+ if (area > (imgArea * 0.05) && area < (imgArea * 0.98)) {
107
109
  contourMetrics.push({ index: i, area, contour });
108
110
  }
109
111
  }
@@ -119,25 +121,31 @@ export class DocumentScanner {
119
121
 
120
122
  const periObj = OpenCV.invoke('arcLength', contour, true);
121
123
  const peri = periObj ? (periObj.value || 0) : 0;
122
- const approx = OpenCV.createObject(ObjectType.PointVector);
123
124
 
124
- // Nội suy (approx) giảm bớt gấp khúc với độ dung sai epsilon 0.02 chu vi
125
- OpenCV.invoke('approxPolyDP', contour, approx, 0.02 * peri, true);
126
-
127
- const approxJS = OpenCV.toJSValue(approx);
125
+ let found4 = false;
126
+
127
+ // Vòng lặp tăng dần độ dung sai Epsilon (từ 2% -> 10% chu vi)
128
+ // Giúp bắt gọn 4 đỉnh kể cả khi đường mép giấy tờ bị nhăn nheo, lởm chởm
129
+ for (let ep = 0.02; ep <= 0.1; ep += 0.02) {
130
+ const approx = OpenCV.createObject(ObjectType.PointVector);
131
+ OpenCV.invoke('approxPolyDP', contour, approx, ep * peri, true);
132
+ const approxJS = OpenCV.toJSValue(approx);
133
+
134
+ // Nếu nội suy thành công ra đúng 4 đỉnh
135
+ if (approxJS && approxJS.array && approxJS.array.length === 4) {
136
+ try {
137
+ const isConvex = OpenCV.invoke('isContourConvex', approx);
138
+ const convexValue = (typeof isConvex === 'object' && isConvex !== null) ? isConvex.value : isConvex;
139
+ if (convexValue !== false) {
140
+ largestPoly = approxJS.array as Point[];
141
+ found4 = true;
142
+ break;
143
+ }
144
+ } catch (convexErr) {}
145
+ }
146
+ }
128
147
 
129
- // Nếu quy chiếu thành công ra đúng 4 đỉnh
130
- if (approxJS && approxJS.array && approxJS.array.length === 4) {
131
-
132
- try {
133
- const isConvex = OpenCV.invoke('isContourConvex', approx);
134
- const convexValue = (typeof isConvex === 'object' && isConvex !== null) ? isConvex.value : isConvex;
135
- if (convexValue === false) {
136
- continue;
137
- }
138
- } catch (convexErr) {}
139
-
140
- largestPoly = approxJS.array as Point[];
148
+ if (found4) {
141
149
  break;
142
150
  }
143
151
  }