Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

OpenCV with Xamarin.Forms

I would like to use OpenCV in a Xamarin.Forms app. OpenCV already provides binaries for Android (as .so files) and iOS (as a framework). I don't need all of OpenCV, so I think that the best way to go about it is to use these provided binaries to create my own library (perhaps in C++) that exposes only the functionality I need. I could then create a C# wrapper for this library that could be used by my Xamarin.Forms app. However, I don't know if this is the best approach, and even if it is, I don't know how to go about it.

A similar question was asked here. However, the answer talks about the difficulty in binding all of OpenCV. I don't need (or want) that. I want to use the binaries provided by OpenCV to create my own library, and then use that. I think that would be easier than trying to bind all of OpenCV.

Tagged:

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    You could build your own projects with OpenVC and expose any apis as you want without any problems. Then use binding library to convert this native project to the libraries can be consumed by Xamarin.
    But if you involve the whole OpenVC in your own project, it will not decrease the library's size even though you expose the fewer functionalities. I think it's better to use the bound libraries for Xamarin instead of your own. Anyway, it's all up to you.

  • softlionsoftlion FRBeta ✭✭✭
    edited May 2019

    Just use the whole OpenCV and activate the link all compiler option. Only required code will stay in your app.

  • ohgabbeyohgabbey Member ✭✭

    Hello. Have you found a solution to implement OpenCV in your Xamarin.Forms application?

  • CarlosAndersonCarlosAnderson USMember ✭✭

    @ohgabbey Yes, I found a way to make it work, but the problem is that the final binaries are very large (~ 200 MB). I haven't found a way to make this smaller, although I haven't tried activating the "link all" option as suggested above.

  • jtthjtth Member ✭✭
    > @CarlosAnderson said:
    > @ohgabbey Yes, I found a way to make it work, but the problem is that the final binaries are very large (~ 200 MB). I haven't found a way to make this smaller, although I haven't tried activating the "link all" option as suggested above.

    Hi Carlos could you share your implementation? We’re curious about using image processing tools from OpenCV in XF.
  • jtthjtth Member ✭✭
    > @softlion said:
    > Just use the whole OpenCV and activate the link all compiler option. Only required code will stay in your app.

    Can you elaborate? Would be interested in doing this
  • jtthjtth Member ✭✭
    edited April 12
    > @CarlosAnderson said:
    > @ohgabbey Yes, I found a way to make it work, but the problem is that the final binaries are very large (~ 200 MB). I haven't found a way to make this smaller, although I haven't tried activating the "link all" option as suggested above.

    Hi Carlos could you share your implementation? We’re curious about using image processing tools from OpenCV in XF.


    > @softlion said:
    > Just use the whole OpenCV and activate the link all compiler option. Only required code will stay in your app.

    Can you elaborate? Would be interested in doing this


    > @ohgabbey said:
    > Hello. Have you found a solution to implement OpenCV in your Xamarin.Forms application?

    Any luck on this?
  • CarlosAndersonCarlosAnderson USMember ✭✭
    edited April 12

    @jtth said:

    @CarlosAnderson said:
    @ohgabbey Yes, I found a way to make it work, but the problem is that the final binaries are very large (~ 200 MB). I haven't found a way to make this smaller, although I haven't tried activating the "link all" option as suggested above.

    Hi Carlos could you share your implementation? We’re curious about using image processing tools from OpenCV in XF.

    I have it working for Android, but I think the steps will be similar for iOS. I developed my solution based on these resources:

    Using Visual Studio 2019, I added a "Dynamic Shared Library (Android)" C++ project to my Xamarin.Forms solution. In this project, I wrote my own API that does the processing using C++ and OpenCV. I then wrapped those classes by functions that don't depend on OpenCV (for example, they return uchar* instead of cv::Mat).

    I then created a "Shared Project" (C#) to wrap the C++ API so that it may be called from C#. This project contains a class with a bunch of DllImport statements that bind to the C++ API.

    In my Xamarin.Forms project, I created an IVisionService that is implemented in the Xamarin.Android project a by VisionService class that makes calls to the C# wrapper. In this class, I also handle working with IntPtr (which is what I get back instead of uchar*) and converting it to an image I can use in Xamarin. In my case, I decided to work with SkiaSharp, so that my images are converted to SKBitmap.

    I hope this helps, and I'd be happy to help if you get stuck.

  • jtthjtth Member ✭✭
    > @CarlosAnderson said:
    > (Quote)
    > I have it working for Android, but I think the steps will be similar for iOS. I developed my solution based on these resources:
    >
    > * https://docs.microsoft.com/en-us/xamarin/cross-platform/cpp/
    > * https://github.com/xamcat/mobcat-samples/tree/master/cpp_with_xamarin
    > * https://channel9.msdn.com/Shows/XamarinShow/Bring-Some-C-to-Xamarin-Apps
    > * https://www.codeproject.com/Articles/18032/How-to-Marshal-a-C-Class
    >
    > Using Visual Studio 2019, I added a "Dynamic Shared Library (Android)" C++ project to my Xamarin.Forms solution. In this project, I wrote my own API that does the processing using C++ and OpenCV. I then wrapped those classes by functions that don't depend on OpenCV (for example, they return uchar* instead of cv::Mat).
    >
    > I then created a "Shared Project" (C#) to wrap the C++ API so that it may be called from C#. This project contains a class with a bunch of DllImport statements that bind to the C++ API.
    >
    > In my Xamarin.Forms project, I created an IVisionService that is implemented in the Xamarin.Android project a by VisionService class that makes calls to the C# wrapper. In this class, I also handle working with IntPtr (which is what I get back instead of uchar*) and converting it to an image I can use in Xamarin. In my case, I decided to work with SkiaSharp, so that my images are converted to SKBitmap.
    >
    > I hope this helps, and I'd be happy to help if you get stuck.


    Awesome! Wanting something that will work for both Android and iOS. For functions such as thresholding, masking, circleFit, etc. Would you be interested in putting up your Android project / wrapper in a github repo we could sample? Seems like we have our work cut out for us. Thanks!
  • MrAKMrAK Member

    https :/ /www. youtube .com/watch?v=pFv_1KeMZVs

    Are the steps you followed similar to this tutorial?

  • CarlosAndersonCarlosAnderson USMember ✭✭

    @MrAK said:
    https :/ /www. youtube .com/watch?v=pFv_1KeMZVs

    Are the steps you followed similar to this tutorial?

    Not really. They're using Java bindings to make it work. I coded everything related to OpenCV in C++ and exposed a simple API to interop with C#.

Sign In or Register to comment.