OpenCV computer vision with iOS: stitching panoramas
Version 6.0 of OpenCVStitch
This update implements Swift/C++ interoperabilty as announced at WWDC 2023. Swift now interfaces directly with the C++ OpenCV API. All traces of Objective-C code have been removed.
This project was created to a answer a couple of Stack Overflow questions:
It demonstrates how to mix Swift and C++ in one project whilst keeping the code clearly separate.
Wrapper
For Swift versions < 5.9 an Objective-C wrapper was required to interface between Swift and C++. Swift 5.9 allows us to dispense with Objective-C altogether. Swift can call the C++ APIs in the OpenCV framework directly.
A small C++ wrapper is still used as a workaround for OpenCV API types that can't be accessed directly or are not yet available from Swift.
Note — the
InputArray/OutputArraygotcha
OpenCV's
InputArray/OutputArrayare reference typedefs (const _InputArray&) over a non-owning proxy, not value types. Early Swift/C++ interop (5.9) couldn't pass them at all, and an early wrapper bridged withcv::InputArray cvInputArray(cv::Mat& m) { return m; }— which compiled and ran, but returns a reference to a temporary (undefined behaviour); it only "worked" because_InputArraystays valid while its backingMatis alive. The wrapper keeps these proxy types inside C++ and exposes only plaincv::Mat&to Swift. (The helpers are now gone, replaced by OpenCV's ownUIImageToMat/MatToUIImage.)
This lifetime dependency is now expressible rather than only conventional: Swift 6.2's safe C++ interop can import
_InputArrayasSWIFT_NONESCAPABLEand mark theMat→proxy bridge__lifetimebound(from<swift/bridging>), so the compiler enforces that the proxy can't outlive itsMat. Hiding the proxies is still the simplest choice here; the annotations are the sanctioned route if they are ever exposed directly.
Bridging Header To access the C++ wrapper from Swift in the same module it is imported via a bridging header.
Installation
Swift Package Manager
This version imports OpenCV as a Package Dependency from https://github.com/yeatse/opencv-spm
The Package will install OpenCV v4.13.0 (or later)
Alternatively, you can manually install the OpenCV framework from OpenCV.org
Use
OpenCVStitch is a very simple iOS/openCV example showing basic use of the Stitcher class. The c++ code is adapted from a sample included with the openCV distribution.
The app has almost no user interface. On launch, the stitching code operates on four sample images, displaying the result in a UIScrollView.
OpenCVSwiftStitch Versions
Version 6.0
Swift/C++ interop
OpenCV >=4.13.0
Swift package manager integration
Tested with XCode 15.0-26.6 / Swift 5.9-6.2 for iOS 15-26
Version 5.1
Swift / Objective-C / C++
OpenCV 4.3
Modern Swift - async / await
Error, exception handling from C++ -> Objective-C -> Swift
Tested with XCode 13.2 / Swift 5.0 for iOS 13.0+
Version 5.0
Swift / Objective-C / C++
OpenCV 4.3
Tested with XCode 12.0 / Swift 5.0 for iOS 9.0+
Version 4.0
Swift / Objective-C / C++
Tested with XCode 10.0 / Swift 4.2 for iOS 8.0+
Version 3.0
Swift / Objective-C / C++
Tested with XCode 8.0 / Swift 3.0 for iOS 8.0+
Version 2.1
Swift / Objective-C / C++
Tested with XCode 7.0 / Swift 2.0 for iOS 7.0+
Version 2.0
Swift / Objective-C / C++
Tested with XCode 6.4 / Swift 1.2 for iOS 7.0+
Version 1.0
Objective-C / C++
Tested with XCode 4.5.2 -> 6.3 for iOS 5.1 upwards
Provides a partial answer to: Libraries to capture panoramas in iOS 6 (Stack Overflow)
OpenCV Versions
OpenCV 4.8.1
The Swift package will bring in the most recent stable version of OpenCV
OpenCV 4.3
The podfile installs OpenCV 4.3
this version of OpenCVStitch opted to use cocoapods to overcome the notorious installation issues with previous versions of the framework
XCode 15
Project is now updated for Swift 5.9 and XCode 15. For backwards compatibility checkout the 2.0 / 2.1 branches, 3.0/4.0/5.0 releases or refer to the Objective-C version v1.0.
Comparisons
| OpenCV 2.4.9 | OpenCV 3.0.0 | OpenCV 3.1.0 | OpenCV 4.3.0 |
|
|
|
|
|
|
|
|








