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.
- package/dist/index.js +26 -17
- package/package.json +1 -1
- 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
|
|
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,
|
|
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
|
|
80
|
-
|
|
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
|
+
// Và loại bỏ những vùng quá lớn (ví 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
|
-
|
|
93
|
-
//
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
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
|
-
|
|
106
|
-
|
|
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.
|
|
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
|
|
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,
|
|
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
|
|
106
|
-
|
|
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
|
+
// Và loại bỏ những vùng quá lớn (ví 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
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
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
|
-
|
|
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
|
}
|