Skip to content

Commit 7b1d60e

Browse files
lennartalffrainyl
andauthored
added API for approxPolyN (#362)
* added API for approxPolyN * upgrade to windows-2022 in coverage CI. * bump dartcv to 4.11.0.4 * added a unit test for approxPolyN * updated CHANGELOGs for upcoming versions * impl async version of ApproxPolyN, more test --------- Co-authored-by: rainyl <[email protected]>
1 parent 271bb3f commit 7b1d60e

File tree

13 files changed

+154
-14
lines changed

13 files changed

+154
-14
lines changed

.github/workflows/coverage.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ on:
1515
jobs:
1616
windows:
1717
name: windows
18-
runs-on: windows-2019
18+
runs-on: windows-2022
1919
steps:
2020
- uses: actions/checkout@v4
2121
- uses: robinraju/[email protected]
@@ -29,7 +29,7 @@ jobs:
2929
with:
3030
repository: "rainyl/dartcv"
3131
latest: true
32-
fileName: "libdartcv-windows-x64-vs2019.tar.gz"
32+
fileName: "libdartcv-windows-x64-vs2022.tar.gz"
3333
out-file-path: "packages/dartcv/libdartcv"
3434
extract: true
3535
- uses: subosito/flutter-action@v2

packages/dartcv/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 1.1.5
2+
3+
* added `approxPolyN`.
4+
15
## 1.1.4
26

37
* remove deprecated `MatType.toInt()`, should use MatType.value instead

packages/dartcv/Makefile

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
.PHONY: ffigen ffigen_test clone_repo clean_repo
1+
.PHONY: ffigen ffigen_test clone_repo clean_repo test test_coverage
22

3+
DART_EXEC := $(abspath $(dir $(shell which dart)))/cache/dart-sdk/bin/dart
4+
UNAME_S := $(shell uname -s)
35

46
ffigen:
57
dart run ffigen --config ffigen/ffigen_const.yaml
@@ -32,7 +34,15 @@ ffigen_test: clone_repo
3234
dart run ffigen --config ffigen/ffigen_core.yaml
3335

3436
test:
35-
dart test
37+
ifeq ($(UNAME_S),Darwin)
38+
DYLD_LIBRARY_PATH=`pwd`/libdartcv/lib $(DART_EXEC) test
39+
else
40+
LD_LIBRARY_PATH=`pwd`/libdartcv/lib $(DART_EXEC) test
41+
endif
3642

3743
test_coverage:
38-
dart pub global run coverage:test_with_coverage
44+
ifeq ($(UNAME_S),Darwin)
45+
DYLD_FALLBACK_LIBRARY_PATH=`pwd`/libdartcv/lib $(DART_EXEC) pub global run coverage:test_with_coverage
46+
else
47+
LD_LIBRARY_PATH=`pwd`/libdartcv/lib $(DART_EXEC) pub global run coverage:test_with_coverage
48+
endif

packages/dartcv/lib/src/g/imgproc.g.dart

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,6 +1109,40 @@ class CvNativeImgproc {
11091109
late final _cv_approxPolyDP = _cv_approxPolyDPPtr.asFunction<
11101110
ffi.Pointer<CvStatus> Function(VecPoint, double, bool, ffi.Pointer<VecPoint>, imp1.CvCallback_0)>();
11111111

1112+
ffi.Pointer<CvStatus> cv_approxPolyN(
1113+
VecPoint curve,
1114+
int n_segments,
1115+
double epsilon_percentage,
1116+
bool ensure_convex,
1117+
ffi.Pointer<VecPoint> rval,
1118+
imp1.CvCallback_0 callback,
1119+
) {
1120+
return _cv_approxPolyN(curve, n_segments, epsilon_percentage, ensure_convex, rval, callback);
1121+
}
1122+
1123+
late final _cv_approxPolyNPtr = _lookup<
1124+
ffi.NativeFunction<
1125+
ffi.Pointer<CvStatus> Function(
1126+
VecPoint, ffi.Int, ffi.Float, ffi.Bool, ffi.Pointer<VecPoint>, imp1.CvCallback_0)>>('cv_approxPolyN');
1127+
late final _cv_approxPolyN = _cv_approxPolyNPtr.asFunction<ffi.Pointer<CvStatus> Function(VecPoint, int, double, bool, ffi.Pointer<VecPoint>, imp1.CvCallback_0)>();
1128+
1129+
ffi.Pointer<CvStatus> cv_approxPolyN2f(
1130+
VecPoint2f curve,
1131+
int n_segments,
1132+
double epsilon_percentage,
1133+
bool ensure_convex,
1134+
ffi.Pointer<VecPoint2f> rval,
1135+
imp1.CvCallback_0 callback,
1136+
) {
1137+
return _cv_approxPolyN2f(curve, n_segments, epsilon_percentage, ensure_convex, rval, callback);
1138+
}
1139+
1140+
late final _cv_approxPolyN2fPtr = _lookup<
1141+
ffi.NativeFunction<
1142+
ffi.Pointer<CvStatus> Function(
1143+
VecPoint2f, ffi.Int, ffi.Float, ffi.Bool, ffi.Pointer<VecPoint2f>, imp1.CvCallback_0)>>('cv_approxPolyN2f');
1144+
late final _cv_approxPolyN2f = _cv_approxPolyN2fPtr.asFunction<ffi.Pointer<CvStatus> Function(VecPoint2f, int, double, bool, ffi.Pointer<VecPoint2f>, imp1.CvCallback_0)>();
1145+
11121146
ffi.Pointer<CvStatus> cv_arcLength(
11131147
VecPoint curve,
11141148
bool is_closed,

packages/dartcv/lib/src/imgproc/imgproc.dart

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,28 @@ VecPoint approxPolyDP(VecPoint curve, double epsilon, bool closed) {
3737
return vec;
3838
}
3939

40+
/// ApproxPolyN approximates a polygon with a convex hull with a specified accuracy and number of sides.
41+
///
42+
/// For further details, please see:
43+
///
44+
/// https://docs.opencv.org/4.x/d3/dc0/group__imgproc__shape.html#ga88981607a2d61b95074688aac55625cc
45+
VecPoint2f approxPolyN2f(VecPoint2f curve, int nsides, {double epsilon_percentage=-1.0, bool ensure_convex=true}) {
46+
final vec = VecPoint2f();
47+
cvRun(() => cimgproc.cv_approxPolyN2f(curve.ref, nsides, epsilon_percentage, ensure_convex, vec.ptr, ffi.nullptr));
48+
return vec;
49+
}
50+
51+
/// ApproxPolyN approximates a polygon with a convex hull with a specified accuracy and number of sides.
52+
///
53+
/// For further details, please see:
54+
///
55+
/// https://docs.opencv.org/4.x/d3/dc0/group__imgproc__shape.html#ga88981607a2d61b95074688aac55625cc
56+
VecPoint approxPolyN(VecPoint curve, int nsides, {double epsilon_percentage=-1.0, bool ensure_convex=true}) {
57+
final vec = VecPoint();
58+
cvRun(() => cimgproc.cv_approxPolyN(curve.ref, nsides, epsilon_percentage, ensure_convex, vec.ptr, ffi.nullptr));
59+
return vec;
60+
}
61+
4062
/// ArcLength calculates a contour perimeter or a curve length.
4163
///
4264
/// For further details, please see:

packages/dartcv/lib/src/imgproc/imgproc_async.dart

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,42 @@ Future<VecPoint> approxPolyDPAsync(VecPoint curve, double epsilon, bool closed)
4040
});
4141
}
4242

43+
/// ApproxPolyN approximates a polygon with a convex hull with a specified accuracy and number of sides.
44+
///
45+
/// For further details, please see:
46+
///
47+
/// https://docs.opencv.org/4.x/d3/dc0/group__imgproc__shape.html#ga88981607a2d61b95074688aac55625cc
48+
Future<VecPoint> approxPolyNAsync(VecPoint curve, int nsides,
49+
{double epsilon_percentage = -1.0, bool ensure_convex = true}) async {
50+
final vec = VecPoint();
51+
return cvRunAsync0(
52+
(callback) =>
53+
cimgproc.cv_approxPolyN(curve.ref, nsides, epsilon_percentage, ensure_convex, vec.ptr, callback),
54+
(c) {
55+
return c.complete(vec);
56+
});
57+
}
58+
59+
/// ApproxPolyN approximates a polygon with a convex hull with a specified accuracy and number of sides.
60+
///
61+
/// For further details, please see:
62+
///
63+
/// https://docs.opencv.org/4.x/d3/dc0/group__imgproc__shape.html#ga88981607a2d61b95074688aac55625cc
64+
Future<VecPoint2f> approxPolyN2fAsync(
65+
VecPoint2f curve,
66+
int nsides, {
67+
double epsilon_percentage = -1.0,
68+
bool ensure_convex = true,
69+
}) async {
70+
final vec = VecPoint2f();
71+
return cvRunAsync0(
72+
(callback) =>
73+
cimgproc.cv_approxPolyN2f(curve.ref, nsides, epsilon_percentage, ensure_convex, vec.ptr, callback),
74+
(c) {
75+
return c.complete(vec);
76+
});
77+
}
78+
4379
/// ArcLength calculates a contour perimeter or a curve length.
4480
///
4581
/// For further details, please see:

packages/dartcv/test/imgproc/imgproc_async_test.dart

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,23 @@ void main() async {
1818
expect(triangleContour.toList(), expected);
1919
});
2020

21+
test("cv.approxPolyNAsync", () async {
22+
final img = cv.Mat.create(cols: 100, rows: 200, type: cv.MatType.CV_8UC1);
23+
final color = cv.Scalar.all(255);
24+
cv.line(img, cv.Point(25, 25), cv.Point(25, 75), color);
25+
cv.line(img, cv.Point(25, 75), cv.Point(75, 50), color);
26+
cv.line(img, cv.Point(75, 50), cv.Point(25, 25), color);
27+
cv.rectangle(img, cv.Rect(125, 25, 175, 75), color);
28+
final (contours, _) = await cv.findContoursAsync(img, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE);
29+
final expected = <cv.Point>[cv.Point(76, 50), cv.Point(25, 76), cv.Point(25, 25)];
30+
{
31+
final triangleContour = await cv.approxPolyNAsync(contours.first, 3);
32+
expect(triangleContour.toList(), expected);
33+
}
34+
35+
// TODO: 2f version test
36+
});
37+
2138
test('cv.convexHullAsync, cv.convexityDefectsAsync', () async {
2239
final img = await cv.imreadAsync("test/images/face-detect.jpg", flags: cv.IMREAD_GRAYSCALE);
2340
expect(img.isEmpty, false);
@@ -851,9 +868,8 @@ void main() async {
851868
handleNested: handleNested,
852869
);
853870
if (intersectArea > 0) {
854-
final fillColor = !cv.isContourConvex(p1) || !cv.isContourConvex(p2)
855-
? cv.Scalar(0, 0, 255)
856-
: cv.Scalar.all(200);
871+
final fillColor =
872+
!cv.isContourConvex(p1) || !cv.isContourConvex(p2) ? cv.Scalar(0, 0, 255) : cv.Scalar.all(200);
857873
await cv.fillPolyAsync(image, cv.VecVecPoint.fromVecPoint(intersectionPolygon), fillColor);
858874
}
859875
await cv.polylinesAsync(image, cv.VecVecPoint.fromVecPoint(intersectionPolygon), true, cv.Scalar.black);

packages/dartcv/test/imgproc/imgproc_test.dart

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,23 @@ void main() async {
1818
expect(triangleContour.toList(), expected);
1919
});
2020

21+
test("cv.approxPolyN", () {
22+
final img = cv.Mat.create(cols: 100, rows: 200, type: cv.MatType.CV_8UC1);
23+
final color = cv.Scalar.all(255);
24+
cv.line(img, cv.Point(25, 25), cv.Point(25, 75), color);
25+
cv.line(img, cv.Point(25, 75), cv.Point(75, 50), color);
26+
cv.line(img, cv.Point(75, 50), cv.Point(25, 25), color);
27+
cv.rectangle(img, cv.Rect(125, 25, 175, 75), color);
28+
final (contours, _) = cv.findContours(img, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE);
29+
final expected = <cv.Point>[cv.Point(76, 50), cv.Point(25, 76), cv.Point(25, 25)];
30+
{
31+
final triangleContour = cv.approxPolyN(contours.first, 3);
32+
expect(triangleContour.toList(), expected);
33+
}
34+
35+
// TODO: 2f version test
36+
});
37+
2138
test('cv.convexHull, cv.convexityDefects', () {
2239
final img = cv.imread("test/images/face-detect.jpg", flags: cv.IMREAD_GRAYSCALE);
2340
expect(img.isEmpty, false);
@@ -877,9 +894,8 @@ void main() async {
877894
handleNested: handleNested,
878895
);
879896
if (intersectArea > 0) {
880-
final fillColor = !cv.isContourConvex(p1) || !cv.isContourConvex(p2)
881-
? cv.Scalar(0, 0, 255)
882-
: cv.Scalar.all(200);
897+
final fillColor =
898+
!cv.isContourConvex(p1) || !cv.isContourConvex(p2) ? cv.Scalar(0, 0, 255) : cv.Scalar.all(200);
883899
cv.fillPoly(image, cv.VecVecPoint.fromVecPoint(intersectionPolygon), fillColor);
884900
}
885901
cv.polylines(image, cv.VecVecPoint.fromVecPoint(intersectionPolygon), true, cv.Scalar.black);

packages/opencv_core/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
* support optional modules, configure it in `pubspec.yaml`, from this version, both opencv_core and
44
opencv_dart can enable/disable optional modules.
5+
* added `approxPolyN`.
56

67
## 1.4.1
78

0 commit comments

Comments
 (0)