Swift uses OpenCV2 for image stitching (merging multiple photos into a panoramic view)

Source: Internet
Author: User
1,OPENCV Introduction
OpenCV's full name is: Open Source Computer Vision Library. It is an open source Cross-platform Computer Vision Library, which realizes many common algorithms in image processing and computer vision.

Its application fields are: 1, human-machine interaction 2, object Recognition 3, image segmentation 4, face recognition 5, action recognition 6, movement tracking 7, Robot 8, motion Analysis 9, machine Vision 10, structure Analysis 11, car safety driving

2, using OpenCV for picture stitching
Here's a sample example that shows how to use OpenCV to stitch multiple photos into a panoramic view. Note: There should be overlap between photos and photos, so that the OpenCV can be analyzed and identified to achieve automatic stitching.
(1) Here are some of the original photos taken
(2) The following is a mosaic of photos
3, Operation steps

(1) First download the latest OPENCV frame: Opencv2.framework.zip. and add it to your project.
(2) Download the third party's Objective-c encapsulation class: Opencvswiftstitch

Because we need to use OPENCV, Swift cannot call C + + directly. So it is necessary to use objective-c as the middle layer and let Swift invoke OpenCV through Objective-c.

Fortunately, GitHub has been provided with the relevant packaging classes, we directly import the following files to the project.
(3) Remember to add CVWrapper.h to the bridge header file Bridge.h


#import "CVWrapper.h"

(4) The use of the following is very simple, the uiimage to Cvwrapper on it

Import Uikit

Class Viewcontroller:uiviewcontroller {

@IBOutlet weak var imageview:uiimageview!
@IBOutlet weak var spinner:uiactivityindicatorview!

Override Func Viewdidload () {
Super.viewdidload ()
}

Override func Viewdidappear (Animated:bool) {
Super.viewdidappear (animated)
Stitch ()
}

Func stitch () {
Because the picture splicing is time-consuming, displays the circular progress bar
Self.spinner.startAnimating ()
Stitching pictures in the background queue
Dispatch_async (Dispatch_get_global_queue (dispatch_queue_priority_default, 0)) {
Let Image1 = UIImage (named: "Img_4247.jpg")
Let Image2 = UIImage (named: "Img_4248.jpg")
Let Image3 = UIImage (named: "Img_4249.jpg")
Let Image4 = UIImage (named: "Img_4250.jpg")

Let imagearray:[uiimage!] = [Image1,image2,image3,image4]

Let Stitchedimage:uiimage = Cvwrapper.processwitharray (Imagearray) as UIImage

Self.imageView.image = Stitchedimage

Stop Loop progress bar effect
Self.spinner.stopAnimating ()
}
}

Override Func didreceivememorywarning () {
Super.didreceivememorywarning ()
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.