Ask Your Question
0

Unity iOS C++ plugin that itself relies on OpenCV

asked 2016-10-28 08:05:09 -0500

Reeven gravatar image

updated 2016-10-28 08:17:52 -0500

From my understanding, basic iOS C++ plugins in Unity are dead simple. Simply jot down working code in a .mm text file and shove that file into Assets/Plugin/iOS. Even simpler than making a windows .dll plugin.

But... what about an iOS plugin that itself makes calls to another framework like, say, OpenCV for iOS? How do you include that? The documentation beyond the most basic examples like the one I linked is sparse. Do I just dump opencv2.framework into Assets/Plugin/iOS and cross my fingers my own C++ code will know to talk to OpenCV's through some kind of magic? I am pretty sure there must be some additional steps, probably once the generated project's gotten to Xcode's side of things. What would these steps be?

Also, includes and such: how would these work in the .mm file?

Any help or link to a tutorial or documentation pertinent to my use case highly appreciated. Thanks!

EDIT: As a point of comparison, here is the C++ .cpp code for my Editor / Windows version of things, its methods being called by Unity through [DllImport]. For Editor / Windows, this works perfectly :

#include "MEHSvm.h"

extern "C"
{
    // create global bow vocabulary
    cv::Ptr<cv::DescriptorMatcher> matcher = cv::DescriptorMatcher::create("BruteForce-Hamming");
    cv::Ptr<cv::DescriptorExtractor> extractor = cv::ORB::create();
    cv::Ptr<cv::FeatureDetector> detector = cv::ORB::create();
    cv::BOWImgDescriptorExtractor bowDE(extractor, matcher);

    cv::Ptr<cv::ml::SVM> svm = cv::ml::SVM::create();

    int Load(const char* svmPath, const char* vocabularyPath)
    {
        svm = svm->load(svmPath);

        cv::Mat vocabulary;
        cv::FileStorage storage(vocabularyPath, cv::FileStorage::READ);
        storage.getFirstTopLevelNode() >> vocabulary;
        storage.release();
        bowDE.setVocabulary(vocabulary);

        return svm->getType(); //should be 100 for C_SVC
    }

    int Predict(uchar* bytes, int width, int height, int type)
    {
        cv::Mat image = cv::Mat(height, width, type, bytes);

        std::vector<cv::KeyPoint> keypoints;
        detector->detect(image, keypoints);
        cv::Mat descriptors;
        bowDE.compute(image, keypoints, descriptors);
        if (descriptors.empty()) return -1;

        return svm->predict(descriptors);
    }
}
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2016-11-03 08:51:02 -0500

Reeven gravatar image

In Xcode I made a static library (a .a file) implementing the header and source for my Load and Predict methods. That static library was linked to the OpenCV for iOS framework. I had to make sure to copy the framework's Headers and Resources subfolders and opencv2 file, all located in Versions\A, back to the root of the framework.

I could then place the static library and the OpenCV for iOS framework in my Unity iOS plugin folder, and use the library from Unity script as I would a .dll or .so through [DllImport ("__Internal")].

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2016-10-28 08:05:09 -0500

Seen: 571 times

Last updated: Nov 03 '16