TrainData.create constructor

TrainData.create(
  1. Mat samples,
  2. int layout,
  3. Mat responses, {
  4. Mat? varIdx,
  5. Mat? sampleIdx,
  6. Mat? sampleWeights,
  7. Mat? varType,
})

Creates training data from in-memory arrays.

samples must be CV_32F, responses CV_32F or CV_32S. The optional arrays are ignored when omitted.

Implementation

factory TrainData.create(
  Mat samples,
  int layout,
  Mat responses, {
  Mat? varIdx,
  Mat? sampleIdx,
  Mat? sampleWeights,
  Mat? varType,
}) {
  // Named locals, not `(varIdx ?? empty).ref` in the call: a `.ref` view of a temporary wrapper
  // can be freed by a GC before the native call reads it (#464).
  final empty = Mat.empty();
  final idx = varIdx ?? empty;
  final sIdx = sampleIdx ?? empty;
  final weights = sampleWeights ?? empty;
  final types = varType ?? empty;
  final p = calloc<cml.TrainData>();
  try {
    cvRun(
      () => cml.cv_ml_TrainData_create(
        samples.ref,
        layout,
        responses.ref,
        idx.ref,
        sIdx.ref,
        weights.ref,
        types.ref,
        p,
      ),
    );
  } catch (_) {
    calloc.free(p);
    rethrow;
  } finally {
    empty.dispose();
  }
  return TrainData._(p);
}