Computer vision with iOS Part 1: Building an OpenCV framework

by Robin Summerhill on September 7, 2011

  1. Introduction
  2. The example project
  3. Using the OpenCV framework in your own projects
  4. Rebuilding the OpenCV framework

Update: Nov 28 2011 – The OpenCV framework has been rebuilt using opencv svn revision 7017
Update: Check out Part 2 of our series on computer vision.


This article is the first in a series looking at computer vision for iOS using the OpenCV library. OpenCV is released under the BSD licence and so is free for both academic and commercial use. It includes optimised implementations of all the standard algorithms used today in the field of computer vision and has a huge user base across the Linux, Windows, Mac and Android worlds. In the past, OpenCV has been difficult to build for iOS. However, recent sterling work by the OpenCV team has added iOS build support and video capture.

In this article we aim to reduce the learning curve even further by providing the OpenCV library as an iOS framework that can be added to your own Xcode projects simply by dragging and dropping. We also provide a build script for re-building the framework should you need to, and an example project that wraps everything up with a neat demo of video capture and image processing using OpenCV on iOS.

We start by walking you through the example project then describe how to use the OpenCV framework in your own projects. Finally, we describe how to re-build the OpenCV framework and explain how the build script works.

As ever, we build on the work of others that have gone before us. We would like to acknowledge the OpenCV team, Eugene Khvedchenya for previous work on building OpenCV for iOS and Diney Bomfim for work on iOS frameworks.

The example project

The example app
The example project is hosted on GitHub. You can visit the GitHub project page at:
or download a zip archive of the project directly at:
The project includes a pre-built OpenCV framework (OpenCV svn revision 7017), a build script to rebuild the framework and an example app that demonstrates video capture and simple image processing using OpenCV.

To build and run the app, open the OpenCVClient Xcode project and hit ‘Run’. Note that video capture is not supported on the iPhone Simulator. Run the example app on an iOS device to see video capture in action.

The app starts by running some simple performance tests converting between UIImages and cv::Mat objects. Timing results are output to the console. After the performance tests have completed the main interface is shown. Tap the ‘Capture’ button to capture a video frame. The frame is processed using the Canny edge detection algorithm to exercise some of OpenCV’s image processing functions and the results are displayed on screen. Use the sliders to adjust the low and high algorithm threshold values. The time taken to process the frame is also displayed. Typical values are around 90ms on the iPhone 4 and around 200ms on the iPhone 3.

Using the OpenCV framework in your own projects

Adding the OpenCV Framework

The easiest way to add the OpenCV framework to your own project is to drag the OpenCV.framework folder from the example project folder in Finder and drop it onto the ‘Frameworks’ group of your target project in the Xcode Project navigator. Check the option ‘Copy items into destination group’s folder’ in the dialog that appears if you want to copy the OpenCV framework into your target project. If you are sharing a copy of the framework between multiple projects or have a common build location for the framework then you will want to leave the option unchecked.

Alternatively, you can navigate to the ‘Build Phases’ tab of the Project properties pane in Xcode. Drop down the ‘Link Binary With Libraries’ build phase item and click the ‘+’ button. Select “Add Other…’ from the dialog that appears and navigate to the OpenCV.framework folder.

Once you have added the OpenCV framework, your project is set up to link against the OpenCV libraries automatically and the OpenCV header files are also made available. Refer to OpenCV header files in your project #include statements using the framework-relative notation (i.e #include <OpenCV/opencv/.../...>).

Add additional required frameworks

To use the OpenCV framework you must add a few extra Apple-supplied frameworks to your project. To do this, navigate to the ‘Build Phases’ tab of the Project properties pane in xCode. Drop down the ‘Link Binary With Libraries’ build phase item and click the ‘+’ button. Add the frameworks and libraries shown below. The frameworks in the first column of the table are required. The frameworks in the second column are optional and are only needed if you are using the video capture support included in OpenCV’s highgui module.

Framework Required Optional (required for video capture)

When you have added all the required frameworks, your project’s Build Phases tab in the Project Properties pane should look like this:

The Build Phases tab in the XCode Project Properties pane

Include the OpenCV headers

The headers that declare the OpenCV and OpenCV2 APIs are provided as part of the OpenCV framework. The headers can be browsed by dropping down the OpenCV.framework item in the ‘Frameoworks’ group of the Project navigator in XCode. To write OpenCV client code you need to include these headers in your project. The easiest way to do this is to modify your pre-complied header file (<project name>-Prefix.pch) to add the three new lines shown below:

// Add this new section BEFORE the #import statements for UIKit and Foundation
#ifdef __cplusplus
    #import <OpenCV/opencv2/opencv.hpp>
// Existing #import statements
#ifdef __OBJC__
    #import <UIKit/UIKit.h>
    #import <Foundation/Foundation.h>

You must also change the extension of any source file in your project in which you wish to use OpenCV from ‘.m‘ to ‘.mm‘. This indicates to the compiler that the source file includes mixed Objective-C and C++ code. Note that individual source files that don’t use OpenCV can remain as ‘.m‘ files.

For the curious, the OpenCV headers must be included before UIKit.h and Foundation.h because OpenCV defines a MIN macro that conflicts with the MIN function defined by the Apple frameworks. If you include the OpenCV headers after UIKit.h and Foundation.h you will receive compilation errors such as ‘LLVM GCC 4.2 Error: Statement-expressions are allowed only inside functions’. Including the OpenCV headers first and surrounding the #import with the __cplusplus conditional test avoids this problem and means that you can still use plain Objective-C for ‘.m‘ files in your project that don’t call the OpenCV APIs.

Using the UIImage extensions

The example project includes extensions to UIImage for converting to and from cv:Mat objects. These are provided as a UIImage category in two source files (UIImage+OpenCV.h and .mm). To use the extensions simply add the two source files to your project and make use of the following new UIImage methods and properties:

@interface UIImage (UIImage_OpenCV)
// Returns an autoreleased UIImage from cv::Mat
+(UIImage *)imageWithCVMat:(const cv::Mat&amp;)cvMat;
// Initialises a UIImage from cv::Mat
-(id)initWithCVMat:(const cv::Mat&amp;)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;

Rebuilding the OpenCV framework

Included with the example project is a shell script ( that automates building and packaging of the OpenCV libraries. Before starting you will need to make sure that you have Subversion and CMake on your build system. Subversion is required to download the latest OpenCV sources and CMake is the build system used by the OpenCV team. Binary installers for both are provided at the locations listed below:

Subversion for Mac
CMake for Mac

Obtaining the OpenCV source

First change to the directory where you want the source files to be extracted. If you are following the layout of the example project, this is the opencv subdirectory of the project root directory:

cd <project root>/opencv

Next, checkout the latest sources from the offical repository. At the time of writing, support for iOS builds and video capture have not made it into the stable OpenCV release so we are using the latest sources in ‘trunk’. (Note the final period at the end of the command):

svn co .

You should now have the OpenCV source tree under your chosen location:

Directory listing of OpenCVForiOS

Directory listing of OpenCVForiOS project folder

Building OpenCV

The opencvbuild shell script takes two command-line arguments: the head of the OpenCV source tree and the location where you want the build to be performed. For the example project we built the framework in the project root directory (again, note the final period as the second argument to opencvbuild):

cd <project root>
./opencvbuild opencv/opencv .

If the build completed successfully you should now have the OpenCV framework along with three library packages in your chosen build location:

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

The library packages are built as an intermediate step before the framework is assembled. You can choose to remove them or you may prefer to link against the individual libraries instead of using the OpenCV framework.

How it works

Most of the heavy lifting in opencvbuild is delegated to cmake and xcodebuild. First, an Xcode project file is created using cmake and the CMake configuration files provided by the OpenCV team. From revision 6675, support for iOS builds was introduced, which makes our life much easier. A command-line build is then initiated using xcodebuild driven by the Xcode project file created in the first step.

The build script actually performs the build twice, once targeting iOS devices (armv6 and armv7) and once targeting the Simulator (i386). The resulting binaries are then combined with the lipo tool to produce fat binaries that support operation on both device and simulator. These fat binaries can be found in the OpenCV_Universal intermediate build directory.

Finally, the libraries and OpenCV header files are assembled into the OpenCV framework. Two problems arise here, which the script overcomes with a couple of sneaky tricks. First, a framework for iOS can only include a single static library but the OpenCV build has produced 11 that we need to include. To get around this restriction the script simply combines the 11 libraries into one using libtool and then moves the resulting super-library into place within the framework. Secondly, the OpenCV headers use relative paths in #include statements, which makes them difficult to use without configuring header search paths within your project settings. To solve this, the script replaces any occurrence of a relative include path (i.e. "opencv/.../...") with a framework-based include path (i.e. <OpenCV/opencv/.../...>). The headers are added to the framework so that the whole package, library and headers, can be added to your project in a single step.

Robin Summerhill is a tech blogger, developer and architect. He is co-founder of Emu Analytics where he is currently working as Head of Technology.

Links to example project source code

Git –
Download zip –

Share and Enjoy:
  • Print
  • email
  • Digg
  • Facebook
  • Google Bookmarks
  • Reddit
  • Twitter


Joe Pagliaro September 14, 2011 at 3:22 pm

Thank you!

This article made it a breeze to get up and running with OpenCV on iOS. It is very well written. I am new to OpenCV, and the sample project you provide, OpenCVForiPhone, provided a great start. I anticipate future articles in this series.

Robin Summerhill September 14, 2011 at 3:30 pm

Glad you liked it! We always appreciate hearing from our readers – the next installment is in the works…

Matt Montag September 25, 2011 at 8:01 pm

I am having trouble building the latest OpenCV source with this script. See my stackoverflow question here:

Dev2rights September 30, 2011 at 6:03 am

Hi Robin,

I have used this tutorial and modified it for a open source tutorial app we are working on. The tutorial is there to benchmark various combinations of feature trackers (MSER, FAST, SURF , STAR) and various feature detector and comparison algorithms. We will be releasing this as a tutorial with source code when completed, do you mind ?

Jim @ Dev2rights

Fraser Hardy March 6, 2012 at 2:01 pm

Hi Jim,

I would be interested to see this tutorial. Is it available online yet?


Irad Gilboa October 9, 2011 at 5:19 pm

Hi Robin,
Thank you for this great tutorial and the excellent sample code (the pre-built OpenCV framework is just what I was looking for).
Using your tutorial I was able to include openCV in my own project in no time – you just made my day!

Keep up the good work,

Phy October 18, 2011 at 6:02 am

Hi Robin,
and thank you for sharing your script! I’ve added a comment here:

Can you give me few hints of what can be wrong?
XCode 4.2
CMake 2.6-4
opencv rev 6769

thank you in advance!!!

Daniel Kiedrowski October 25, 2011 at 5:28 pm

thanks for your work.
Two Questions:
1. What Version of OpenCV is in the framework?
2. How can I use cv::findContours. Everytime I use it with your framework I get assertions.


Daniel Kiedrowski October 26, 2011 at 1:54 am

Hello again.
Never mind question 2. After some hours of recompiling OpenCV and looking at my code, I finally found my stupid mistake. I think I share it here, in case someone else is doing it.

Instead of writing std::vector<std::vector > to define a container for the resulting contour, I wrote std::vector<std::vector > what leads to my compiler mixing up cv::Point with Point (which actually exist, perhaps in the iOS SDK) and giving me this annoying assertions.


patrick July 6, 2012 at 11:20 pm

this saved me – made the same mistake

Sarpdoruk TAHMAZ November 6, 2011 at 8:14 am

Thanks for the great and well written article. Thanks to you I was able to include OpenCV to my iOS project.

w00 November 8, 2011 at 5:53 am

Hi, i’m trying to do templateMatching, but i think i’m doing it wrong because the ouput that i get doesn’t include any data if the object is found where. Atleast, i don’t see it…

If you have the time could you please take a look at:

WANG Chi November 8, 2011 at 8:48 pm

Great, will try out this tonight :)

yiqunc November 21, 2011 at 9:31 pm

hi ROBIN, your tutorial is so helpful! big thanks!
I have some problem when trying to rebuild the opencv source with your “” script.
I noticed that build for the Simulator was successful but failed for the Device build, I am using:
CMake 2.8.6
opencv Rev: 6988

The error info is like this:
In file included from /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.0.sdk/usr/include/mach/task.h:37:
In file included from /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.0.sdk/usr/include/mach/mach_interface.h:50:
In file included from /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.0.sdk/usr/include/mach/mach.h:67:
In file included from /Users/yiqunc/opencv/opencv/modules/core/src/system.cpp:76:
/Applications/xcode4.2.0/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS5.0.sdk/usr/include/mach/arm/task.h:438:2: error: unknown type name ‘mach_zone_name_array_t’; did you mean ‘mach_port_name_array_t’? [3]
mach_zone_name_array_t *names,
/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.0.sdk/usr/include/mach/port.h:107:27: note: ‘mach_port_name_array_t’ declared here [3]
typedef mach_port_name_t *mach_port_name_array_t;
In file included from /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.0.sdk/usr/include/mach/task.h:37:
In file included from /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.0.sdk/usr/include/mach/mach_interface.h:50:
In file included from /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.0.sdk/usr/include/mach/mach.h:67:
In file included from /Users/yiqunc/opencv/opencv/modules/core/src/system.cpp:76:
/Applications/xcode4.2.0/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS5.0.sdk/usr/include/mach/arm/task.h:440:2: error: unknown type name ‘task_zone_info_array_t’; did you mean ‘zone_info_array_t’? [3]
task_zone_info_array_t *info,
/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.0.sdk/usr/include/mach_debug/zone_info.h:91:22: note: ‘zone_info_array_t’ declared here [3]
typedef zone_info_t *zone_info_array_t;
2 warnings and 2 errors generated.


The following build commands failed:
CompileC /Users/yiqunc/opencv/tmp/modules/core/ modules/core/src/system.cpp normal armv7 c++
(1 failure)

I also have checked this file “/Applications/xcode4.2.0/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS5.0.sdk/usr/include/mach/arm/task.h” and it does inculde another header file “” which includes which contains the declaration of “mach_zone_name_array_t”.

could you please give me some hints?

Thank you very much!


Robin Summerhill November 22, 2011 at 8:26 am

It sounds like there have been some changes since 6675. I will take a look and make any fixes needed. Thanks for pointing this out.

yiqunc November 22, 2011 at 7:14 pm

Thanks Robin, you have done an awesome job!

Robin Summerhill November 28, 2011 at 10:37 am

I’ve rebuilt the framework using the latest revision from the OpenCV repository (7017) and updated the project repository on GitHub. I didn’t see any build problems. Let me know if you are still having issues rebuilding.

sarah December 9, 2011 at 2:25 am

Hi thanks for your written
I have problem with running the script in command line. because it says that the line 80 (cmake -GXcode) in not found. could you please help me what i can do to make framework now?

thanks a lot

Robin Summerhill December 15, 2011 at 1:21 pm

It sounds like a problem with you CMake installation. The cmake executable must be on your path for the script to work. Type ‘which cmake’ in Terminal to find where cmake has been installed and then type ‘cmake –version’ to see if it is on your path and to see which version is installed.

Don’t forget that you can always use the pre-built OpenCV framework provided with the example. You only need to rebuild using the script if you have a specific need to.

Gustavo December 15, 2011 at 9:38 am

It is giving me problems with the library highgui on the revision 7054.


The following build commands failed:
CompileC /Users/opencv/trunk/tmp/modules/highgui/ /Users/opencv/trunk/opencv/modules/highgui/src/ normal i386 objective-c++
(1 failure)


Robin Summerhill December 15, 2011 at 1:51 pm

Hi Gustavo,

I’ve just tried building 7054 and am getting a different set of errors to you. The bleeding-edge trunk is not always stable and some check-ins may break the build on certain platforms. Unless you have a specific need to use the very latest OpenCV source from trunk it is best to use the pre-built framework included with the example project for this article. I periodically rebuild the framework and test it on a range of iOS devices so you can be sure that the pre-built framework will work and is fairly up-to-date.


Gustavo December 16, 2011 at 5:16 am

Hi Robin,
Thanks for the answer, I don´t need the last version but I want to build the whole project because I want to try with some compiler options, to see if the framework improve the performance, because my application is slow and I need to increase the FPS.
The parameters wich I want to compile the libraries are thease:

On the script is only used “-O3″.
Also now, I have the same problem with the highgui in the revision 7017, besides it is not giving problems when I try to compile the libraries directly in XCode, I don´t know why.

Gustavo December 20, 2011 at 5:19 am

Solved, I just need to erase some code from the source code.
But It’s not worth it to use the compile parameters the application is slower than the original compilation.

Good Luck

Ben December 20, 2011 at 10:43 am

Hi Robin,
i got a conflict with the iOS Mapkit framework:
/Frameworks/MapKit.framework/Headers/MKGeometry.h:135: error: ‘isinf’ was not declared in this scope
If i delete the reference to the Mapkit framework, it’s working… any ideas? what can I do?
Thanks, Ben

Robin Summerhill December 20, 2011 at 6:17 pm

This appears to be a known problem when using MapKit in an Objective-C++ project. Note that it is nothing to do with OpenCV.

isinf is declared as a macro in math.h but in an Objective-C++ project, which #includes <cmath> , isinf is #undef’d and std::isinf is declared instead as a wrapper.

A workaround is to add this line before you #import MKGeometry.h or any of the other MapKit headers:

using namespace std;

wingnet December 22, 2011 at 4:21 am

this is a great demo. thank you.
I just ported an opencv demo from windows to ios smoothly.

however, there is a minor issue that cv::polylines can’t be compiled correctly. It seems that the 5th parameter(int isClosed) is the reason. I had to use cv::line to work around. wish u could take a look at this problem.

waiting for your 3rd post about opencv on ios.

gis December 29, 2011 at 3:36 am

Hi. I followed your instructions. I am using opencv for descriptor extraction and homography. Everything works fine on simulator, but on device it has this error:
Ld /Users/attila/Library/Developer/Xcode/DerivedData/PushPop-elmkmflbmdvkqvaeocterigucivq/Build/Products/Debug-iphoneos/ normal armv7
cd /Users/attila/GAOYAN/PushPop
setenv PATH “/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin:/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin”
/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/g++-4.2 -arch armv7 -isysroot /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.3.sdk -L/Users/attila/Library/Developer/Xcode/DerivedData/PushPop-elmkmflbmdvkqvaeocterigucivq/Build/Products/Debug-iphoneos -F/Users/attila/Library/Developer/Xcode/DerivedData/PushPop-elmkmflbmdvkqvaeocterigucivq/Build/Products/Debug-iphoneos -F/Users/attila/GAOYAN/PushPop/../../Downloads/aptogo-OpenCVForiPhone-717a18c -filelist /Users/attila/Library/Developer/Xcode/DerivedData/PushPop-elmkmflbmdvkqvaeocterigucivq/Build/Intermediates/ -dead_strip -miphoneos-version-min=4.3 -lz -framework ImageIO -framework AVFoundation -framework OpenCV -framework QuartzCore -framework Accelerate -framework MobileCoreServices -framework UIKit -framework Foundation -framework CoreGraphics -o /Users/attila/Library/Developer/Xcode/DerivedData/PushPop-elmkmflbmdvkqvaeocterigucivq/Build/Products/Debug-iphoneos/

Undefined symbols for architecture armv7:
“___divmodsi4″, referenced from:
void cv::pyrDown_<cv::FixPtCast, cv::NoVec >(cv::Mat const&, cv::Mat&)in OpenCV(pyramids.o)
void cv::pyrDown_<cv::FixPtCast, cv::NoVec >(cv::Mat const&, cv::Mat&)in OpenCV(pyramids.o)
void cv::pyrDown_<cv::FixPtCast, cv::NoVec >(cv::Mat const&, cv::Mat&)in OpenCV(pyramids.o)
void cv::pyrDown_<cv::FltCast, cv::NoVec >(cv::Mat const&, cv::Mat&)in OpenCV(pyramids.o)
void cv::pyrDown_<cv::FltCast, cv::NoVec >(cv::Mat const&, cv::Mat&)in OpenCV(pyramids.o)
ld: symbol(s) not found for architecture armv7
collect2: ld returned 1 exit status

Do you have any advice? Thanks a lot!!

gis December 29, 2011 at 4:11 am

I traced that it is caused by this line:

cvPyrDown(iplImage, small_image, CV_GAUSSIAN_5x5);

I replaced it with cvResize to solve the link error problem. So it must be some issue with pyramids.o on device.


Alex January 8, 2012 at 11:11 pm

Great article and great work!

I have a question while trying to implement cv::HoughLinesP ( inside processFrame{} with the following code:

cv::Mat grayFrame, output, lines;
cv::cvtColor(_lastFrame, grayFrame, cv::COLOR_RGB2GRAY);
cv::HoughLinesP(output, lines,1, CV_PI/180,80, 30, 10);
for(int x=0; x<lines.cols; x++) {
uchar *stX = + x;
uchar *stY = + (x+4);

uchar *endX = + (x+8);
uchar *endY = + (x+12);

cv::Point start(*stX,*stY);
cv::Point end(*endX,*endY);
NSLog(@"P1: %i, P2: %i, P3: %i, P4: %i",start.x,start.y,end.x,end.y);
cv::line( _lastFrame, start,end, cv::Scalar(255,0,0), 3, 8 );

Although the points are various "meaningful" numbers the lines I get are all messed up.
I wonder if there is a problem with my implementation approach since on opecv documentation ( vector lines is used instead. Unfortunately I couldn’t see how to implement vector. Does anyone have ideas on implementing HoughLinesP properly?

Ivan January 14, 2012 at 3:53 pm

I have the problem with opencv
i did everything mentioned above, however, every time i try to include some header file (eg. #include ) i receive “no such file or directory” error. Can anyone help me with thia please?

gary January 31, 2012 at 11:28 pm

@gis: i had the same problem – make sure you have added all the required ios frameworks (libz, imageIO, etc) after i added those, the build errors went away! thanks so much for putting this together, robin!

Hari February 3, 2012 at 6:31 am

I followed all your steps and it MOSTLY works fine, but there is a problem! Now I am able to use mat objects in my ‘.mm’ files but when I declare it in a header file like this…

@interface TestViewController : UIViewController
cv::Mat myMat;

I get a compiler error: ‘Type name requires a specifier or qualifier’

However the same error does not come in your openCVClient where you go
cv::Mat _lastFrame;

Why does this happen?

Robin Summerhill February 3, 2012 at 7:11 am

If you use OpenCV types in a header file then you need to make sure that every source file that includes that header (directly or indirectly) is a ‘mm’ file. If you look back at the compiler messages output during the build you will see which compilation unit triggered the error you are seeing.

hari February 5, 2012 at 11:28 pm

Wow! I changed my app delegate to .mm and it worked. It’s such a sucky issue, would’ve never known if u hadn’t told. Thanks a lot!

allan February 23, 2012 at 5:16 pm

I have to write a comment to thank you for that post!

That will help me a LOT to start using OpenCV on IOS. Thank you very much! The sample project is very well organized and the post is very well written. Thanks a lot from Brazil!

Lukas February 24, 2012 at 12:08 pm

Hi, somebody how saved image with cv:imwrite() in OpenCVClientViewController? I have image totaly rabish.

Is it my bad or is it some serious problem?

Brian February 28, 2012 at 8:41 pm

Badass! This rocks, I have one question, how does OpenCV from your framework use the AVFoundation & Core Video and the other apple frameworks when you open the video capture? Did you create your own subclass of VideoCapture or something and where can I see this code, just curious. Thanks!

Robin Summerhill February 29, 2012 at 4:50 am

Later builds of OpenCV include support for video capture using AVFoundation. You can look at the OpenCV source to see how it is done. Take a look at the next article in the series to see a more flexible way of doing it by using AVFoundation directly.

Apurva March 3, 2012 at 11:33 pm

Fantastic job Robin! Thanks for sharing.

sam March 8, 2012 at 9:45 pm

thanks very much. What’s the best build option when Compile it ?

minghong.long March 11, 2012 at 3:02 am

I try to use some function about opencv
such like this cvLine(tempMat, cvPoint(10, 130), cvPoint(300, 130), cvScalar(255, 255, 255, 0));
but I got an error
“No matching function for call to cvLine”
why is that happen?
I have cpoy the frameWork into my project.
Did I miss something ?

Fraser Hardy March 14, 2012 at 10:43 am

The the first argument requires a pointer to CvArr.

If you CMD click on the offending function this is shown here:
cvLine( CvArr* img, CvPoint pt1, CvPoint pt2,
CvScalar color, int thickness CV_DEFAULT(1),
int line_type CV_DEFAULT(8), int shift CV_DEFAULT(0) );

So your code should read:
cvLine(&tempMat, cvPoint(10, 130), cvPoint(300, 130), cvScalar(255, 255, 255, 0));

Hope that helps,


longminghong March 30, 2012 at 12:42 pm

Thank you !!
It’s work well !
I haven’t use C For a Long time !!
Thank you again

minghong.long March 11, 2012 at 3:04 am

And I write it in your demo
God !
I have no idea How to fix it…..

Fraser Hardy March 14, 2012 at 10:39 am


I have a problem compiling when using certain functions in the openCV library.

Please see my question on stack overflow:

Any help would be greatly appreciated.


Fenton Stone March 15, 2012 at 3:42 am

Hi Robin,

I invoke the method cvCreateFGDStatModel in my project, it works fine on simulator, but crash on the devices, iPhone3GS and iPad2. However, if invoke cvCreateGaussianBGModel instead, it works fine on both simulator and devices , but it comes out not what i want.

Any help is appreciated.

Fraser Hardy March 15, 2012 at 6:20 am

I found a solution to my problem…. Someone has fixed the issue in this fork of the aptogo code:

Robin Summerhill March 15, 2012 at 6:39 am

Hi Fraser,

This fix has now been merged into the aptogo GitHub repository.

mark March 17, 2012 at 1:02 am

tried the sample code, it compiles ok & runs on the simulator. when i try to run it on my device, i get this error:

error: failed to launch ‘/Users/mark/Library/Developer/Xcode/DerivedData/OpenCVClient-gjpbrvzbxqrhugaqfmixoikidouk/Build/Products/Debug-iphoneos/’ — No such file or directory (/Users/mark/Library/Developer/Xcode/DerivedData/OpenCVClient-gjpbrvzbxqrhugaqfmixoikidouk/Build/Products/Debug-iphoneos/

why is it looking for OpenCVClient? why doesn’t it run the .app file?

i’m using Xcode 4.3.1 and SDK 5.1

caleb September 13, 2012 at 5:18 pm

For me, the trouble was with the file OpenCVClient-Info.plist. If you look in there, you’ll find that the Icon File setting is blank. Deleting that line allows you to run the app from Xcode on the device. (If you don’t delete that line, you should be able to launch the app directly on the device, but not from Xcode.)

Chris Jukes March 28, 2012 at 12:08 pm

Hi Robin, I’ve been using the OpenCV frameworks but need some guidance, have you done anything with Eigenfaces for recognition on iOS ?

I’m trying to do face recognition but struggling with the PCA bit and especially projecting my sample vectors and then matching to the training set to match the person.

any help appreciated.

longminghong March 30, 2012 at 12:38 pm

Thank you !!
It’s work well !
I haven’t use C For a Long time !!
Thank you again

Andrew April 2, 2012 at 3:49 pm

Wouldn’t dynamic framework get the app rejected from iOS AppStore?

Robin Summerhill April 2, 2012 at 5:47 pm

Hi Andrew,

It’s not a dynamic framework. It’s a static library wrapped up as a framework to make it easy to add to your Xcode project. iOS has no provision for custom dynamic frameworks so you couldn’t use one even if you wanted to.

Jason April 9, 2012 at 7:20 am

Thanks a lot for the tutorial! The example project works great.

However, I have created my own project and copied the framework for opencv. When I type cv:: I don’t get the list of functions and such for cv as I can in your project. This only happens when attempting in a .h file but works fine in a .mm file. In the example project this works in both files. I believe I am missing something very obvious but really can’t see what that is. Any help is appreciated. Thanks!

Jason April 9, 2012 at 7:39 am

I was able to solve this by changing the .h to .hpp

Joshua Noble April 25, 2012 at 12:13 am

When I try to run this script, I get an error:

Build settings from command line:
SDKROOT = iphonesimulator5.1

xcodebuild: error: Unable to read project ‘OpenCV.xcode’.
Reason: Project file ‘OpenCV’ was written by an unsupported Xcode version (39)

Curious if anyone else has seen it?

Tiago Reis May 6, 2012 at 1:23 pm


I having the same problem. Did you managed to solve it?


Tiago Reis May 6, 2012 at 1:54 pm


a bit after asking if you had solved the problem, I managed to solve it myself.

For me the problem was the version of cmake that I had installed, I had to update it to the latest version (2.8.8)

BMeyer May 11, 2012 at 5:34 am

HI, I have the same problem, but cmake has version 2.8.8. Anybody else who gets this error and/or has solved it?

Silver_205 June 3, 2012 at 5:37 pm

I’m dying to know how you overcame the following exception in image transformations

Cast of objective-c pointer type ‘NSData *’ to C pointer type ‘CFDataRef’ (aka ‘const_CFData *’) requires a bridged cast.

during this line:
CGDataProviderRef provider = CGDataProviderCreateWithCFData((CFDataRef)data);

without using the “__bridge” identifier.
in your project it works fine, but when i have created numerous projects and I just can’t override it.. I’m using LLVM GCC 4.2.

Brent T Nashville July 6, 2012 at 12:32 pm

Excellent tutorial!
I am fairly experienced on opencv & surf, however, most of that effort has been in the EMGU VB wrapper. So my Objective C, C or CPP is all new to me. Everything has gone well until I tried to add my firs “find contours” call and now find myself with the imfamous “no matching function”. I’ve tried a host of things including both CVfindcontours and CV::FindContours call attempts but end up at the same place. I’m sure there is something obvious to most but this one seems to have me stumped. Any pointer in a direction would be most appreciated.
Regards, and thanks again for a terrific tutorial.

Seungoh Ryu February 10, 2013 at 10:33 pm

I am trying to make cvFindContours work on iOS,
and managed to work in the simulator, but the same code running on the device (iPad) does not work…
(i,e, fails to find any contours)..
Did you make any progress since your comment here?

Matt Eaton July 29, 2012 at 4:16 pm

Thanks for the great article and the framework. I pretty new to IOS development and am looking at using opencv. When I look at the OpenCV.framework directory it’s around 45 MB. Is this what gets installed onto the device as part of the app? That seems very large.


Arvind December 28, 2012 at 4:32 am

Hi Matt, did you find the answer to your question? I also want to know. Thanks

Fuli August 8, 2012 at 4:41 am

Hi, I used the framework in the sample project, it works very well in ipod touch 5.01, but it is very slow in iphone 4 5.01, it took about 6 times time. any idea?

Fuli August 8, 2012 at 5:40 am

I think I have found the reason, the picture taken by iphone is much larger than taken on ipod touch.

bill September 21, 2012 at 2:11 pm

Thanks for the great work on this framework doctor. Any chance of getting a new one built including an armv7s slice for iphone 5?

hana October 21, 2012 at 11:19 am

i cant checkout from the terminal it says
svn: OPTIONS of ‘’: authorization failed: Could not authenticate to server: rejected Basic challenge (

is there alternate link ?

Jian December 13, 2012 at 10:23 pm

Hi Robin.

I have some problem.. you work looks awesome.
I imported your openCV framework to my project. But I want use stitching function in the openCV framework.
If I try to import stitching.hpp, then I have bugs. I don’t know well about openCV. I’m new.
Can you lead me to achieve my purpose.
I really hope your help.


Arvind December 31, 2012 at 2:43 am

Hi Robin, very useful and helped us a lot. Could you say whether IMRead and IMWrite work with iOS, and if yes, how? I find conflicting info on the Internet. Someone in the thread above has asked this but didn’t get an answer. Thanks again.

{ 6 trackbacks }

Previous post:

Next post: