Skip to content
项目

foundry/OpenCVSwiftStitch

在 GitHub 查看

关于

OpenCVStitch 第 6 版。介绍如何在 iOS 上使用 OpenCV 拼接图像。本版本演示如何在同一个项目中结合使用 Swift 和 C++,无需再使用 Swift v5.9 之前所需的 Objective-C 包装器。

设计与媒体

这些数字如何统计

实测星标增长

24小时
0
7天
0
15天
0
1个月
正在收集历史

各周期内的星标净变化,周期相互重叠,并非每日新增。缺失的测量保持空白。星标代表关注度,不代表软件质量。

README.md

原文

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/OutputArray gotcha

OpenCV's InputArray/OutputArray are 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 with cv::InputArray cvInputArray(cv::Mat& m) { return m; } — which compiled and ran, but returns a reference to a temporary (undefined behaviour); it only "worked" because _InputArray stays valid while its backing Mat is alive. The wrapper keeps these proxy types inside C++ and exposes only plain cv::Mat& to Swift. (The helpers are now gone, replaced by OpenCV's own UIImageToMat / MatToUIImage.)

This lifetime dependency is now expressible rather than only conventional: Swift 6.2's safe C++ interop can import _InputArray as SWIFT_NONESCAPABLE and mark the Mat→proxy bridge __lifetimebound (from <swift/bridging>), so the compiler enforces that the proxy can't outlive its Mat. 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.9OpenCV 3.0.0OpenCV 3.1.0OpenCV 4.3.0
v2_inset2 v3_inset2 v3.1_inset2 v5_inset2
v2_inset2 v2_inset2 v2_inset2 v5_inset2

探索相关项目

加载中…