Skip to content

Enhance README and CMakeLists for OpenCV module customization #355

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
May 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/dartcv/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* add `MatType.elemSize` `MatType.elemSize1`
* remove deprecated `(double x, double y, double z).asPoint3f`
`(double x, double y).asPoint2f` `(int x, int y).asPoint` `VecPoint.toVecVecPoint`
* add `bin/gen_cmake_vars.dart` for generating optional module vars for CMake

## 1.1.3

Expand Down
89 changes: 89 additions & 0 deletions packages/dartcv/bin/gen_cmake_vars.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// ignore_for_file: avoid_dynamic_calls, avoid_print

import 'dart:io';

import 'package:yaml/yaml.dart';

const defaultModuleSettings = {
// "core": "ON", // not configurable
"calib3d": "ON",
"contrib": "ON",
"dnn": "ON",
"features2d": "ON",
"flann": "ON",
// "gapi", // disabled
"highgui": "OFF",
"imgproc": "ON",
"imgcodecs": "ON",
"objdetect": "ON",
"photo": "ON",
"stitching": "ON",
"video": "ON",
"videoio": "ON",
};

void main(List<String> args) {
final pubspecPath = Platform.script.resolve("../../../../pubspec.yaml");
final excludeModules = parseUserDefinedExcludeModules(pubspecPath.toFilePath());

final cmakeVars = StringBuffer("###DARTCV_GEN_CMAKE_VAR_BEGIN###\n");
for (final k in excludeModules.entries) {
cmakeVars.write("${k.key.toUpperCase()}=${k.value.toUpperCase()}\n");
}
cmakeVars.write("###DARTCV_GEN_CMAKE_VAR_END###");
print(cmakeVars);
}

/// Parse the user defined exclude modules from pubspec.yaml
///
/// Returns a list of excluded module names
Map<String, String> parseUserDefinedExcludeModules(String pubspecPath, {bool excludeMode = true}) {
try {
print("Generating cmake vars from $pubspecPath");
// Read the pubspec.yaml file
final File file = File(pubspecPath);
if (!file.existsSync()) {
print("$pubspecPath not found");
return defaultModuleSettings;
}

// Parse the YAML content
final String yamlContent = file.readAsStringSync();
final dynamic yamlMap = loadYaml(yamlContent);

// Navigate to the hooks.user_defines.dartcv4.exclude_modules section
if (yamlMap is YamlMap &&
yamlMap['hooks'] is YamlMap &&
yamlMap['hooks']['user_defines'] is YamlMap &&
yamlMap['hooks']['user_defines']['dartcv4'] is YamlMap) {
final dartcvDefines = yamlMap['hooks']['user_defines']['dartcv4'] as YamlMap;

final excludeModules = dartcvDefines['exclude_modules'] as YamlList? ?? YamlList();
// include is priority over exclude
final includeModules = dartcvDefines['include_modules'] as YamlList? ?? YamlList();

final include = includeModules
.map((dynamic module) => module.toString().toLowerCase())
.where((e) => defaultModuleSettings.containsKey(e))
.toList();
final exclude = excludeModules
.map((dynamic module) => module.toString().toLowerCase())
.where((e) => defaultModuleSettings.containsKey(e) && !include.contains(e))
.toList();

final result = {
for (final e in defaultModuleSettings.keys)
e: exclude.contains(e) ? "OFF" : defaultModuleSettings[e]!,
};

return result;
}

print("parse error");
return defaultModuleSettings;
} catch (e) {
// Return empty list in case of any error
print('Error parsing exclude_modules: $e');
return defaultModuleSettings;
}
}
67 changes: 29 additions & 38 deletions packages/dartcv/lib/src/calib3d/fisheye.dart
Original file line number Diff line number Diff line change
Expand Up @@ -107,25 +107,24 @@ class Fisheye {
distorted ??= Mat.empty();

cvRun(
() =>
Kundistorted == null
? ccalib3d.cv_fisheye_distortPoints(
undistorted.ref,
distorted!.ref,
K.ref,
D.ref,
alpha,
ffi.nullptr,
)
: ccalib3d.cv_fisheye_distortPoints_1(
undistorted.ref,
distorted!.ref,
Kundistorted.ref,
K.ref,
D.ref,
alpha,
ffi.nullptr,
),
() => Kundistorted == null
? ccalib3d.cv_fisheye_distortPoints(
undistorted.ref,
distorted!.ref,
K.ref,
D.ref,
alpha,
ffi.nullptr,
)
: ccalib3d.cv_fisheye_distortPoints_1(
undistorted.ref,
distorted!.ref,
Kundistorted.ref,
K.ref,
D.ref,
alpha,
ffi.nullptr,
),
);
return distorted;
}
Expand All @@ -142,25 +141,17 @@ class Fisheye {
distorted ??= Mat.empty();

return cvRunAsync0(
(callback) =>
Kundistorted == null
? ccalib3d.cv_fisheye_distortPoints(
undistorted.ref,
distorted!.ref,
K.ref,
D.ref,
alpha,
callback,
)
: ccalib3d.cv_fisheye_distortPoints_1(
undistorted.ref,
distorted!.ref,
Kundistorted.ref,
K.ref,
D.ref,
alpha,
callback,
),
(callback) => Kundistorted == null
? ccalib3d.cv_fisheye_distortPoints(undistorted.ref, distorted!.ref, K.ref, D.ref, alpha, callback)
: ccalib3d.cv_fisheye_distortPoints_1(
undistorted.ref,
distorted!.ref,
Kundistorted.ref,
K.ref,
D.ref,
alpha,
callback,
),
(c) => c.complete(distorted),
);
}
Expand Down
29 changes: 14 additions & 15 deletions packages/dartcv/lib/src/calib3d/usac_params.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,20 @@ class UsacParams extends CvStruct<cvg.UsacParams> {
int finalPolisher = NONE_POLISHER,
int finalPolisherIterations = 0,
}) {
final p =
calloc<cvg.UsacParams>()
..ref.confidence = confidence
..ref.isParallel = isParallel
..ref.loIterations = loIterations
..ref.loMethod = loMethod
..ref.loSampleSize = loSampleSize
..ref.maxIterations = maxIterations
..ref.neighborsSearch = neighborsSearch
..ref.randomGeneratorState = randomGeneratorState
..ref.sampler = sampler
..ref.score = score
..ref.threshold = threshold
..ref.final_polisher = finalPolisher
..ref.final_polisher_iterations = finalPolisherIterations;
final p = calloc<cvg.UsacParams>()
..ref.confidence = confidence
..ref.isParallel = isParallel
..ref.loIterations = loIterations
..ref.loMethod = loMethod
..ref.loSampleSize = loSampleSize
..ref.maxIterations = maxIterations
..ref.neighborsSearch = neighborsSearch
..ref.randomGeneratorState = randomGeneratorState
..ref.sampler = sampler
..ref.score = score
..ref.threshold = threshold
..ref.final_polisher = finalPolisher
..ref.final_polisher_iterations = finalPolisherIterations;
return UsacParams.fromPointer(p);
}

Expand Down
29 changes: 14 additions & 15 deletions packages/dartcv/lib/src/contrib/ximgproc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -590,21 +590,20 @@ class EdgeDrawingParams extends CvStruct<cvg.EdgeDrawingParams> {
double Sigma = 1.0,
bool SumFlag = true,
}) {
final p =
calloc<cvg.EdgeDrawingParams>()
..ref.AnchorThresholdValue = AnchorThresholdValue
..ref.EdgeDetectionOperator = EdgeDetectionOperator
..ref.GradientThresholdValue = GradientThresholdValue
..ref.LineFitErrorThreshold = LineFitErrorThreshold
..ref.MaxDistanceBetweenTwoLines = MaxDistanceBetweenTwoLines
..ref.MaxErrorThreshold = MaxErrorThreshold
..ref.MinLineLength = MinLineLength
..ref.MinPathLength = MinPathLength
..ref.NFAValidation = NFAValidation
..ref.PFmode = PFmode
..ref.ScanInterval = ScanInterval
..ref.Sigma = Sigma
..ref.SumFlag = SumFlag;
final p = calloc<cvg.EdgeDrawingParams>()
..ref.AnchorThresholdValue = AnchorThresholdValue
..ref.EdgeDetectionOperator = EdgeDetectionOperator
..ref.GradientThresholdValue = GradientThresholdValue
..ref.LineFitErrorThreshold = LineFitErrorThreshold
..ref.MaxDistanceBetweenTwoLines = MaxDistanceBetweenTwoLines
..ref.MaxErrorThreshold = MaxErrorThreshold
..ref.MinLineLength = MinLineLength
..ref.MinPathLength = MinPathLength
..ref.NFAValidation = NFAValidation
..ref.PFmode = PFmode
..ref.ScanInterval = ScanInterval
..ref.Sigma = Sigma
..ref.SumFlag = SumFlag;
return EdgeDrawingParams.fromPointer(p);
}

Expand Down
52 changes: 26 additions & 26 deletions packages/dartcv/lib/src/core/core_async.dart
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,13 @@ Future<Mat> bitwiseANDAsync(InputArray src1, InputArray src2, {OutputArray? dst,
dst ??= Mat.empty();
return mask == null
? cvRunAsync0((callback) => ccore.cv_bitwise_and(src1.ref, src2.ref, dst!.ref, callback), (c) {
return c.complete(dst);
})
return c.complete(dst);
})
: cvRunAsync0((callback) => ccore.cv_bitwise_and_1(src1.ref, src2.ref, dst!.ref, mask.ref, callback), (
c,
) {
return c.complete(dst);
});
c,
) {
return c.complete(dst);
});
}

/// BitwiseNot inverts every bit of an array.
Expand All @@ -98,11 +98,11 @@ Future<Mat> bitwiseNOTAsync(InputArray src, {OutputArray? dst, InputArray? mask}
dst ??= Mat.empty();
return mask == null
? cvRunAsync0((callback) => ccore.cv_bitwise_not(src.ref, dst!.ref, callback), (c) {
return c.complete(dst);
})
return c.complete(dst);
})
: cvRunAsync0((callback) => ccore.cv_bitwise_not_1(src.ref, dst!.ref, mask.ref, callback), (c) {
return c.complete(dst);
});
return c.complete(dst);
});
}

/// BitwiseOr calculates the per-element bit-wise disjunction of two arrays
Expand All @@ -114,13 +114,13 @@ Future<Mat> bitwiseORAsync(InputArray src1, InputArray src2, {OutputArray? dst,
dst ??= Mat.empty();
return mask == null
? cvRunAsync0((callback) => ccore.cv_bitwise_or(src1.ref, src2.ref, dst!.ref, callback), (c) {
return c.complete(dst);
})
return c.complete(dst);
})
: cvRunAsync0((callback) => ccore.cv_bitwise_or_1(src1.ref, src2.ref, dst!.ref, mask.ref, callback), (
c,
) {
return c.complete(dst);
});
c,
) {
return c.complete(dst);
});
}

/// BitwiseXor calculates the per-element bit-wise "exclusive or" operation
Expand All @@ -132,13 +132,13 @@ Future<Mat> bitwiseXORAsync(InputArray src1, InputArray src2, {OutputArray? dst,
dst ??= Mat.empty();
return mask == null
? cvRunAsync0((callback) => ccore.cv_bitwise_xor(src1.ref, src2.ref, dst!.ref, callback), (c) {
return c.complete(dst);
})
return c.complete(dst);
})
: cvRunAsync0((callback) => ccore.cv_bitwise_xor_1(src1.ref, src2.ref, dst!.ref, mask.ref, callback), (
c,
) {
return c.complete(dst);
});
c,
) {
return c.complete(dst);
});
}

/// BatchDistance is a naive nearest neighbor finder.
Expand Down Expand Up @@ -856,11 +856,11 @@ Future<(Scalar mean, Scalar stddev)> meanStdDevAsync(InputArray src, {InputArray
final stddev = calloc<cvg.Scalar>();
return mask == null
? cvRunAsync0((callback) => ccore.cv_meanStdDev(src.ref, mean, stddev, callback), (c) {
return c.complete((Scalar.fromPointer(mean), Scalar.fromPointer(stddev)));
})
return c.complete((Scalar.fromPointer(mean), Scalar.fromPointer(stddev)));
})
: cvRunAsync0((callback) => ccore.cv_meanStdDev_1(src.ref, mean, stddev, mask.ref, callback), (c) {
return c.complete((Scalar.fromPointer(mean), Scalar.fromPointer(stddev)));
});
return c.complete((Scalar.fromPointer(mean), Scalar.fromPointer(stddev)));
});
}

/// Merge creates one multi-channel array out of several single-channel ones.
Expand Down
Loading