@@ -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 );
0 commit comments