Skip to content

Commit 2362331

Browse files
committed
add cv.invertAffineTransform, remove net.forwardAsync, add ios version info
1 parent 8d989c0 commit 2362331

File tree

7 files changed

+80
-48
lines changed

7 files changed

+80
-48
lines changed

CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ if(IOS)
6363
set_target_properties(${library_name} PROPERTIES
6464
FRAMEWORK TRUE
6565
FRAMEWORK_VERSION CXX
66-
MACOSX_FRAMEWORK_IDENTIFIER dev.rainyl.opencv_dart
66+
MACOSX_FRAMEWORK_IDENTIFIER dev.rainyl.opencvDart
67+
MACOSX_FRAMEWORK_BUNDLE_VERSION ${PROJECT_VERSION}
68+
MACOSX_FRAMEWORK_SHORT_VERSION_STRING ${PROJECT_VERSION}
6769
)
6870
endif(IOS)
6971

lib/src/core/mat.dart

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ final _bindings = cvg.CvNative(loadNativeLibrary());
1515

1616
class Mat with EquatableMixin implements ffi.Finalizable {
1717
Mat._(this.ptr) {
18-
_finalizer.attach(this, ptr);
18+
finalizer.attach(this, ptr);
1919
}
2020
factory Mat.fromCMat(cvg.Mat mat) => Mat._(mat);
2121

@@ -25,8 +25,8 @@ class Mat with EquatableMixin implements ffi.Finalizable {
2525
}
2626

2727
factory Mat.fromScalar(Scalar s, MatType type, {int rows = 1, int cols = 1}) {
28-
final _ptr = _bindings.Mat_NewWithSizeFromScalar(s.ref, rows, cols, type.toInt32());
29-
return Mat._(_ptr);
28+
final ptr = _bindings.Mat_NewWithSizeFromScalar(s.ref, rows, cols, type.toInt32());
29+
return Mat._(ptr);
3030
}
3131

3232
factory Mat.create({
@@ -39,24 +39,24 @@ class Mat with EquatableMixin implements ffi.Finalizable {
3939
}) {
4040
type = type ?? MatType.CV_8UC3;
4141
final scalar = Scalar(b.toDouble(), g.toDouble(), r.toDouble(), 0);
42-
final _ptr = _bindings.Mat_NewWithSizeFromScalar(scalar.ref, rows, cols, type.toInt32());
42+
final ptr = _bindings.Mat_NewWithSizeFromScalar(scalar.ref, rows, cols, type.toInt32());
4343

44-
return Mat._(_ptr);
44+
return Mat._(ptr);
4545
}
4646

4747
factory Mat.eye(int rows, int cols, MatType type) {
48-
final _ptr = _bindings.Eye(rows, cols, type.toInt32());
49-
return Mat._(_ptr);
48+
final ptr = _bindings.Eye(rows, cols, type.toInt32());
49+
return Mat._(ptr);
5050
}
5151

5252
factory Mat.zeros(int rows, int cols, MatType type) {
53-
final _ptr = _bindings.Zeros(rows, cols, type.toInt32());
54-
return Mat._(_ptr);
53+
final ptr = _bindings.Zeros(rows, cols, type.toInt32());
54+
return Mat._(ptr);
5555
}
5656

5757
factory Mat.ones(int rows, int cols, MatType type) {
58-
final _ptr = _bindings.Ones(rows, cols, type.toInt32());
59-
return Mat._(_ptr);
58+
final ptr = _bindings.Ones(rows, cols, type.toInt32());
59+
return Mat._(ptr);
6060
}
6161

6262
factory Mat.randn(int rows, int cols, MatType type, {Scalar? mean, Scalar? std}) {
@@ -83,11 +83,11 @@ class Mat with EquatableMixin implements ffi.Finalizable {
8383
int prows,
8484
int pcols,
8585
) {
86-
final _ptr = _bindings.Mat_FromPtr(m, rows, cols, type, prows, pcols);
87-
return Mat._(_ptr);
86+
final ptr = _bindings.Mat_FromPtr(m, rows, cols, type, prows, pcols);
87+
return Mat._(ptr);
8888
}
8989

90-
static final _finalizer = ffi.NativeFinalizer(_bindings.addresses.Mat_Close);
90+
static final finalizer = ffi.NativeFinalizer(_bindings.addresses.Mat_Close);
9191
cvg.Mat ptr;
9292
MatType get _type => MatType(_bindings.Mat_Type(ptr));
9393
int get width => _bindings.Mat_Cols(ptr);
@@ -660,17 +660,16 @@ class Mat with EquatableMixin implements ffi.Finalizable {
660660
}
661661

662662
Uint8List get data {
663-
final _data = _bindings.Mat_DataPtr(ptr);
664-
return _data.data.cast<ffi.Uint8>().asTypedList(_data.length);
663+
final data = _bindings.Mat_DataPtr(ptr);
664+
return data.data.cast<ffi.Uint8>().asTypedList(data.length);
665665
}
666666

667667
MatType get type => _type;
668668

669-
// @override
670-
// Future<Null> onDispose() {
671-
// _bindings.Mat_Close(_ptr);
672-
// return super.onDispose();
673-
// }
669+
@override
670+
String toString() {
671+
return "Mat(address=${ptr.address}, type=$type rows=$rows, cols=$cols, channels=$channels)";
672+
}
674673

675674
@override
676675
List<Object?> get props => [ptr.address];

lib/src/core/scalar.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,6 @@ class Scalar with EquatableMixin implements ffi.Finalizable {
4242
static final Scalar blue = Scalar.fromRgb(0, 0, 255);
4343
static final Scalar black = Scalar.fromRgb(0, 0, 0);
4444
static final Scalar white = Scalar.fromRgb(255, 255, 255);
45+
static final Scalar zeros = Scalar.fromRgb(0, 0, 0);
46+
static final Scalar max = Scalar(double.maxFinite, double.maxFinite, double.maxFinite, double.maxFinite);
4547
}

lib/src/dnn/dnn.dart

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ class Net implements ffi.Finalizable {
214214
/// For further details, please see:
215215
/// https://docs.opencv.org/4.x/db/d30/classcv_1_1dnn_1_1Net.html#a5e74adacffd6aa53d56046581de7fcbd
216216
void setInput(InputArray blob, {String name = "", double scalefactor = 1.0, Scalar? mean}) {
217-
mean ??= Scalar.default_();
217+
// mean ??= Scalar.default_(); not supported yet
218218
using((arena) {
219219
final cname = name.toNativeUtf8(allocator: arena);
220220
_bindings.Net_SetInput(ptr, blob.ptr, cname.cast());
@@ -233,18 +233,18 @@ class Net implements ffi.Finalizable {
233233
);
234234
}
235235

236+
/// OpenVINO not supported yet, this is not available
236237
/// ForwardAsync runs forward pass to compute output of layer with name outputName.
237238
///
238239
/// For further details, please see:
239240
/// https://docs.opencv.org/4.x/db/d30/classcv_1_1dnn_1_1Net.html#a814890154ea9e10b132fec00b6f6ba30
240-
AsyncArray forwardAsync({String outputName = ""}) {
241-
return using<AsyncArray>((arena) {
242-
final cname = outputName.toNativeUtf8(allocator: arena);
243-
return AsyncArray.fromPointer(
244-
_bindings.Net_forwardAsync(ptr, cname.cast()),
245-
);
246-
});
247-
}
241+
// AsyncArray forwardAsync({String outputName = ""}) {
242+
// return using<AsyncArray>((arena) {
243+
// final cname = outputName.toNativeUtf8(allocator: arena);
244+
// final p = _bindings.Net_forwardAsync(ptr, cname.cast());
245+
// return AsyncArray.fromPointer(p);
246+
// });
247+
// }
248248

249249
/// ForwardLayers forward pass to compute outputs of layers listed in outBlobNames.
250250
///
@@ -352,7 +352,7 @@ Mat blobFromImage(
352352
}) {
353353
return using<Mat>((arena) {
354354
size ??= (0, 0);
355-
mean ??= Scalar.default_();
355+
mean ??= Scalar.zeros;
356356
final _ptr = _bindings.Net_BlobFromImage(
357357
image.ptr,
358358
scalefactor,
@@ -384,7 +384,7 @@ Mat blobFromImages(
384384
return using<Mat>((arena) {
385385
blob ??= Mat.empty();
386386
size ??= (0, 0);
387-
mean ??= Scalar.default_();
387+
mean ??= Scalar.zeros;
388388
_bindings.Net_BlobFromImages(
389389
images.toMats(arena).ref,
390390
blob!.ptr,

lib/src/imgproc/imgproc.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1570,6 +1570,18 @@ double matchShapes(
15701570
return r;
15711571
}
15721572

1573+
/// Inverts an affine transformation.
1574+
/// The function computes an inverse affine transformation represented by 2×3 matrix M:
1575+
/// The result is also a 2×3 matrix of the same type as M.
1576+
///
1577+
/// For further details, please see:
1578+
/// https://docs.opencv.org/4.x/da/d54/group__imgproc__transform.html#ga57d3505a878a7e1a636645727ca08f51
1579+
Mat invertAffineTransform(InputArray M, {OutputArray? iM}) {
1580+
iM ??= Mat.empty();
1581+
_bindings.InvertAffineTransform(M.ptr, iM.ptr);
1582+
return iM;
1583+
}
1584+
15731585
/// NewCLAHE returns a new CLAHE algorithm
15741586
///
15751587
/// For further details, please see:

test/dnn_test.dart

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ bool checkCaffeNet(cv.Net net) {
1313
final img = cv.imread("test/images/space_shuttle.jpg", flags: cv.IMREAD_COLOR);
1414
expect(img.isEmpty, false);
1515

16-
final blob = cv.blobFromImage(img,
17-
scalefactor: 1.0, size: (224, 224), mean: cv.Scalar.all(0), swapRB: false, crop: false);
16+
final blob =
17+
cv.blobFromImage(img, scalefactor: 1.0, size: (224, 224), mean: cv.Scalar.all(0), swapRB: false, crop: false);
1818
expect(blob.isEmpty, false);
1919

2020
net.setInput(blob, name: "data");
@@ -51,8 +51,8 @@ bool checkTensorflow(cv.Net net) {
5151
final img = cv.imread("test/images/space_shuttle.jpg", flags: cv.IMREAD_COLOR);
5252
expect(img.isEmpty, false);
5353

54-
final blob = cv.blobFromImage(img,
55-
scalefactor: 1.0, size: (224, 224), mean: cv.Scalar.all(0), swapRB: true, crop: false);
54+
final blob =
55+
cv.blobFromImage(img, scalefactor: 1.0, size: (224, 224), mean: cv.Scalar.all(0), swapRB: true, crop: false);
5656
expect(blob.isEmpty, false);
5757

5858
net.setInput(blob, name: "input");
@@ -76,8 +76,8 @@ bool checkOnnx(cv.Net net) {
7676
final img = cv.imread("test/images/space_shuttle.jpg", flags: cv.IMREAD_COLOR);
7777
expect(img.isEmpty, false);
7878

79-
final blob = cv.blobFromImage(img,
80-
scalefactor: 1.0, size: (224, 224), mean: cv.Scalar.all(0), swapRB: true, crop: false);
79+
final blob =
80+
cv.blobFromImage(img, scalefactor: 1.0, size: (224, 224), mean: cv.Scalar.all(0), swapRB: true, crop: false);
8181
expect(blob.isEmpty, false);
8282

8383
net.setInput(blob, name: "data_0");
@@ -90,9 +90,9 @@ bool checkOnnx(cv.Net net) {
9090
expect((minLoc.x, minLoc.y), (955, 0));
9191
expect((maxLoc.x, maxLoc.y), (812, 0));
9292

93-
final probAsync = net.forwardAsync(outputName: "prob_1");
94-
final probMatAsync = probAsync.get();
95-
expect(probMatAsync.isEmpty, false);
93+
// final probAsync = net.forwardAsync(outputName: "prob_1");
94+
// final probMatAsync = probAsync.get();
95+
// expect(probMatAsync.isEmpty, false);
9696

9797
final perf = net.getPerfProfile();
9898
expect(perf, greaterThan(0));
@@ -104,8 +104,8 @@ bool checkTflite(cv.Net net) {
104104
final img = cv.imread("test/images/space_shuttle.jpg", flags: cv.IMREAD_COLOR);
105105
expect(img.isEmpty, false);
106106

107-
final blob = cv.blobFromImage(img,
108-
scalefactor: 1.0, size: (224, 224), mean: cv.Scalar.all(0), swapRB: true, crop: false);
107+
final blob =
108+
cv.blobFromImage(img, scalefactor: 1.0, size: (224, 224), mean: cv.Scalar.all(0), swapRB: true, crop: false);
109109
expect(blob.isEmpty, false);
110110

111111
// TODO: TFLite support of opencv is not complete
@@ -121,8 +121,8 @@ void main() async {
121121
final net = cv.Net.empty();
122122
expect(net.isEmpty, true);
123123

124-
final model = cv.Net.fromFile("test/models/bvlc_googlenet.caffemodel",
125-
config: "test/models/bvlc_googlenet.prototxt");
124+
final model =
125+
cv.Net.fromFile("test/models/bvlc_googlenet.caffemodel", config: "test/models/bvlc_googlenet.prototxt");
126126
checkCaffeNet(model);
127127
});
128128

@@ -134,8 +134,7 @@ void main() async {
134134
});
135135

136136
test('cv.Net.fromCaffe', () {
137-
final model =
138-
cv.Net.fromCaffe("test/models/bvlc_googlenet.prototxt", "test/models/bvlc_googlenet.caffemodel");
137+
final model = cv.Net.fromCaffe("test/models/bvlc_googlenet.prototxt", "test/models/bvlc_googlenet.caffemodel");
139138
checkCaffeNet(model);
140139
});
141140

test/imgproc_test.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -882,6 +882,24 @@ void main() async {
882882
expect(2.0 <= similarity && similarity <= 3.0, true);
883883
});
884884

885+
test('cv.invertAffineTransform', () {
886+
final src = [
887+
cv.Point(0, 0),
888+
cv.Point(10, 5),
889+
cv.Point(10, 10),
890+
];
891+
892+
final dst = [
893+
cv.Point(0, 0),
894+
cv.Point(10, 0),
895+
cv.Point(10, 10),
896+
];
897+
final m = cv.getAffineTransform(src, dst);
898+
final inv = cv.invertAffineTransform(m);
899+
expect(inv.isEmpty, false);
900+
expect((inv.rows, inv.cols), (2, 3));
901+
});
902+
885903
// accumulate
886904
test('cv.accumulate', () {
887905
final src = cv.imread("test/images/lenna.png", flags: cv.IMREAD_COLOR);

0 commit comments

Comments
 (0)