Skip to content

Commit

Permalink
Merge pull request #315 from rainyl/no-copy-vec
Browse files Browse the repository at this point in the history
No copy vec, update to opencv 4.11.0
  • Loading branch information
rainyl authored Jan 17, 2025
2 parents 044f74d + c55d231 commit 38509a9
Show file tree
Hide file tree
Showing 70 changed files with 11,062 additions and 1,546 deletions.
13 changes: 13 additions & 0 deletions packages/dartcv/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
## 1.1.1

* make `VecVecChar` unmodifible
* more tests

## 1.1.0

* update to OpenCV 4.11.0
* change vector wrappers to std::vector
* fix reference of estimateAffinePartial2D
* support `VideoWriter.get()` and `VideoWriter.set()`
* use native implementation of 8U/8S -> F16 for `cv.LUT()`

## 1.0.1

* add example
Expand Down
8 changes: 7 additions & 1 deletion packages/dartcv/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.PHONY: ffigen ffigen_test clone_repo clean_repo


ffigen: clone_repo
ffigen:
dart run ffigen --config ffigen/ffigen_const.yaml
dart run ffigen --config ffigen/ffigen_types.yaml
dart run ffigen --config ffigen/ffigen_core.yaml
Expand Down Expand Up @@ -30,3 +30,9 @@ clone_repo:
ffigen_test: clone_repo
dart run ffigen --config ffigen/ffigen_types.yaml
dart run ffigen --config ffigen/ffigen_core.yaml

test:
dart test

test_coverage:
dart pub global run coverage:test_with_coverage
4 changes: 2 additions & 2 deletions packages/dartcv/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

OpenCV bindings for Dart language.

[dartcv4](https://pub.dev/packages/dartcv4) is only for pure dart, for Flutter, use [opencv_core](https://pub.dev/packages/opencv_core),
if videoio module is required, use [opencv_dart](https://pub.dev/packages/opencv_dart)
[dartcv4](https://pub.dev/packages/dartcv4) is only for pure dart, for Flutter, use [opencv_core](https://pub.dev/packages/opencv_core) (if `videoio` module is NOT required) or [opencv_dart](https://pub.dev/packages/opencv_dart)
(if videoio module IS required).

## Install

Expand Down
7 changes: 7 additions & 0 deletions packages/dartcv/ffigen/ffigen_core.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,17 @@ headers:
- ../src/dartcv/core/logging.h
- ../src/dartcv/core/mat.h
- ../src/dartcv/core/svd.h
- ../src/dartcv/core/stdvec.h
- ../src/dartcv/core/utils.h
- ../src/dartcv/core/version.h
include-directives:
- ../src/dartcv/core/core.h
- ../src/dartcv/core/exception.h
- ../src/dartcv/core/logging.h
- ../src/dartcv/core/mat.h
- ../src/dartcv/core/svd.h
- ../src/dartcv/core/stdvec.h
- ../src/dartcv/core/utils.h
- ../src/dartcv/core/version.h
functions:
leaf:
Expand All @@ -53,6 +57,9 @@ functions:
include:
- ".*_Close.*"
- ".*_close.*"
- ".*_destroy.*"
- ".*_delete.*"
- ".*_free.*"
# exclude: # If you only use exclude, then everything not excluded is generated.
# - "dispose"

Expand Down
6 changes: 3 additions & 3 deletions packages/dartcv/lib/src/calib3d/calib3d.dart
Original file line number Diff line number Diff line change
Expand Up @@ -215,21 +215,21 @@ Mat undistortPoints(
int flags = 0,
VecPoint2f? corners,
}) {
final pCorners = calloc<cvg.VecPoint2f>();
final corners = VecPoint2f();
final p = calloc<ffi.Bool>();
cvRun(
() => ccalib3d.cv_findChessboardCornersSB(
image.ref,
patternSize.toSize().ref,
pCorners,
corners.ptr,
flags,
p,
ffi.nullptr,
),
);
final rval = p.value;
calloc.free(p);
return (rval, VecPoint2f.fromPointer(pCorners));
return (rval, corners);
}

// Finds the positions of internal corners of the chessboard using a sector based approach.
Expand Down
18 changes: 7 additions & 11 deletions packages/dartcv/lib/src/contrib/aruco.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,24 +62,20 @@ class ArucoDetector extends CvStruct<cvg.ArucoDetector> {
/// For further details, please see:
/// https://docs.opencv.org/master/d9/d6a/group__aruco.html#ga3bc50d61fe4db7bce8d26d56b5a6428a
(VecVecPoint2f corners, VecI32 ids, VecVecPoint2f rejectedImgPoints) detectMarkers(InputArray image) {
final pCorners = calloc<cvg.VecVecPoint2f>();
final pRejected = calloc<cvg.VecVecPoint2f>();
final pIds = calloc<cvg.VecI32>();
final corners = VecVecPoint2f();
final rejected = VecVecPoint2f();
final ids = VecI32();
cvRun(
() => ccontrib.cv_aruco_arucoDetector_detectMarkers(
ref,
image.ref,
pCorners,
pIds,
pRejected,
corners.ptr,
ids.ptr,
rejected.ptr,
ffi.nullptr,
),
);
return (
VecVecPoint2f.fromPointer(pCorners),
VecI32.fromPointer(pIds),
VecVecPoint2f.fromPointer(pRejected)
);
return (corners, ids, rejected);
}
}

Expand Down
20 changes: 7 additions & 13 deletions packages/dartcv/lib/src/contrib/aruco_async.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,30 @@

library cv.contrib;

import 'dart:ffi' as ffi;
import 'package:ffi/ffi.dart';

import '../core/base.dart';
import '../core/mat.dart';
import '../core/point.dart';
import '../core/scalar.dart';
import '../core/vec.dart';
import '../g/contrib.g.dart' as cvg;
import '../native_lib.dart' show ccontrib;
import 'aruco.dart';
import 'aruco_dict.dart';

extension ArucoDetectorAsync on ArucoDetector {
Future<(VecVecPoint2f, VecI32, VecVecPoint2f)> detectMarkersAsync(InputArray image) async {
final pCorners = calloc<cvg.VecVecPoint2f>();
final pRejected = calloc<cvg.VecVecPoint2f>();
final pIds = calloc<cvg.VecI32>();
final corners = VecVecPoint2f();
final rejected = VecVecPoint2f();
final ids = VecI32();
return cvRunAsync0(
(callback) => ccontrib.cv_aruco_arucoDetector_detectMarkers(
ref,
image.ref,
pCorners,
pIds,
pRejected,
corners.ptr,
ids.ptr,
rejected.ptr,
callback,
), (c) {
return c.complete(
(VecVecPoint2f.fromPointer(pCorners), VecI32.fromPointer(pIds), VecVecPoint2f.fromPointer(pRejected)),
);
return c.complete((corners, ids, rejected));
});
}
}
Expand Down
12 changes: 6 additions & 6 deletions packages/dartcv/lib/src/contrib/img_hash.dart
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,9 @@ class BlockMeanHash extends CvStruct<cvg.BlockMeanHash> implements ImgHashBase {

/// https://docs.opencv.org/4.x/df/d55/classcv_1_1img__hash_1_1BlockMeanHash.html#ad5aef85f58315551cac14bcabe05f0c3
VecF64 getMean() {
final ret = calloc<cvg.VecF64>();
cvRun(() => ccontrib.cv_img_hash_BlockMeanHash_getMean(ref, ret, ffi.nullptr));
return VecF64.fromPointer(ret);
final ret = VecF64();
cvRun(() => ccontrib.cv_img_hash_BlockMeanHash_getMean(ref, ret.ptr, ffi.nullptr));
return ret;
}

@override
Expand All @@ -210,11 +210,11 @@ class BlockMeanHash extends CvStruct<cvg.BlockMeanHash> implements ImgHashBase {
}

Future<VecF64> getMeanAsync() async {
final ret = calloc<cvg.VecF64>();
final ret = VecF64();
return cvRunAsync0<VecF64>(
(callback) => ccontrib.cv_img_hash_BlockMeanHash_getMean(ref, ret, callback),
(callback) => ccontrib.cv_img_hash_BlockMeanHash_getMean(ref, ret.ptr, callback),
(c) {
return c.complete(VecF64.fromPointer(ret));
return c.complete(ret);
},
);
}
Expand Down
27 changes: 14 additions & 13 deletions packages/dartcv/lib/src/contrib/wechat_qrcode.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,26 +60,27 @@ class WeChatQRCode extends CvStruct<cvg.WeChatQRCode> {
InputArray img, [
VecMat? points,
]) {
final p = calloc<cvg.VecMat>();
final rval = calloc<cvg.VecVecChar>();
cvRun(() => ccontrib.cv_wechat_qrcode_WeChatQRCode_detectAndDecode(ref, img.ref, p, rval, ffi.nullptr));
final vec = VecVecChar.fromPointer(rval);
final points = VecMat.fromPointer(p);
return (vec.asStringList(), points);
points ??= VecMat();
final strs = VecVecChar();
cvRun(() => ccontrib.cv_wechat_qrcode_WeChatQRCode_detectAndDecode(
ref, img.ref, points!.ptr, strs.ptr, ffi.nullptr));
final rval = (strs.asStringList(), points);
strs.dispose();
return rval;
}

Future<(List<String>, VecMat)> detectAndDecodeAsync(
InputArray img, [
VecMat? points,
]) async {
final p = calloc<cvg.VecMat>();
final rval = calloc<cvg.VecVecChar>();
points ??= VecMat();
final strs = VecVecChar();
return cvRunAsync0(
(callback) => ccontrib.cv_wechat_qrcode_WeChatQRCode_detectAndDecode(ref, img.ref, p, rval, callback),
(c) {
final vec = VecVecChar.fromPointer(rval);
final points = VecMat.fromPointer(p);
return c.complete((vec.asStringList(), points));
(callback) => ccontrib.cv_wechat_qrcode_WeChatQRCode_detectAndDecode(
ref, img.ref, points!.ptr, strs.ptr, callback), (c) {
final rval = (strs.asStringList(), points!);
strs.dispose();
return c.complete(rval);
});
}

Expand Down
22 changes: 11 additions & 11 deletions packages/dartcv/lib/src/contrib/ximgproc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -322,19 +322,19 @@ class EdgeBoxes extends CvStruct<cvg.EdgeBoxes> {
///
/// https://docs.opencv.org/4.x/dd/d65/classcv_1_1ximgproc_1_1EdgeBoxes.html#a822e422556f8103d01a0a4db6815f0e5
(VecRect boxes, VecF32 scores) getBoundingBoxes(InputArray edge_map, InputArray orientation_map) {
final pvr = calloc<cvg.VecRect>();
final pvf = calloc<cvg.VecF32>();
final pvr = VecRect();
final pvf = VecF32();
cvRun(
() => ccontrib.cv_ximgproc_EdgeBoxes_getBoundingBoxes(
ref,
edge_map.ref,
orientation_map.ref,
pvr,
pvf,
pvr.ptr,
pvf.ptr,
ffi.nullptr,
),
);
return (VecRect.fromPointer(pvr), VecF32.fromPointer(pvf));
return (pvr, pvf);
}

double get alpha => ccontrib.cv_ximgproc_EdgeBoxes_getAlpha(ref);
Expand Down Expand Up @@ -714,15 +714,15 @@ class EdgeDrawing extends CvStruct<cvg.EdgeDrawing> {
}

VecI32 getSegmentIndicesOfLines() {
final p = calloc<cvg.VecI32>();
cvRun(() => ccontrib.cv_ximgproc_EdgeDrawing_getSegmentIndicesOfLines(ref, p, ffi.nullptr));
return VecI32.fromPointer(p);
final v = VecI32();
cvRun(() => ccontrib.cv_ximgproc_EdgeDrawing_getSegmentIndicesOfLines(ref, v.ptr, ffi.nullptr));
return v;
}

VecVecPoint getSegments() {
final p = calloc<cvg.VecVecPoint>();
cvRun(() => ccontrib.cv_ximgproc_EdgeDrawing_getSegments(ref, p, ffi.nullptr));
return VecVecPoint.fromPointer(p);
final p = VecVecPoint();
cvRun(() => ccontrib.cv_ximgproc_EdgeDrawing_getSegments(ref, p.ptr, ffi.nullptr));
return p;
}

EdgeDrawingParams get params {
Expand Down
8 changes: 4 additions & 4 deletions packages/dartcv/lib/src/contrib/xobjdetect.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ class WBDetector extends CvStruct<cvg.PtrWBDetector> {
///
/// https://docs.opencv.org/4.x/de/d0e/classcv_1_1xobjdetect_1_1WBDetector.html#ad19680e6545f49a9ca42dfc3457319e2
(VecRect bboxes, VecF64 confidences) detect(Mat img) {
final bboxesPtr = calloc<cvg.VecRect>();
final confidencesPtr = calloc<cvg.VecF64>();
cvRun(() => ccontrib.cv_xobjdetect_WBDetector_detect(ref, img.ref, bboxesPtr, confidencesPtr));
return (VecRect.fromPointer(bboxesPtr), VecF64.fromPointer(confidencesPtr));
final bboxes = VecRect();
final confidences = VecF64();
cvRun(() => ccontrib.cv_xobjdetect_WBDetector_detect(ref, img.ref, bboxes.ptr, confidences.ptr));
return (bboxes, confidences);
}

/// Train WaldBoost detector.
Expand Down
6 changes: 3 additions & 3 deletions packages/dartcv/lib/src/core/core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1187,9 +1187,9 @@ Mat sortIdx(InputArray src, int flags, {OutputArray? dst}) {
/// For further details, please see:
/// https://docs.opencv.org/master/d2/de8/group__core__array.html#ga0547c7fed86152d7e9d0096029c8518a
VecMat split(InputArray m) {
final vec = calloc<cvg.VecMat>();
cvRun(() => ccore.cv_split(m.ref, vec, ffi.nullptr));
return VecMat.fromPointer(vec);
final vec = VecMat();
cvRun(() => ccore.cv_split(m.ref, vec.ptr, ffi.nullptr));
return vec;
}

/// Calculates a square root of array elements.
Expand Down
6 changes: 3 additions & 3 deletions packages/dartcv/lib/src/core/core_async.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1423,11 +1423,11 @@ Future<Mat> sortIdxAsync(InputArray src, int flags, {OutputArray? dst}) async {
/// For further details, please see:
/// https://docs.opencv.org/master/d2/de8/group__core__array.html#ga0547c7fed86152d7e9d0096029c8518a
Future<VecMat> splitAsync(InputArray m) async {
final vec = calloc<cvg.VecMat>();
final vec = VecMat();
return cvRunAsync0(
(callback) => ccore.cv_split(m.ref, vec, callback),
(callback) => ccore.cv_split(m.ref, vec.ptr, callback),
(c) {
return c.complete(VecMat.fromPointer(vec));
return c.complete(vec);
},
);
}
Expand Down
Loading

0 comments on commit 38509a9

Please sign in to comment.