Forum Xamarin.iOS

Using OpenCV with Xamarin.iOS

trinnguyentrinnguyen VNMember ✭✭

Hi, I have found some C# wrapper libraries for OpenCV, and EmguCV can be used for MonoTouch. I must buy Commercial License.

Is there any free library wrapper for opencv ?

Posts

  • LeighBowersLeighBowers GBMember

    Did you ever find a free solution?

  • trinnguyentrinnguyen VNMember ✭✭

    yeah, i had solution for this.
    I use OpenCV 2.4.4 on native and write Objective-C static library work with OpenCV. Then, binding this library from C#, you can research with http://docs.xamarin.com/guides/ios/advanced_topics/binding_objective-c_libraries

    When built static library from Objective-C, you will get a .a file, like libSmartImage.a .You also must add OpenCV to binding project from C#, rename file opencv2 in opencv2.framework/Version to opencv2.a

    Overall, you must add two files libName.a and opencv2.a to Binding project of Xamarin.iOS.
    And with opencv2.a file binding, use must add to link file:
    LinkWith ("opencv2.a", LinkTarget.ArmV7 | LinkTarget.Simulator, ForceLoad = true, IsCxx = true, LinkerFlags = "-ObjC -lz -licucore -lc++", Frameworks="QuartzCore AVFoundation CoreVideo AssetsLibrary CoreGraphics CoreMedia")]

    Good luck

  • Hi TriNguyen, I have a native library that another developer created that uses OpenCV. This developer is unavailable, and I'm trying to create a binding library in Xamarin so I can use this library in my iOS project. I've gone through the binding tutorials and walkthroughs, and I have a base binding project that is linking this said native library, however, I'm getting tons of compile errors on the project that is referencing the binding project (the binding project compiles just fine).

    I was wondering if you would be willing to post / provide the source to your binding project that links a native library that uses OpenCV? I'm not familiar enough with OpenCV to understand exactly what I need to hook up in the binding project. I tend to learn best by dissecting projects and code. If you can provide a sample binding project that you were successful with creating linking a native library using OpenCV, I'd greatly appreciate it.

    Thanks.

  • trinnguyentrinnguyen VNMember ✭✭

    Hi Robby,

    I can't provide source code now, i will try to make demo project how to use OpenCV in Xamarin. But i think it isn't hard for you to follow:

    • Learn how to binding static Object-C library in Xamarin
    • Wrapper you native library (worked with OpenCV) to a ObjC static library. I guess that your native library is in C language
    • Wrapper ObjC static library in C#

    I got success with OpenCV 2.4.4, haven't tried higher version.

  • Hi TriNguyen, thank you for your response. Don't worry about putting that demo project together. I was able to contact the developer that created the native library and we worked out the issues with using it in a binding project. What you've described in this thread is key to getting native libraries using opencv working in a binding project.

    Just to describe how I was able to get this to work, I used Jarina's sample project as a base project, which he provides here:
    http://forums.xamarin.com/discussion/comment/5371/#Comment_5371

    In the AssemblyInfo.cs file, I added these two lines to link both the native library, and opencv:

    [assembly: LinkWith ("libnativelibrary.a", LinkTarget.ArmV7 | LinkTarget.Simulator, IsCxx = true, ForceLoad = true)]

    [assembly: LinkWith ("opencv2.a", LinkTarget.ArmV7 | LinkTarget.Simulator, ForceLoad = true, IsCxx = true, LinkerFlags = "-ObjC -lz -licucore -lc++", Frameworks="QuartzCore AVFoundation CoreVideo AssetsLibrary CoreGraphics CoreMedia")]

    (replace the "libnativelibrary.a" reference to the native library you want to use)

    NOTE: the "opencv2.a" file was obtained by grabbing the latest iOS version of OpenCV from the internet, then following TriNguyen's step above on renaming the library to use in the project.

    I then created my own class to use for defining the entry points (you can use Jarina's "Foo.cs" class as a guide).

    Although the binding project compiled fine each time I updated it, I was having a ton of compile issues with the project referencing the binding library, all relating to the opencv linkage. I can't say exactly what fixed those issues because it required hours of back and forth with the native library developer to modify the native library, but what I can say is that removing _main in the native library fixed most of the issues.

    Also, at one point I was getting a lot of "surf.o" errors. I found this page that describes how to remove these references from opencv:
    http://twolivesleft.com/Codea/Talk/discussion/2736/opencv/p1

    I don't remember if this was needed or not, but just in case someone else runs into these same issues here's something to try.

    TriNguyen, just wanted to say thank you for your posts in this thread. It definitely helped me in figuring out my issues with opencv in native libraries.

  • BrunoOliveiraBrunoOliveira BRMember

    Hi @TriNguyen‌,

    Did you get the chance to make the demo project with OpenCV!?
    I'm starting my research now on how to use it with xamarin android and ios.

    Thanks,
    Bruno

  • trinnguyentrinnguyen VNMember ✭✭

    Hi @AwakenDeveloper, I'm sorry that i was busy at that time, haven't seen your reply till now. Thanks for updates, it will help much :)

    @BrunoOliveira: I just completed a cross-platform project with Xamarin(iOS, Android) and Windows Phone 8, working with OpenCV. The latest version of OpenCV now is very good when integrate with Xamarin, don't have much issues when building as before. You should follow the guide of some posts above.

    I haven't completed a demo project as i talked in the previous post :) But when you work with OpenCV on Xamarin, if having any issue, please post here and i will help.

    Good luck,

  • ClaudioSanchezClaudioSanchez USUniversity, Developer Group Leader ✭✭

    @TriNguyen‌: Please do write about using OpenCV with Xamarin.* - That would be very interesting.

  • trinnguyentrinnguyen VNMember ✭✭

    @ClaudioSanchez : Just created an example project on GitHub, hope it will help you, check here: https://github.com/trinnguyen/xamarin.ios-opencv

    Also i posted an entry on my own blog about detail steps, here: http://trinnguyen.com/2014/06/using-opencv-on-xamarin-ios/

  • ChrisHerbonChrisHerbon DEMember
    edited January 2015

    @TriNguyen‌ : I am very curious about your cross platform project. Is there any chance you could share with us how to get OpenCV running for Android and Windows Phone also?

    By the way: It looks like your website is not working anymore.

  • trinnguyentrinnguyen VNMember ✭✭

    @ChrisHerbon‌ : Yes, some tips here:
    [-] Android

    • OpenCV support Android already, with OpenCV-Android SDK. They have full tutorial here: http://docs.opencv.org/doc/tutorials/introduction/android_binary_package/O4A_SDK.html
    • You will need to learn NDK first for following this tutorial and make some changes if want to get it work without installing OpenCV Manager app from Google Play Store (load static library .so)
    • If you want to support Xamarin, just need to complete your wrapper library on Android first, then do a binding on Android.

    [-] Windows Phone: from WP8, Microsoft supports C/C++. We have 2 options here:

    • Using OpenCV from Microsoft Open Tech: https://github.com/MSOpenTech/opencv
    • You can build OpenCV for WP from original C source code. This way will take much time, but you can control what components should be included in your build (because there are some functions are missing in Microsoft build). It's possible, I did it successfully already ;)

    About Xamarin cross-platform architecture, depend on your design, just can share interfaces, have to implement for each platform. Btw, we can also share your logic code with OpenCV (in C/C++ code) via platforms.

    I will try to re-launch my blog and have some tutorials about this.

    Thanks,

    • Tri
  • trinnguyentrinnguyen VNMember ✭✭

    Hi @ChrisHerbon, I updated blog OpenCV with Xamarin.iOS here with detailed steps. http://trinnguyen.com/using-opencv-on-xamarin-ios/

    Xamarin.Android will be completed soon....

  • YannRoseauYannRoseau FRMember

    Hi @TriNguyen,
    Can you tell us when your item "Using OpenCV on Xamarin.Android" become avaible, please ?
    I started Xamarin recently, and I must finish our projet for university in less than a month.
    Thanks.

  • YannRoseauYannRoseau FRMember
    edited May 2015

    OK, I found this tutorial :

    I have just a problem on building (I use OpenCv 2.4.10) :

    Error MSB4018 :
    The "CreateNativeLibraryArchive" task failed unexpectedly.
    System.ArgumentNullException: Parameter "message" cannot be null.
    - Microsoft.Build.Shared.ErrorUtilities.VerifyThrowArgumentNull(Object parameter, String parameterName, String resourceName)
    - Microsoft.Build.Utilities.TaskLoggingHelper.LogWarning(String subcategory, String warningCode, String helpKeyword, String file, Int32 lineNumber, Int32 columnNumber, Int32 endLineNumber, Int32 endColumnNumber, String message, Object[] messageArgs)
    - Microsoft.Build.Utilities.TaskLoggingHelper.LogWarning(String message, Object[] messageArgs)
    - Xamarin.Android.Tasks.CreateNativeLibraryArchive.Execute()
    - Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute()
    - Microsoft.Build.BackEnd.TaskBuilder.d__20.MoveNext() (MSB4018) (OpenCv.Binding.Droid)

  • trinnguyentrinnguyen VNMember ✭✭

    Hi Yann, I have not had plan for publishing new blog about OpenCV for Xamarin.Android. I will notify here when have new.

    Btw, inbox me if you have details question. I am willing to support.

    To use OpenCV on Xamarin.Android, just follow these steps:
    1 - Wrap Java Android Library, contain classes with all actions that you want to work with OpenCV (like DetectFace, LearnPeople,...)
    2 - Binding above Java Android Library on Xamarin.Android, this is easy with document on Xamarin Developer Site.
    3 - Config binding project, for embed native OpenCV framework too.
    4 - Finish. Here we go, reference to this Xamarin.Android binding project and do interested stuffs.

    Good luck !

  • DiomedesDominguezDiomedesDominguez DOMember ✭✭

    @ClaudioSanchez said:
    @TriNguyen‌: Please do write about using OpenCV with Xamarin.* - That would be very interesting.

    @ClaudioSanchez do you have an implementation of OpenCV with Xamarin.Forms or do you recommend me to do the same as @trinnguyen did but for Xamarin.Android too?

Sign In or Register to comment.