Establishing the OPENCV framework

Source: Internet
Author: User
Tags svn zip uikit
Original posts: http://blog.sina.com.cn/s/blog_698415f20100y7ip.html

Introduced

This article is a OPENCV visual library that uses computers on iOS. OpenCV is released under the BSD license, so both learning and business are free. It includes an optimized implementation of all the standard algorithms used in the field of computer vision today, and has a huge user base spanning the world of Linux,windows,mac and Android. OpenCV has been difficult to build on iOS in the past. Recently, however, OpenCV's team has been a great addition to building on iOS and supporting video and capture.

In this article, our goal is to build the OPENCV framework on iOS, and we can simply drag and drop it into our own Xcode project, greatly reducing the learning curve. We also provide a way to rebuild the framework, and if you want, sample project packages include video capture and presentations using iOS OPENCV processing images.

We first explained the sample project. Second, explain how to use the OPENCV framework in your project. Finally, we explain how to reconstruct the OPENCV framework and explain how the build script works.

As in the past, our work is based on the work of others. We thank acknowledge's Openc team, Eugene Khvedchenya for OpenCV to run the work on iOS and Diney Bomfim for the iOS framework.

Sample Project

This sample project is hosted on GitHub. From here you can visit the GitHub project page: https://github.com/aptogo/OpenCVForiPhone or download the zip file directly: https://github.com/aptogo/ Opencvforiphone/zipball/master.

The project includes a pre-built OpenCV framework (OpenCV SVN revision 7017), a script for rebuilding the framework, and an example program that demonstrates video capture using OpenCV and simple image processing.

Build and run the application, use Xcode to open the Opencvclient project and click "Run". Note: The iphone simulator does not support video capture. You need to run the sample program on your iOS device and you'll see the video capture effect as you run.

The application begins to run some simple performance tests, the conversion between Cv::mat and UIImage. The timing junction is output to the console. The main page displays the results after the performance test is complete. Click the Capture button to capture the video frame. Frame processing uses the edge detection algorithm to perform some OPENCV image processing functions, and the results are displayed on the screen. Use the sliders to adjust the algorithm's threshold value low and high. The time to process the frame is also displayed. For the iphone4typical value is about 90ms, IPhone3 is about 200ms.

Using the OPENCV framework in your own projects

Add OpenCV Frame
The simplest way to add a OPENCV framework is to drag the OpenCV frame of the sample program into the "frameworks" of your project. If you want to copy the OpenCV frame to your project, select Copy items into destination Group's folder when the dialog box appears. If you want to share a frame in multiple projects or have a co-built frame, then you can uncheck it.

Another way you can manipulate "Build phases" in Xcode is to familiarize yourself with the tag window of the object. Click the dropdown "Link Binary with Libraries" and click the "+" button. Select "Add Other ..." To find the Opencv.framework folder to add from the dialog box that appears.

Once you have added the OPENCV framework, your project will automatically set up a link to the OPENCV library and can use the OpenCV header file. Import the OpenCV header file in your project using the framework-relative tag # include declaration (i.e #include <OpenCV/opencv/.../...>).

Add additional required Frames
Using the OPENCV framework, you must add some additional apple-provided frames in your project. In the Xcode Object Properties window, manipulate "Build phases". Click the dropdown "Link Binary with Libraries" and click the "+" button. Add the frameworks and libraries shown below. It is necessary to check the frame of the column in the table required. The frame checked in table optional is optional, if required using the Highgui module in OPENCV including video capture support.

Framework
Required
Optional (required for video capture)
Avfoundation.framework


Imageio.framework


Libz.dylib


Corevideo.framework


Coremedia.framework


When you add all the required frames, the Project Properties window for your project's "Build phases" tab should look like this:


The Build phases tab in the XCode Project Properties pane
Import OpenCV Header File
The OPENCV header file declared by OpenCV and OpenCV2 APIs is part of the OPENCV framework. Find the "frameworks" group in the "Project Navigator" in Xcode and find the "Opencv.framework" click on the drop-down menu to see the header file. When writing OPENCV code, you need to import the relevant header file into your project. The simplest way is to add the header file precompiled module to (<project name>-prefix.pch) and add the following as follows:

///////////////////////////////////////////////////////////////////////////
ADD This new section before the #import statements for UIKit and Foundation
#ifdef __cplusplus
#import <OpenCV/opencv2/opencv.hpp>
#endif

Existing #import Statements
#ifdef __objc__
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#endif
In the source file that uses the OPENCV framework, you must also change the extension of the source file, and change ". M" to ". MM". This indicates that the source file for the compiler includes mixed objective-c and C code. Note that a single source file that does not use OPENCV can use the ". M" extension.

Oddly, OpenCV's header files must be imported before UIKit.h and Foundation.h because OPENCV defines a minimum macro-min function that conflicts with the Apple framework. If you import after UIKit.h and Foundation.h, you will receive a compile error "LLVM" and "GCC 4.2" errors, such as: "Statement-expressions is allowed only inside functions ", the OpenCV header file is imported using the" __cplusplus "condition to Precompile" #import "to avoid this problem. Your project does not have a file called OpenCV APIs, you can still use the pure objective-c ". M" as the extension.

Using UIImage extensions
Sample projects include UIImage and Cv:mat, and the import of their header files. These 2 files provide the UIImage class (Uiimage+opencv.h and. mm). With extensions, you only need to add 2 original files to your project and take advantage of the following new UIImage methods and properties:

@interface UIImage (UIIMAGE_OPENCV)

Returns an autoreleased UIImage from Cv::mat
+ (UIImage *) Imagewithcvmat: (const cv::mat&) Cvmat;

Initialises a UIImage from Cv::mat
-(ID) Initwithcvmat: (const cv::mat&) Cvmat;

Returns Cv::mat object from UIImage
@property (nonatomic, readonly) Cv::mat Cvmat;

Returns grayscale Cv::mat object from UIImage
@property (nonatomic, readonly) Cv::mat Cvgrayscalemat;

@end

Rebuilding the OPENCV framework

Shell Footsteps (opencvbuild.sh) that contain sample project auto-construction and packaging. Before installing, you will need to make sure that you have subversion and CMake tools in your system. Subversion needs to download the latest OpenCV source. CMake is the compilation system used by the OPENCV team. Here is the installation file link:

Subversion for Mac
http://www.open.collab.net/downloads/community/
CMake for Mac
Http://www.cmake.org/cmake/resources/software.html
Get OpenCV Source
First extract the source files into the directory you want. If you want to see the layout of the sample project, this is a subdirectory of the OPENCV project root directory:

CD <project ROOT&GT;/OPENCV
Next, checkout the latest source code for the official repository. When writing this article, OpenCV did not release a stable version to support iOS video capture. Therefore, we use the latest version of the "trunk" source (note: The last date at the end of the command):

SVN Co https://code.ros.org/svn/opencv/trunk

After execution you will find OpenCV in the local directory where you extracted the files:


Opencvforios project Folder List

Create OpenCV
Opencvbuild shell scripts require 2 command-line arguments: Established at the head position of the OPENCV source tree structure. For example projects, we are the framework established at the root of the project. (Note the second parameter of the Opencvbuild final stage):

CD <project root>
./opencvbuild Opencv/opencv.
If the construction is completed successfully, you will get a OPENCV framework, and there will also be 3 library packages in the selected file fake:

Opencv.framework
Framework for use with IOS device or Simulator
Opencv_iphoneos
Libraries and headers for use with IOS device
Opencv_iphonesimulator
Libraries and headers for use with IPhone Simulator
Opencv_universal
Fat libraries and headers for use with IOS device or Simulator
These library packages are a combined framework of intermediate steps. You can delete them, and you might prefer to use the OPENCV framework instead of linking to individual libraries.

Example Program link address:

Git-
Https://github.com/aptogo/OpenCVForiPhone
Download zip–
Https://github.com/aptogo/OpenCVForiPhone/zipball/master

Resources:

Http://fossies.org/dox/OpenCV-2.3.1a/kinect__maps_8cpp.html


The following are OPENCV related resources:

1.Using OpenCV on IOS

Http://server.everfine.com.tw/blog/archives/2011/04/using-opencv-on.html

2.Building OpenCV for IPhone in one click

http://computer-vision-talks.com/2011/02/building-opencv-for-iphone-in-one-click/

3.opencv-ios

Https://github.com/macmade/OpenCV-iOS

4. OpenCV iOS (three demo tutorials)

Http://docs.opencv.org/doc/tutorials/ios/table_of_content_ios/table_of_content_ios.html


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.