OpenCV iOS Development (i)--Install (GO)

Source: Internet
Author: User

OpenCV is an open source cross-platform computer vision and machine learning library that can be used for Image video processing, graphics recognition, machine learning and other applications. This article will cover the start of Hello World in OpenCV iOS development.

Installation

There are three ways to install OpenCV:

    • Download source code compilation
    • Installing with Cocoapods
    • Use the official framework

The first way is too cumbersome, and I am not familiar with, want to build their own can go to GitHub download compilation.

The second way is very simple, and then podfile to join pod ‘OpenCV-iOS‘, ‘~> 3.1‘ , and then run pod install on the line, but the problem cocoapods above OPENCV version only 3.x, no I want 2.x, and I tried to install a few times, because the pit Dad's network failed.

The third Way is to download the official website, I downloaded the latest 2.4.13, the following example is the use of this version of the development, but the use of the time there are a lot of pits, will be mentioned later.

Using OpenCV

First add the downloaded opencv2.framework to the project, and add the dependent libraries that you want to OpenCV to the project.

    • Libc++.tbd
    • Avfoundation.framework
    • Coreimage.framework
    • Coregraphics.framework
    • Quartzcore.framework
    • Accelerate.framework

If you want to use a webcam for video processing, you also need to add the following two dependent libraries:

    • Corevideo.framework
    • Coremedia.framework
    • Assetslibrary.framework

After adding the dependent library we formally began to write the first Hello World, because OpenCV is written in C + +, so the introduction of files in the project needs to be written in object-c++ compatibility, the specific way is to import the OpenCV header files to m mmfile, the OPENCV will be encapsulated with object-c++ and then properly imported in object-c. Unfamiliar with C + + syntax can be used to quickly learn the link.

    • X-Minute Quick y
    • Introducing C + + (i) to iOS developers
    • Introducing iOS developers to C + + (ii)

The following is a formal addition to the code, in this case we can give the camera a black and white reverse filter.
The first step is to import the OpenCV library into the file that we modified mm .

#import <opencv2/opencv.hpp>#import <opencv2/highgui/cap_ios.h>

Then add a uiimageview as the display, and then use the OpenCV Cvvideocamera object to get the picture from the camera displayed in Uiimageview, with the following code:

@interface Viewcontroller () <cvvideocameradelegate>{Uiimageview *cameraview; Cvvideocamera *videocamera;}    @end @implementation viewcontroller-(void) viewdidload {[Super viewdidload];    Cameraview = [[Uiimageview alloc] initWithFrame:self.view.frame];    [Self.view Addsubview:cameraview];    Videocamera = [[Cvvideocamera alloc] initwithparentview:cameraview];    Videocamera.defaultavcapturedeviceposition = Avcapturedevicepositionfront;    Videocamera.defaultavcapturesessionpreset = avcapturesessionpreset640x480;    Videocamera.defaultavcapturevideoorientation = avcapturevideoorientationportrait;    Videocamera.defaultfps = 30;    Videocamera.grayscalemode = NO; Videocamera.delegate = self;}    -(void) Viewdidappear: (BOOL) animated {[Super viewdidappear:animated]; [Videocamera start];}    -(void) Viewwilldisappear: (BOOL) animated {[Super viewwilldisappear:animated]; [Videocamera stop];} #pragma mark-cvvideocameradelegate-(void) Processimage: (cv::mat&) Image {   Here we are going to add the code for the Graphics processing} 

To invert a picture in black and white, you need two steps, first turn the picture into a grayscale image, and then swap the black and white for the detailed code as follows:

- (void)processImage:(cv::Mat&)image {    //在这儿我们将要添加图形处理的代码    cv::Mat image_copy;    //首先将图片由RGBA转成GRAY    cv::cvtColor(image, image_copy, cv::COLOR_BGR2GRAY);    //反转图片    cv::bitwise_not(image_copy, image_copy);    //将处理后的图片赋值给image,用来显示    cv::cvtColor(image_copy, image, cv::COLOR_GRAY2BGR);}

MatIt is a matrix object, in OpenCV, a picture information in C + + using Mat objects to store, and in C language using IplImage pointers to store, that is, the cvtColor content of the picture is copied in the specified format.

After adding the above code, the following results will appear in the video after running.


Picture reversal effects may be encountered problems

Previous versions, such as the version of 2.4.11 I used earlier, could run into the following error after importing ' opencv2.framework ' to the project:

Undefined Symbols for Architecture x86_64: "_jpeg_free_large", referenced from: _free_pool in Opencv2 (JMEMMGR.O) "_ Jpeg_free_small ", referenced from: _free_pool in Opencv2 (JMEMMGR.O) _self_destruct in Opencv2 (JMEMMGR.O)" _jpeg _get_large ", referenced from: _alloc_large in Opencv2 (JMEMMGR.O) _alloc_barray in Opencv2 (JMEMMGR.O)" _jpeg_get _small ", referenced from: _jinit_memory_mgr in Opencv2 (JMEMMGR.O) _alloc_small in Opencv2 (JMEMMGR.O)" _jpeg_mem _available ", referenced from: _realize_virt_arrays in Opencv2 (JMEMMGR.O)" _jpeg_mem_init ", referenced from: _ji      Nit_memory_mgr in Opencv2 (JMEMMGR.O) "_jpeg_mem_term", referenced from: _jinit_memory_mgr in Opencv2 (JMEMMGR.O) _self_destruct in Opencv2 (JMEMMGR.O) "_jpeg_open_backing_store", referenced from: _realize_virt_arrays in Opencv2 (j MEMMGR.O) Ld:symbol (s) not found for Architecture X86_64clang:error:linker command failed with exit code 1 (use-v to SE e invocation)

After online search to know that is missing a libjpeg.a dependent library, you can search for this a file on the Internet, download after use to lipo -info libjpeg.a see if support is included armv6 armv7 armv7s arm64 . Of course, you can download Libjpeg-turbo directly from this path and add it to the project directly after installation /opt/libjpeg-turbo/lib/libjpeg.a . However, in the latest OPENCV 2.4.13 version of this error is not available.

The following error occurs if you run the above example:

  undefined symbols for architecture arm64: "_objc_class_$_alassetslibrary", Referenced from:objc-class-ref in Opencv2 (CAP_IOS_VIDEO_CAMERA.O) "_cmsamplebuffergetpresentationtimestamp", Refere nced from:-[cvvideocamera captureOutput:didOutputSampleBuffer:fromConnection:] in Opencv2 (CAP_IOS_VIDEO_CAMERA.O) " _cmtimemake ", referenced from:-[cvvideocamera createvideodataoutput] in Opencv2 (CAP_IOS_VIDEO_CAMERA.O)" _CMSampleB Uffergetimagebuffer ", referenced from:-[capturedelegate captureOutput:didOutputSampleBuffer:fromConnection:] in Ope Ncv2 (CAP_AVFOUNDATION.O)-[cvvideocamera captureOutput:didOutputSampleBuffer:fromConnection:] in Opencv2 (cap_ios_ VIDEO_CAMERA.O) Ld:symbol (s) not found for Architecture Arm64clang:error:linker command failed with exit code 1 (use-v To see invocation)  

This is because we use the camera and video, need to CoreVideo.framework import CoreMedia.framework , AssetsLibrary.framework Three libraries will not be wrong.



Behind me like a dream
Links: http://www.jianshu.com/p/79f9c4200b9e
Source: Pinterest
Copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please specify the source. Go to: http://www.jianshu.com/p/79f9c4200b9e

OpenCV iOS Development (i)--Install (GO)

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.