Swift and OpenCV: a teaser
If you want to perform image processing in Swift, then you may want to integrate the OpenCV library into your app.
OpenCV is a free open source library for image processing. It’s certainly not the only image processing library, but it’s used widely by people who write C++ or Python code for image processing. And free is a good price.
OpenCV has fast, mature, and robust algorithms for many uses beyond those I’ve written about in the past, such as calculation of perspective transforms. My own Swift image processing and data-crunching algorithms have served me well for a few years, but OpenCV functions offer greater flexibility. They tend to run faster, too.
Integrating OpenCV into an iOS project is easier than it used to be. However, developers who know Swift better than they know C++ and/or Python may find it tough to work with OpenCV. Hence I plan to provide some help to developers who want to do more image processing in Swift.
And . . . I’ll get to that in future articles.
In the meantime, if there is a particular type of image processing task you want to perform, or if you want help calling a particular OpenCV function from Swift, please post a comment.
As examples, I’ve written Swift wrapper functions to make it easier to work with the following algorithms implemented in OpenCV:
- findHomography() and AKAZE to find perspective transforms (similar to findHomography() and SIFT)
- grabCut() as an interactive method to separate a foreground object from the background
- k-means clustering to reduce an image to a specific number of colors
- mean shift, which is like k-means but doesn’t require specifying the number of clusters in advance
- flood fill, which takes a seed point (a starter point) and keeps filling neighboring pixels until it reaches a stopping condition; this is similar to a paint bucket fill in image editing software
- findContours(), which can be used to find what many of us older image processing engineers would call “blobs” — connected regions of pixels in a binarized image
- Canny edge detector, which is one of the many algorithms that will help teach you the limitations of image processing (sigh)
And so on. I’m not describing these algorithms or providing pictures yet, but given sufficient interest I’ll try to pull some code examples online.