rainyl / opencv_dart

OpenCV bindings for Dart language and Flutter. Support Asynchronous Now!
https://pub.dev/packages/opencv_dart
Apache License 2.0
136 stars 18 forks source link

How to convert or interpret different specific variable types between each other? #220

Closed frg-x closed 2 months ago

frg-x commented 3 months ago

Question

There are many specific variable types in the opencv_dart package. How can I convert or interpret different specific variable types between each other, such as Mat to VecMat, or List to VecPoint?

For instance, I need to use the calcHist() method, but how can I pass a VecMat as the first parameter when I only have a Mat? How do I pass VecI32, VecF32, VecVecPoint or how do I create these types of variables from other Dart native variable types?


Mat calcHist(
  VecMat src,
  VecI32 channels,
  Mat mask,
  VecI32 histSize,
  VecF32 ranges, {
  Mat? hist,
  bool accumulate = false,
}) {
  hist ??= Mat.empty();
  cvRun(
    () => cimgproc.CalcHist(
      src.ref,
      channels.ref,
      mask.ref,
      hist!.ref,
      histSize.ref,
      ranges.ref,
      accumulate,
    ),
  );

  return hist;
}```
rainyl commented 3 months ago

All classes extend Vec (except those extend CvVec like Vec3b) stand for vector wrappers, i.e., VecMat is a wrapper of vector<Mat>.

The Vec has a constructor Vec.fromList(), which allows you construct a Vec from a dart List, you can also try to call extension methods like cvd or asList to construct them. (But I gradually realize that .cvd may not be a good method name, may change it to asVec or something in the future.)

You can refer to test/ to find the examples.

https://github.com/rainyl/opencv_dart/blob/62d50b2a6b84d00ef58b1148c5db8610e4a4c8f8/test/imgproc/imgproc_test.dart#L192-L197

rainyl commented 3 months ago

Also, as for calcHist, according to the document of official opencv, https://docs.opencv.org/4.x/d6/dc7/group__imgproc__hist.html#ga4b2b5fd75503ff9e6844cc4dcdaed35d , the input must be a vector or a pointer, so you have to wrap your single image to a VecMat, e.g.,

final myimg = ...;
final VecMat imgs = cv.VecMat.fromList([myimg]);
// or
final VecMat imgs = [myimg].cvd;
frg-x commented 3 months ago

Thanks a lot! 👍

rainyl commented 2 months ago

Extension asVec() was added for List such as List<Point> and List<Mat> in v1.2.2, if no further problems, I am going to close this issus, feel free to reopen it if you still have any questions.