OpenCV Swift Project - ios

I am currently using OpenCV in a Swift project. I have no experience using the library and I have some questions.
Is it possible to use Swift to directly communicate with the OpenCV library?
If not:
I have used Cocoa Pods to download the OpenCV framework. Do I still need to write an Objective-C++ wrapper?

No, OpenCV requires you to use Objective-C++
Yes you do, you can write your interface and native code in swift but you still have to bridge it with whatever you wanna do in OpenCV.

Related

Creating and exporting a Metal library

I see that Apple documentation usually focuses on creating Metal libraries that can then be loaded using the Metal API from Swift or Objective-C, but I am trying to create a library that can provide utility Metal functions to projects that would import it (so you would #include <MyLibrary/Header.h>. Unfortunately I can't find anywhere how to do this:
I see no clear way of doing this as part of a Swift Package (which is what I would have preferred), since I would need a way to tell Xcode that the package contains a Metal Library it should link with the current project library
I also tried the "Metal Library" template in Xcode, but I don't understand how it should be used.
What is the correct way of doing this?

How to use sunlubo/SwiftFFmpeg library in iOS swift?

I am trying to use https://github.com/sunlubo/SwiftFFmpeg library in iOS to convert mp4 file to mpegts. But I am getting below error when trying to add it using SPM:
& No such module 'CFFmpeg' error when trying to add the library manually as used in the Demo project(https://github.com/sunlubo/SwiftFFmpegDemo-iOS). Can anyone provide me the steps or example on how to use this library for iOS?
I am using a M1 Macbook, Xcode 13, SwiftFFmpeg library version(1.0.5), Swift version(5.4)
Thanks in advance!
you can use Mobile ffmpeg cocoa pods like this:
pod 'mobile-ffmpeg-full-gpl'
see this answer https://stackoverflow.com/a/55312118/17927980 that's very helpful

SIFT feature detection in C

I am working on a SIFT feature detection in Opencv. The problem is the libraries supporting this project (ex: features2d,nonfree) are available only in .hpp and I need to write in C. So any suggestions?
I'm hoping that I don't have to translate the libraries.
.hpp usually indicates a C++ header. If you really need a C interface, write a wrapper library. But I would just use a C++ compiler for the whole project.

Using data types from a objective-c++ framework in a swift datamodel

I want to use the openCV2 framework in my iOS app, however, I am more comfortable with swift. I can read and understand obj-c well enough to understand the framework and write the methods I need from it in a bridging header, but I'm not comfortable writing the whole app in obj-C. Unfortunately, there are some data types, (cv::Mat, cv::MserManager, etc) that I will need to use in my datamodel, or possibly elsewhere. Is there a way to include datatypes in my bridging header so that I can work with them in swift?
You cannot use C++ types in code called from Swift. However, you can use them in Objective-C++ files, the ones that have the .mm extension. You can mix Objective-C and C++ code in Objective-C++, and can expose Objective-C methods that don't reference C++ in their declarations to Swift via the bridging wrapper. These functions can still use C++ in their implementations, which are not visible in Swift via the bridging header.
You also need to be careful about the language linkage (remember extern "C"?).
Here are some answers that provide examples:
1) Video processing with OpenCV in IOS Swift project
2) Include C++ header file in Swift
3) How to access Swift-objects from a c++ class?
Unfortunately, you cannot bridge C++ and Objective-C++ directly into Swift. On the bright side, you can still work with the openCV2 framework in your app, but you'll need to write C or Objective-C wrappers for your types as described in a related question here: Can I mix Swift with C++? Like the Objective - C .mm files

using an ios framework for native c++ code for ios

I have written a c++ library that needs opencv which is an image processing library. I want to now use this c++ library on ios. To do that I am going to copy my code to a mac and build to produce a cocoa touch static library.
Since, this has a dependency on opencv, I downloaded its ios framework. But now I am confused whether a framework can be used from c++ code or just from objective c/c++ ? Do I have to recompile this library so that i get c++ libraries or I can use the framework in my c++ code?
Yes, it can be used with c++. You will have to make sure you Type is set to "Objective C++ Source" for where you are making the framework calls.
I mix my C++ code with frameworks all the time.
Note this goes both ways. If you have Obj-C interacting with C++, you'll need to either have the file be a .mm or be of the "Objective C++ Source" Type.
The Type selection is in the File Inspector for files.

Resources