A Flutter plug-in providing a binding to OpenCV-4.x.
pubspec.yaml of your flutter project, add the following dependency:
dependencies: ... opencv: "^1.0.2"
In your library add the following import:
All functions are currently contained in the ImgProc class.
Variables for integer choices (read pre-defined values/enums for border types, threshold types, etc.) are stored in ImgProc & Core classes, exactly similar to how OpenCV itself does. However, these variables are renamed to suit the lowerCamelCase Dart fashion (just to objectively not lose "health suggestion points" on pub.dev.
i.e. One of the border types is
can be used in
ImgProc.blur(someBytes, someIntArray, someIntArray, Core.borderReflect);
So, if you refer to the OpenCV docs or a tutorial, the function you're implementing in OpenCV is very much identical to that in flutter_opencv.
The only difference is how the data is managed - OpenCV uses the Mat class to store every image as a matrix.
flutter_opencv instead only works on data as an array of bytes.
The only difference in OpenCV's methods and ours - OpenCV functions take values of the source & destination matrices by reference. In Flutter, we use byte arrays instead of Mat() & we do not need to provide a destination byte array.
someFunction(sourceMatrix, destinationMatrix, otherValue1, otherValue2, ...) in OpenCV
is now instead
dynamic destinationMatrix = someFunction(sourceMatrix, otherValue1, otherValue2, ...) in flutter_opencv.
For simple thresholding, in OpenCV, you'd do
Imgproc.threshold(sourceMatrix, destinationMatrix, 80, 255, Imgproc.THRESH_BINARY);
Whereas in flutter_opencv you'd do
res = await ImgProc.threshold(await file.readAsBytes(), 80, 255, ImgProc.CV_THRESH_BINARY);
Input images & formats
Since we're not representing images as Mat(), we're going to need the byte data. I'll write down methods for each (Flutter) Image class here. For now,
- Image from File:
File file = await DefaultCacheManager().getSingleFile(_URL);will fetch the file from the internet.
await file.readAsBytes()can be directly passed to any of the functions, since it returns a byte array.
Storing result, temporarily
dynamic outputMatrix = someFunction(...) will store a byte array in outputMatrix.
Output images & formats
- Image from memory:
Image.memory(outputMatrix)will show the image obtained (as a byte array) from the above function.
Since you're never going to just implement one function, there's a need to be able to cascade. Inputs & outputs to all functions are common - byte arrays. Hence output from one function can directly be fed to the next.
FAQs related to the idea/concept behind this plug-in
Why not do a full binding?
I am actually planning to write a full binding and publish it separately later on. I understand that this implementation is lackluster & a lot of functions still need to be added. If you really need something, make a feature request.
Why not use the core OpenCV classes like Mat?
Because I wanted to provide a simple interface - one that does not require the user to learn OpenCV or it's API. Instead, much of the way the library works is designed around Flutter/Dart, making it easier for Flutter users to pick it up.
Does lesser functions (in comparison to a full binding) mean lower build size?
Unfortunately, no. The build size would be the same for this, or a fully bound version (which I'll work on after this project reaches certain level of maturity)
None yet. I'm still profiling the plug-in for most use cases, and haven't faced any issues with rendering so far. Will add benchmarks soon. PR/PM me if interested.
What OpenCV version is flutter_opencv built on top of?
As of now, the plug-in is built on top of the builds provided for OpenCV v4.1. I know that OpenCV v4.3 is out already, but an issue with the android exportable AAR file prevents me from hosting multiple versions right now.
In case you need to change your OpenCV version (read downgrade) while working with this library, you can change it the android/build.gradle file. Just change the dependency version to some other supported one for
Please keep in mind that the above build files (AARs) are provided by someone else - https://github.com/quickbirdstudios/opencv-android. I'm grateful to these guys, but unsure of how often (and far) they would maintain the build repositories, so I'm also working on putting multiple different versions of OpenCV builds on bintray - but it's low on the priority list for now.
With Flutter OpenCV
example directory for a complete sample app using Flutter OpenCV.
For help getting started with Flutter, view the online documentation.
- iOS not supported currently - I do not own a Mac, so deploying for iOS will be a little slow.
xSetup CI for docs
xDocument basic usage
xSetup CI for build quality/code cov
Please see the Changelog page to know what's recently changed.
Feel free to contribute to this project.