rn-opencv-doc-perspective-correction 1.0.2 → 1.0.4
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 +25 -25
- package/package.json +1 -1
- package/src/index.ts +30 -30
package/dist/index.js
CHANGED
|
@@ -35,15 +35,15 @@ class DocumentScanner {
|
|
|
35
35
|
let hierarchyObj = null;
|
|
36
36
|
try {
|
|
37
37
|
src = react_native_fast_opencv_1.OpenCV.base64ToMat(imageBase64);
|
|
38
|
-
gray = react_native_fast_opencv_1.OpenCV.
|
|
39
|
-
blurred = react_native_fast_opencv_1.OpenCV.
|
|
40
|
-
edges = react_native_fast_opencv_1.OpenCV.
|
|
41
|
-
react_native_fast_opencv_1.OpenCV.invoke('cvtColor', src, gray,
|
|
42
|
-
const ksize = react_native_fast_opencv_1.OpenCV.createObject(
|
|
38
|
+
gray = react_native_fast_opencv_1.OpenCV.createObject(react_native_fast_opencv_1.ObjectType.Mat, 0, 0, react_native_fast_opencv_1.DataTypes.CV_8U);
|
|
39
|
+
blurred = react_native_fast_opencv_1.OpenCV.createObject(react_native_fast_opencv_1.ObjectType.Mat, 0, 0, react_native_fast_opencv_1.DataTypes.CV_8U);
|
|
40
|
+
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);
|
|
41
|
+
react_native_fast_opencv_1.OpenCV.invoke('cvtColor', src, gray, react_native_fast_opencv_1.ColorConversionCodes.COLOR_BGR2GRAY);
|
|
42
|
+
const ksize = react_native_fast_opencv_1.OpenCV.createObject(react_native_fast_opencv_1.ObjectType.Size, 5, 5);
|
|
43
43
|
react_native_fast_opencv_1.OpenCV.invoke('GaussianBlur', gray, blurred, ksize, 0);
|
|
44
44
|
react_native_fast_opencv_1.OpenCV.invoke('Canny', blurred, edges, 75, 200, 3, false);
|
|
45
|
-
contoursObj = react_native_fast_opencv_1.OpenCV.createObject(
|
|
46
|
-
hierarchyObj = react_native_fast_opencv_1.OpenCV.
|
|
45
|
+
contoursObj = react_native_fast_opencv_1.OpenCV.createObject(react_native_fast_opencv_1.ObjectType.MatVector);
|
|
46
|
+
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);
|
|
47
47
|
react_native_fast_opencv_1.OpenCV.invoke('findContours', edges, contoursObj, hierarchyObj, 1, 2);
|
|
48
48
|
const contoursSize = react_native_fast_opencv_1.OpenCV.invoke('size', contoursObj) || 0;
|
|
49
49
|
let maxArea = 0;
|
|
@@ -53,22 +53,12 @@ class DocumentScanner {
|
|
|
53
53
|
const area = react_native_fast_opencv_1.OpenCV.invoke('contourArea', contour);
|
|
54
54
|
if (area > maxArea) {
|
|
55
55
|
const peri = react_native_fast_opencv_1.OpenCV.invoke('arcLength', contour, true);
|
|
56
|
-
const approx = react_native_fast_opencv_1.OpenCV.
|
|
56
|
+
const approx = react_native_fast_opencv_1.OpenCV.createObject(react_native_fast_opencv_1.ObjectType.PointVector);
|
|
57
57
|
react_native_fast_opencv_1.OpenCV.invoke('approxPolyDP', contour, approx, 0.02 * peri, true);
|
|
58
|
-
const
|
|
59
|
-
if (
|
|
58
|
+
const approxJS = react_native_fast_opencv_1.OpenCV.toJSValue(approx);
|
|
59
|
+
if (approxJS && approxJS.array && approxJS.array.length === 4) {
|
|
60
60
|
maxArea = area;
|
|
61
|
-
|
|
62
|
-
for (let v = 0; v < 4; v++) {
|
|
63
|
-
try {
|
|
64
|
-
const pt = react_native_fast_opencv_1.OpenCV.invoke('row', approx, v);
|
|
65
|
-
if (pt && typeof pt === 'object' && 'x' in pt)
|
|
66
|
-
points.push(pt);
|
|
67
|
-
}
|
|
68
|
-
catch (err) { }
|
|
69
|
-
}
|
|
70
|
-
if (points.length === 4)
|
|
71
|
-
largestPoly = points;
|
|
61
|
+
largestPoly = approxJS.array;
|
|
72
62
|
}
|
|
73
63
|
}
|
|
74
64
|
}
|
|
@@ -105,11 +95,21 @@ class DocumentScanner {
|
|
|
105
95
|
if (maxWidth === 0 || maxHeight === 0)
|
|
106
96
|
return undefined;
|
|
107
97
|
src = react_native_fast_opencv_1.OpenCV.base64ToMat(imageBase64);
|
|
108
|
-
dst = react_native_fast_opencv_1.OpenCV.
|
|
109
|
-
const srcPoints = react_native_fast_opencv_1.OpenCV.createObject(
|
|
110
|
-
|
|
98
|
+
dst = react_native_fast_opencv_1.OpenCV.createObject(react_native_fast_opencv_1.ObjectType.Mat, 0, 0, react_native_fast_opencv_1.DataTypes.CV_8UC3);
|
|
99
|
+
const srcPoints = react_native_fast_opencv_1.OpenCV.createObject(react_native_fast_opencv_1.ObjectType.Point2fVector, [
|
|
100
|
+
react_native_fast_opencv_1.OpenCV.createObject(react_native_fast_opencv_1.ObjectType.Point2f, tl.x, tl.y),
|
|
101
|
+
react_native_fast_opencv_1.OpenCV.createObject(react_native_fast_opencv_1.ObjectType.Point2f, tr.x, tr.y),
|
|
102
|
+
react_native_fast_opencv_1.OpenCV.createObject(react_native_fast_opencv_1.ObjectType.Point2f, br.x, br.y),
|
|
103
|
+
react_native_fast_opencv_1.OpenCV.createObject(react_native_fast_opencv_1.ObjectType.Point2f, bl.x, bl.y)
|
|
104
|
+
]);
|
|
105
|
+
const dstPoints = react_native_fast_opencv_1.OpenCV.createObject(react_native_fast_opencv_1.ObjectType.Point2fVector, [
|
|
106
|
+
react_native_fast_opencv_1.OpenCV.createObject(react_native_fast_opencv_1.ObjectType.Point2f, 0, 0),
|
|
107
|
+
react_native_fast_opencv_1.OpenCV.createObject(react_native_fast_opencv_1.ObjectType.Point2f, maxWidth - 1, 0),
|
|
108
|
+
react_native_fast_opencv_1.OpenCV.createObject(react_native_fast_opencv_1.ObjectType.Point2f, maxWidth - 1, maxHeight - 1),
|
|
109
|
+
react_native_fast_opencv_1.OpenCV.createObject(react_native_fast_opencv_1.ObjectType.Point2f, 0, maxHeight - 1)
|
|
110
|
+
]);
|
|
111
111
|
const perspectiveMatrix = react_native_fast_opencv_1.OpenCV.invoke('getPerspectiveTransform', srcPoints, dstPoints);
|
|
112
|
-
const size = react_native_fast_opencv_1.OpenCV.createObject(
|
|
112
|
+
const size = react_native_fast_opencv_1.OpenCV.createObject(react_native_fast_opencv_1.ObjectType.Size, maxWidth, maxHeight);
|
|
113
113
|
react_native_fast_opencv_1.OpenCV.invoke('warpPerspective', src, dst, perspectiveMatrix, size);
|
|
114
114
|
return react_native_fast_opencv_1.OpenCV.invoke('toBase64', dst);
|
|
115
115
|
}
|
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.4",
|
|
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
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// @ts-nocheck
|
|
2
|
-
import { OpenCV, OpenCVMat } from 'react-native-fast-opencv';
|
|
2
|
+
import { OpenCV, OpenCVMat, ObjectType, DataTypes, ColorConversionCodes } from 'react-native-fast-opencv';
|
|
3
3
|
|
|
4
4
|
export type Point = { x: number; y: number };
|
|
5
5
|
|
|
@@ -42,19 +42,19 @@ export class DocumentScanner {
|
|
|
42
42
|
|
|
43
43
|
try {
|
|
44
44
|
src = OpenCV.base64ToMat(imageBase64);
|
|
45
|
-
gray = OpenCV.
|
|
46
|
-
blurred = OpenCV.
|
|
47
|
-
edges = OpenCV.
|
|
45
|
+
gray = OpenCV.createObject(ObjectType.Mat, 0, 0, DataTypes.CV_8U);
|
|
46
|
+
blurred = OpenCV.createObject(ObjectType.Mat, 0, 0, DataTypes.CV_8U);
|
|
47
|
+
edges = OpenCV.createObject(ObjectType.Mat, 0, 0, DataTypes.CV_8U);
|
|
48
48
|
|
|
49
|
-
OpenCV.invoke('cvtColor', src, gray,
|
|
49
|
+
OpenCV.invoke('cvtColor', src, gray, ColorConversionCodes.COLOR_BGR2GRAY);
|
|
50
50
|
|
|
51
|
-
const ksize = OpenCV.createObject(
|
|
51
|
+
const ksize = OpenCV.createObject(ObjectType.Size, 5, 5);
|
|
52
52
|
OpenCV.invoke('GaussianBlur', gray, blurred, ksize, 0);
|
|
53
53
|
|
|
54
54
|
OpenCV.invoke('Canny', blurred, edges, 75, 200, 3, false);
|
|
55
55
|
|
|
56
|
-
contoursObj = OpenCV.createObject(
|
|
57
|
-
hierarchyObj = OpenCV.
|
|
56
|
+
contoursObj = OpenCV.createObject(ObjectType.MatVector);
|
|
57
|
+
hierarchyObj = OpenCV.createObject(ObjectType.Mat, 0, 0, DataTypes.CV_8U);
|
|
58
58
|
|
|
59
59
|
OpenCV.invoke('findContours', edges, contoursObj, hierarchyObj, 1, 2);
|
|
60
60
|
|
|
@@ -68,21 +68,13 @@ export class DocumentScanner {
|
|
|
68
68
|
|
|
69
69
|
if (area > maxArea) {
|
|
70
70
|
const peri = OpenCV.invoke('arcLength', contour, true);
|
|
71
|
-
const approx = OpenCV.
|
|
71
|
+
const approx = OpenCV.createObject(ObjectType.PointVector);
|
|
72
72
|
OpenCV.invoke('approxPolyDP', contour, approx, 0.02 * peri, true);
|
|
73
73
|
|
|
74
|
-
const
|
|
75
|
-
if (
|
|
74
|
+
const approxJS = OpenCV.toJSValue(approx);
|
|
75
|
+
if (approxJS && approxJS.array && approxJS.array.length === 4) {
|
|
76
76
|
maxArea = area;
|
|
77
|
-
|
|
78
|
-
const points: Point[] = [];
|
|
79
|
-
for (let v = 0; v < 4; v++) {
|
|
80
|
-
try {
|
|
81
|
-
const pt = OpenCV.invoke('row', approx, v);
|
|
82
|
-
if (pt && typeof pt === 'object' && 'x' in pt) points.push(pt as Point);
|
|
83
|
-
} catch (err) { }
|
|
84
|
-
}
|
|
85
|
-
if (points.length === 4) largestPoly = points;
|
|
77
|
+
largestPoly = approxJS.array as Point[];
|
|
86
78
|
}
|
|
87
79
|
}
|
|
88
80
|
}
|
|
@@ -123,20 +115,28 @@ export class DocumentScanner {
|
|
|
123
115
|
if (maxWidth === 0 || maxHeight === 0) return undefined;
|
|
124
116
|
|
|
125
117
|
src = OpenCV.base64ToMat(imageBase64);
|
|
126
|
-
dst = OpenCV.
|
|
127
|
-
|
|
128
|
-
const srcPoints = OpenCV.createObject(
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
118
|
+
dst = OpenCV.createObject(ObjectType.Mat, 0, 0, DataTypes.CV_8UC3);
|
|
119
|
+
|
|
120
|
+
const srcPoints = OpenCV.createObject(ObjectType.Point2fVector,
|
|
121
|
+
[
|
|
122
|
+
OpenCV.createObject(ObjectType.Point2f, tl.x, tl.y),
|
|
123
|
+
OpenCV.createObject(ObjectType.Point2f, tr.x, tr.y),
|
|
124
|
+
OpenCV.createObject(ObjectType.Point2f, br.x, br.y),
|
|
125
|
+
OpenCV.createObject(ObjectType.Point2f, bl.x, bl.y)
|
|
126
|
+
]
|
|
127
|
+
);
|
|
128
|
+
const dstPoints = OpenCV.createObject(ObjectType.Point2fVector,
|
|
129
|
+
[
|
|
130
|
+
OpenCV.createObject(ObjectType.Point2f, 0, 0),
|
|
131
|
+
OpenCV.createObject(ObjectType.Point2f, maxWidth - 1, 0),
|
|
132
|
+
OpenCV.createObject(ObjectType.Point2f, maxWidth - 1, maxHeight - 1),
|
|
133
|
+
OpenCV.createObject(ObjectType.Point2f, 0, maxHeight - 1)
|
|
134
|
+
]
|
|
135
135
|
);
|
|
136
136
|
|
|
137
137
|
const perspectiveMatrix = OpenCV.invoke('getPerspectiveTransform', srcPoints, dstPoints);
|
|
138
138
|
|
|
139
|
-
const size = OpenCV.createObject(
|
|
139
|
+
const size = OpenCV.createObject(ObjectType.Size, maxWidth, maxHeight);
|
|
140
140
|
OpenCV.invoke('warpPerspective', src, dst, perspectiveMatrix, size);
|
|
141
141
|
|
|
142
142
|
return OpenCV.invoke('toBase64', dst);
|