Skip to content

Commit 4af53ea

Browse files
committed
API Change: toList3D, more test
1 parent 403438d commit 4af53ea

File tree

3 files changed

+40
-23
lines changed

3 files changed

+40
-23
lines changed

lib/src/core/mat.dart

+19-19
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class Mat extends CvStruct<cvg.Mat> {
2929
/// Mat (Size size, int type, void *data, size_t step=AUTO_STEP)
3030
///
3131
/// https://docs.opencv.org/4.x/d3/d63/classcv_1_1Mat.html#a9fa74fb14362d87cb183453d2441948f
32-
factory Mat.fromList(int rows, int cols, MatType type, List data, [int step = 0]) {
32+
factory Mat.fromList(int rows, int cols, MatType type, List<num> data, [int step = 0]) {
3333
assert(data is List<int> || data is List<double>, "Only support List<int> or List<double>");
3434
final p = calloc<cvg.Mat>();
3535
// 1 copy
@@ -425,7 +425,7 @@ class Mat extends CvStruct<cvg.Mat> {
425425
///
426426
/// https://docs.opencv.org/4.x/d3/d63/classcv_1_1Mat.html#a7a6d7e3696b8b19b9dfac3f209118c40
427427
T at<T>(int row, int col, [int? i2]) {
428-
if (T == int || T == double) {
428+
if (T == int || T == double || T == num) {
429429
return atNum(row, col, i2) as T;
430430
} else if (isSubtype<T, CvVec>()) {
431431
return atVec<T>(row, col);
@@ -1282,21 +1282,21 @@ class Mat extends CvStruct<cvg.Mat> {
12821282
void release() => cvRun(() => CFFI.Mat_Release(ptr));
12831283

12841284
/// This Method converts single-channel Mat to 2D List
1285-
List<List<num>> toList() {
1286-
switch (type.depth) {
1287-
case MatType.CV_8U:
1288-
case MatType.CV_8S:
1289-
case MatType.CV_16U:
1290-
case MatType.CV_16S:
1291-
case MatType.CV_32S:
1292-
return List.generate(rows, (row) => List.generate(cols, (col) => at<int>(row, col)));
1293-
case MatType.CV_32F:
1294-
case MatType.CV_64F:
1295-
return List.generate(rows, (row) => List.generate(cols, (col) => at<double>(row, col)));
1296-
default:
1297-
throw UnsupportedError("toList() for $type is not supported!");
1298-
}
1299-
}
1285+
List<List<num>> toList() => switch (type.depth) {
1286+
MatType.CV_8U => List.generate(rows, (row) => List.generate(cols, (col) => atU8(row, col))),
1287+
MatType.CV_8S => List.generate(rows, (row) => List.generate(cols, (col) => atI8(row, col))),
1288+
MatType.CV_16U =>
1289+
List.generate(rows, (row) => List.generate(cols, (col) => atU16(row, col))),
1290+
MatType.CV_16S =>
1291+
List.generate(rows, (row) => List.generate(cols, (col) => atI16(row, col))),
1292+
MatType.CV_32S =>
1293+
List.generate(rows, (row) => List.generate(cols, (col) => atI32(row, col))),
1294+
MatType.CV_32F =>
1295+
List.generate(rows, (row) => List.generate(cols, (col) => atF32(row, col))),
1296+
MatType.CV_64F =>
1297+
List.generate(rows, (row) => List.generate(cols, (col) => atF64(row, col))),
1298+
_ => throw UnsupportedError("toList() for $type is not supported!")
1299+
};
13001300

13011301
/// Returns a 3D list of the mat, only for multi-channel mats.
13021302
/// The list is ordered as [row][col][channel].
@@ -1310,10 +1310,10 @@ class Mat extends CvStruct<cvg.Mat> {
13101310
/// final list = mat.toList3D<Vec3b, int>();
13111311
/// print(list); // [[[0, 1, 2], [3, 4, 5], [6, 7, 8]]]
13121312
/// ```
1313-
List<List<List<P>>> toList3D<T extends CvVec, P extends num>() {
1313+
List<List<List<num>>> toList3D<T extends CvVec>() {
13141314
assert(channels >= 2, "toList3D() only for channels >= 2, but this.channels=$channels");
13151315
return List.generate(
1316-
rows, (row) => List.generate(cols, (col) => at<T>(row, col).val as List<P>));
1316+
rows, (row) => List.generate(cols, (col) => at<T>(row, col).val));
13171317
}
13181318

13191319
/// Get the data pointer of the Mat, this getter will reture a view of native

test/core/core_test.dart

+20-3
Original file line numberDiff line numberDiff line change
@@ -339,21 +339,38 @@ void main() async {
339339
for (int channel in [1, 2, 3, 4]) {
340340
for (var depth in depthSrc) {
341341
final srcType = cv.MatType.makeType(depth, channel);
342-
final src = cv.Mat.zeros(3, 3, srcType);
342+
final src = cv.Mat.randu(3, 3, srcType, low: cv.Scalar.all(0), high: cv.Scalar.all(255));
343343
final lutSize = switch (depth) {
344344
cv.MatType.CV_8U || cv.MatType.CV_8S => 256,
345345
cv.MatType.CV_16U || cv.MatType.CV_16S => 65536,
346346
_ => throw Exception("Unsupported type"),
347347
};
348348
for (var lutDepth in depthLut) {
349349
final lutType = cv.MatType.makeType(lutDepth, channel);
350-
final lut = cv.Mat.fromScalar(1, lutSize, lutType, cv.Scalar(255, 241, 21, 0));
351-
testOneLUT(src, lut);
350+
// 0-1: 65536-1-0 2-3: 65536-1-1 3-4: 65536-1-2
351+
final lutData = switch (lutDepth) {
352+
cv.MatType.CV_32F || cv.MatType.CV_64F => List.generate(
353+
lutSize * lutType.channels, (i) => (lutSize - (i ~/ channel) - 1).toDouble()),
354+
_ => List.generate(lutSize * lutType.channels, (i) => lutSize - (i ~/ channel) - 1),
355+
};
356+
final lutInverse = cv.Mat.fromList(1, lutSize, lutType, lutData);
357+
testOneLUT(src, lutInverse);
352358
}
353359
}
354360
}
355361
});
356362

363+
test('cv.LUT 1', () {
364+
final mat = cv.imread("test/images/lenna.png", flags: cv.IMREAD_COLOR);
365+
final src = mat.convertTo(cv.MatType.CV_16UC3, alpha: 65536.0/255.0);
366+
final lutData = List.generate(65536 * 3, (i) => 65536 - (i ~/ 3) - 1);
367+
final lut = cv.Mat.fromList(1, 65536, cv.MatType.CV_16UC3, lutData);
368+
final dst = cv.LUT(src, lut);
369+
expect(dst.isEmpty, equals(false));
370+
expect(dst.shape, src.shape);
371+
// cv.imwrite("lut.png", dst.convertTo(cv.MatType.CV_8UC3, alpha: 255.0/65536.0));
372+
});
373+
357374
test('cv.magnitude', () {
358375
final src1 = cv.Mat.randu(4, 4, cv.MatType.CV_32FC1);
359376
final src2 = cv.Mat.randu(4, 4, cv.MatType.CV_32FC1);

test/core/mat_test.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ void main() async {
3232
mat3.rows,
3333
(row) => List.generate(
3434
mat3.cols, (col) => List.generate(mat3.channels, (c) => row == col && c == 0 ? 1 : 0)));
35-
expect(mat3.toList3D<cv.Vec3b, int>(), expected3);
35+
expect(mat3.toList3D<cv.Vec3b>(), expected3);
3636

3737
final mat4 = cv.Mat.ones(100, 100, cv.MatType.CV_8UC3);
3838
expect((mat4.width, mat4.height, mat4.channels), (100, 100, 3));

0 commit comments

Comments
 (0)