Skip to content

Commit 580213b

Browse files
authored
Merge pull request #274 from rainyl/docs
more tests, bump opencv_core and opencv_dart to 1.3.0
2 parents e34b6bb + d31129b commit 580213b

File tree

14 files changed

+178
-129
lines changed

14 files changed

+178
-129
lines changed

packages/dartcv/CHANGELOG.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
1+
## 1.0.1
2+
3+
* add example
4+
5+
## 1.0.0
6+
7+
* stable release
8+
* add `cv.Mat.fromMat`
9+
110
## 0.0.2
211

3-
- More tests
4-
- add copyToAsync PSNRAsync mulTransposedAsync VecRect2f Net.getUnconnectedOutLayersNames enableModelDiagnostics getAvailableBackends getAvailableTargets
12+
* More tests
13+
* add copyToAsync PSNRAsync mulTransposedAsync VecRect2f Net.getUnconnectedOutLayersNames enableModelDiagnostics getAvailableBackends getAvailableTargets
514

615
## 0.0.1
716

8-
- Initial version.
17+
* Initial version.
File renamed without changes.

packages/dartcv/lib/src/core/exception.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class CvException implements Exception {
2525

2626
@override
2727
String toString() {
28-
return "CvException(code: $code, err: $err, msg:$msg) in $func of file $file:$line";
28+
return "CvException(code: $code, msg:$msg) in $func of file $file:$line";
2929
}
3030
}
3131

packages/dartcv/lib/src/core/mat.dart

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,13 @@ class Mat extends CvStruct<cvg.Mat> {
2828

2929
//SECTION - Constructors
3030

31-
factory Mat.fromNative(cvg.Mat mat) {
32-
final p = calloc<cvg.Mat>()..ref = mat;
33-
return Mat._(p);
31+
factory Mat.fromMat(Mat mat, {bool copy = false, Rect? roi}) {
32+
roi ??= Rect(0, 0, mat.cols, mat.rows);
33+
final p = calloc<cvg.Mat>();
34+
cvRun(() => ccore.cv_Mat_create_13(mat.ref, roi!.ref, p, ffi.nullptr));
35+
final dst = Mat._(p, false);
36+
if (copy) return dst.clone();
37+
return dst;
3438
}
3539

3640
/// Create a Mat from a list of data
@@ -167,12 +171,16 @@ class Mat extends CvStruct<cvg.Mat> {
167171
}
168172

169173
factory Mat.create({int rows = 0, int cols = 0, int r = 0, int g = 0, int b = 0, MatType? type}) {
170-
type = type ?? MatType.CV_8UC3;
171-
final scalar = Scalar(b.toDouble(), g.toDouble(), r.toDouble(), 0);
172-
final p = calloc<cvg.Mat>();
173-
cvRun(() => ccore.cv_Mat_create_5(scalar.ref, rows, cols, type!.value, p, ffi.nullptr));
174-
final mat = Mat._(p);
175-
return mat;
174+
if (rows == 0 && cols == 0) {
175+
return Mat.empty();
176+
} else {
177+
type = type ?? MatType.CV_8UC3;
178+
final scalar = Scalar(b.toDouble(), g.toDouble(), r.toDouble(), 0);
179+
final p = calloc<cvg.Mat>();
180+
cvRun(() => ccore.cv_Mat_create_5(scalar.ref, rows, cols, type!.value, p, ffi.nullptr));
181+
final mat = Mat._(p);
182+
return mat;
183+
}
176184
}
177185

178186
/// Create [Mat] from another [Mat] with range
@@ -317,7 +325,10 @@ class Mat extends CvStruct<cvg.Mat> {
317325
return (ms.p[0], ms.p[1], ms.p[2]);
318326
}
319327

328+
/// Returns the matrix element size in bytes.
320329
int get elemSize => ccore.cv_Mat_elemSize(ref);
330+
331+
/// Returns the size of each matrix element channel in bytes.
321332
int get elemSize1 => ccore.cv_Mat_elemSize1(ref);
322333
int get dims => ccore.cv_Mat_dims(ref);
323334

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,28 @@ class CvNativeCore {
450450
ffi.Pointer<CvStatus> Function(
451451
Mat, int, int, int, int, ffi.Pointer<Mat>, imp1.CvCallback_0)>();
452452

453+
ffi.Pointer<CvStatus> cv_Mat_create_13(
454+
Mat self,
455+
CvRect roi,
456+
ffi.Pointer<Mat> rval,
457+
imp1.CvCallback_0 callback,
458+
) {
459+
return _cv_Mat_create_13(
460+
self,
461+
roi,
462+
rval,
463+
callback,
464+
);
465+
}
466+
467+
late final _cv_Mat_create_13Ptr = _lookup<
468+
ffi.NativeFunction<
469+
ffi.Pointer<CvStatus> Function(Mat, CvRect, ffi.Pointer<Mat>,
470+
imp1.CvCallback_0)>>('cv_Mat_create_13');
471+
late final _cv_Mat_create_13 = _cv_Mat_create_13Ptr.asFunction<
472+
ffi.Pointer<CvStatus> Function(
473+
Mat, CvRect, ffi.Pointer<Mat>, imp1.CvCallback_0)>();
474+
453475
/// @brief Create Mat with specified sizes and type
454476
///
455477
/// @param sizes array of integers, each describing a dimension

packages/dartcv/lib/src/g/core.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ files:
4444
name: cv_Mat_create_11
4545
c:@F@cv_Mat_create_12:
4646
name: cv_Mat_create_12
47+
c:@F@cv_Mat_create_13:
48+
name: cv_Mat_create_13
4749
c:@F@cv_Mat_create_2:
4850
name: cv_Mat_create_2
4951
c:@F@cv_Mat_create_3:

packages/dartcv/pubspec.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ name: dartcv4
22
description: >
33
OpenCV bindings for Dart language. dartcv is for pure dart only,
44
for flutter, please use opencv_core, if you need videoio module, use opencv_dart.
5-
version: 0.0.2
5+
version: 1.0.1
6+
repository: https://github.com/rainyl/opencv_dart
67
homepage: https://github.com/rainyl/opencv_dart/tree/main/packages/dartcv
78

89
environment:

packages/dartcv/test/core/mat_test.dart

Lines changed: 94 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ void main() async {
1818
expect(mat1.isContinus, equals(true));
1919
expect(mat1.step.$1, equals(100 * 3));
2020
expect(mat1.elemSize, equals(3));
21+
expect(mat1.elemSize1, 1);
22+
expect(mat1.dims, 2);
23+
expect(mat1.flags, isA<int>());
2124
expect(mat1.at<int>(0, 0, 0), 255);
2225

2326
final mat2 = cv.Mat.zeros(3, 3, cv.MatType.CV_8UC1);
@@ -45,6 +48,36 @@ void main() async {
4548
expect(mat5.at<int>(0, 0), equals(255));
4649
});
4750

51+
test('cv.Mat.fromMat', () {
52+
final src = cv.Mat.fromScalar(100, 200, cv.MatType.CV_8UC3, cv.Scalar.white);
53+
final mat1 = cv.Mat.fromMat(src); // A reference of src
54+
expect(mat1.size, [100, 200]);
55+
expect(mat1.at<cv.Vec3b>(0, 0), cv.Vec3b(255, 255, 255));
56+
57+
final diff = mat1.subtract(src);
58+
expect(diff.sum(), cv.Scalar.zeros);
59+
60+
mat1.set<int>(0, 0, 241);
61+
expect(mat1.at<int>(0, 0), equals(241));
62+
mat1.dispose();
63+
expect(src.at<int>(0, 0), equals(241));
64+
65+
final mat2 = cv.Mat.fromMat(src, roi: cv.Rect(10, 10, 20, 20));
66+
expect(mat2.size, [20, 20]);
67+
expect(mat2.at<cv.Vec3b>(0, 0), cv.Vec3b(255, 255, 255));
68+
mat2.set<cv.Vec3b>(0, 0, cv.Vec3b(2, 4, 1));
69+
expect(mat2.at<cv.Vec3b>(0, 0), cv.Vec3b(2, 4, 1));
70+
expect(src.at<cv.Vec3b>(10, 10), cv.Vec3b(2, 4, 1));
71+
72+
final mat3 = cv.Mat.fromMat(src, roi: cv.Rect(21, 21, 20, 20), copy: true);
73+
expect(mat3.size, [20, 20]);
74+
expect(mat3.at<cv.Vec3b>(0, 0), cv.Vec3b(255, 255, 255));
75+
mat3.set<cv.Vec3b>(0, 0, cv.Vec3b(2, 4, 1));
76+
expect(mat3.at<cv.Vec3b>(0, 0), cv.Vec3b(2, 4, 1));
77+
mat3.dispose();
78+
expect(src.at<cv.Vec3b>(21, 21), cv.Vec3b(255, 255, 255));
79+
});
80+
4881
test('Mat.fromBytes', () {
4982
const int rows = 3;
5083
const int cols = 3;
@@ -180,11 +213,11 @@ array([[[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8]],
180213
});
181214

182215
test('Mat operations Add', () {
183-
final mat0 = cv.Mat.ones(100, 100, cv.MatType.CV_8UC3).multiplyU8(128);
184-
final mat1 = cv.Mat.ones(100, 100, cv.MatType.CV_8UC3).setTo(cv.Scalar.all(127));
216+
final mat0 = cv.Mat.ones(100, 100, cv.MatType.CV_8UC3).multiplyU8(128); // 128
217+
final mat1 = cv.Mat.ones(100, 100, cv.MatType.CV_8UC3).setTo(cv.Scalar.all(127)); // 127
185218

186219
// Mat
187-
final mat2 = mat1.add<cv.Mat>(mat0);
220+
final mat2 = mat1.add<cv.Mat>(mat0); // 255
188221
expect((mat2.width, mat2.height, mat2.channels), (100, 100, 3));
189222
expect(mat2.at<int>(0, 0), equals(255));
190223
expect(() => mat2.add<double>(0.1), throwsUnsupportedError);
@@ -194,16 +227,21 @@ array([[[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8]],
194227
expect(mat2_1.at<int>(0, 0), equals(255));
195228

196229
// int
197-
final mat3 = mat1.add<int>(3);
198-
expect((mat3.width, mat3.height, mat3.channels), (100, 100, 3));
199-
expect(mat3.at<int>(0, 0), equals(130));
200-
mat3.add<int>(1, inplace: true);
201-
expect(mat3.at<int>(0, 0), equals(131));
230+
const types = [cv.MatType.CV_8UC3, cv.MatType.CV_16UC3, cv.MatType.CV_16SC3, cv.MatType.CV_32SC3];
231+
for (final type in types) {
232+
final mat4 = mat1.convertTo(type).add<int>(54);
233+
expect(mat4.at<int>(0, 0), equals(181));
234+
mat4.add<int>(1, inplace: true);
235+
expect(mat4.at<int>(0, 0), equals(182));
236+
mat4.dispose();
237+
}
202238

203-
final mat4 = mat1.convertTo(cv.MatType.CV_32SC3).add<int>(54);
204-
expect(mat4.at<int>(0, 0), equals(181));
205-
mat4.add<int>(1, inplace: true);
206-
expect(mat4.at<int>(0, 0), equals(182));
239+
{
240+
final mat4_1 = mat1.convertTo(cv.MatType.CV_8SC3).add<int>(54); // 127+54, overflow, 127
241+
expect(mat4_1.at<int>(0, 0), equals(127));
242+
mat4_1.add<int>(1, inplace: true);
243+
expect(mat4_1.at<int>(0, 0), equals(127));
244+
}
207245

208246
// float
209247
final mat5 = mat1.convertTo(cv.MatType.CV_32FC3).add<double>(54.5);
@@ -232,16 +270,21 @@ array([[[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8]],
232270
expect(mat2_1.at<int>(0, 0), equals(128));
233271

234272
// int
235-
final mat3 = mat0.subtract<int>(13);
236-
expect((mat3.width, mat3.height, mat3.channels), (100, 100, 3));
237-
expect(mat3.at<int>(0, 0), equals(242));
238-
mat3.subtract<int>(1, inplace: true);
239-
expect(mat3.at<int>(0, 0), equals(241));
273+
const types = [cv.MatType.CV_8UC3, cv.MatType.CV_16UC3, cv.MatType.CV_16SC3, cv.MatType.CV_32SC3];
274+
for (final type in types) {
275+
final mat4 = mat0.convertTo(type).subtract<int>(14);
276+
expect(mat4.at<int>(0, 0), equals(241));
277+
mat4.subtract<int>(1, inplace: true);
278+
expect(mat4.at<int>(0, 0), equals(240));
279+
mat4.dispose();
280+
}
240281

241-
final mat4 = mat0.convertTo(cv.MatType.CV_32SC3).subtract<int>(14);
242-
expect(mat4.at<int>(0, 0), equals(241));
243-
mat4.subtract<int>(1, inplace: true);
244-
expect(mat4.at<int>(0, 0), equals(240));
282+
{
283+
final mat4_1 = mat0.convertTo(cv.MatType.CV_8SC3).subtract<int>(14);
284+
expect(mat4_1.at<int>(0, 0), equals(113));
285+
mat4_1.subtract<int>(1, inplace: true);
286+
expect(mat4_1.at<int>(0, 0), equals(112));
287+
}
245288

246289
// float
247290
final mat5 = mat0.convertTo(cv.MatType.CV_32FC3).subtract<double>(54.5);
@@ -270,16 +313,22 @@ array([[[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8]],
270313
expect(mat2_1.at<int>(0, 0), equals(200));
271314

272315
// int
273-
final mat3 = mat0.multiply<int>(2);
274-
expect((mat3.width, mat3.height, mat3.channels), (100, 100, 3));
275-
expect(mat3.at<int>(0, 0), equals(200));
276-
mat3.multiply<int>(1, inplace: true);
277-
expect(mat3.at<int>(0, 0), equals(200));
316+
const types = [cv.MatType.CV_8UC3, cv.MatType.CV_16UC3, cv.MatType.CV_16SC3, cv.MatType.CV_32SC3];
317+
for (final type in types) {
318+
final mat3 = mat0.convertTo(type).multiply<int>(2);
319+
expect((mat3.width, mat3.height, mat3.channels), (100, 100, 3));
320+
expect(mat3.at<int>(0, 0), equals(200));
321+
mat3.multiply<int>(1, inplace: true);
322+
expect(mat3.at<int>(0, 0), equals(200));
323+
mat3.dispose();
324+
}
278325

279-
final mat4 = mat0.convertTo(cv.MatType.CV_32SC3).multiply<int>(2);
280-
expect(mat4.at<int>(0, 0), equals(200));
281-
mat4.multiply<int>(1, inplace: true);
282-
expect(mat4.at<int>(0, 0), equals(200));
326+
{
327+
final mat4 = mat0.convertTo(cv.MatType.CV_8SC3).multiply<int>(2);
328+
expect(mat4.at<int>(0, 0), equals(127));
329+
mat4.multiply<int>(1, inplace: true);
330+
expect(mat4.at<int>(0, 0), equals(127));
331+
}
283332

284333
// float
285334
final mat5 = mat0.convertTo(cv.MatType.CV_32FC3).multiply<double>(1.5);
@@ -313,16 +362,21 @@ array([[[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8]],
313362
expect(mat2_1.at<int>(0, 0), equals(100));
314363

315364
// int
316-
final mat3 = mat0.divide<int>(2);
317-
expect((mat3.width, mat3.height, mat3.channels), (100, 100, 3));
318-
expect(mat3.at<int>(0, 0), equals(100));
319-
mat3.divide<int>(2, inplace: true);
320-
expect(mat3.at<int>(0, 0), equals(50));
321-
322-
final mat4 = mat0.convertTo(cv.MatType.CV_32SC3).divide<int>(2);
323-
expect(mat4.at<int>(0, 0), equals(100));
324-
mat4.divide<int>(2, inplace: true);
325-
expect(mat4.at<int>(0, 0), equals(50));
365+
const types = [cv.MatType.CV_8UC3, cv.MatType.CV_16UC3, cv.MatType.CV_16SC3, cv.MatType.CV_32SC3];
366+
for (final type in types) {
367+
final mat4 = mat0.convertTo(type).divide<int>(2);
368+
expect(mat4.at<int>(0, 0), equals(100));
369+
mat4.divide<int>(2, inplace: true);
370+
expect(mat4.at<int>(0, 0), equals(50));
371+
mat4.dispose();
372+
}
373+
374+
{
375+
final mat4 = mat0.convertTo(cv.MatType.CV_8SC3).divide<int>(2);
376+
expect(mat4.at<int>(0, 0), equals(64));
377+
mat4.divide<int>(2, inplace: true);
378+
expect(mat4.at<int>(0, 0), equals(32));
379+
}
326380

327381
// float
328382
final mat5 = mat0.convertTo(cv.MatType.CV_32FC3).divide<double>(5.0);

packages/dartcv/test/core/vec_test.dart

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,10 @@ void main() {
274274
expect(dm, cv.DMatch(1, 1, 1, 1.0));
275275
// change the reference will affect the original value
276276
dm.queryIdx = 100;
277-
expect(dm, cv.DMatch(100, 1, 1, 1.0));
277+
dm.trainIdx = 2;
278+
dm.imgIdx = 3;
279+
dm.distance = 241.0;
280+
expect(dm, cv.DMatch(100, 2, 3, 241.0));
278281
// change the value
279282
vec[1] = cv.DMatch(100, 100, 11, 21.0);
280283
expect(vec[1], cv.DMatch(100, 100, 11, 21.0));
@@ -330,7 +333,13 @@ void main() {
330333
expect(kp, cv.KeyPoint(1.000, 1.000, 1.000, 1.000, 1.000, 1, 1));
331334
// change the reference will affect the original value
332335
kp.x = 100.0;
333-
expect(kp, cv.KeyPoint(100.0, 1.000, 1.000, 1.000, 1.000, 1, 1));
336+
kp.y = 100.0;
337+
kp.size = 100.0;
338+
kp.angle = 100.0;
339+
kp.response = 100.0;
340+
kp.octave = 100;
341+
kp.classID = 100;
342+
expect(kp, cv.KeyPoint(100.0, 100.0, 100.0, 100.0, 100.0, 100, 100));
334343
// change the value
335344
vec[1] = cv.KeyPoint(5.000, 2.000, 5.000, 4.000, 1.000, 0, 0);
336345
expect(vec[1], cv.KeyPoint(5.000, 2.000, 5.000, 4.000, 1.000, 0, 0));

packages/opencv_core/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 1.3.0
2+
3+
* bump dartcv to 0.0.3
4+
* add `cv.Mat.fromMat`
5+
16
## 0.0.2
27

38
* bump dartcv to 0.0.2

0 commit comments

Comments
 (0)