05-07 03:51
Notice
Recent Posts
Recent Comments
관리 메뉴

Scientific Computing & Data Science

[OpenCV] Setting OpenCV Development Environment in Xcode 본문

Programming/OpenCV

[OpenCV] Setting OpenCV Development Environment in Xcode

cinema4dr12 2013. 10. 30. 23:27

Written by CINEMA4D

In this article I'm going to explain how to set up OpenCV development environment in Xcode.

I got most of the information from https://sites.google.com/site/learningopencv1/installing-opencv.

My test environment is as follows (latest versions at this writing):

- Mac OS X 10.9 (Mavericks)

- Xcode 5.0.1 (5A2053)

- OpenCV 2.4.6


1.  Install Xcode
Xcode 5.0.1 usually comes with Mac OS X 10.9. You can download the latest version of Xcode from Mac App Store.

2.  Download MacPorts
MacPorts is designed for compiling, installing and upgrading either command-line, X11 or Aqua based open-source software on the OS X operating system.
You can download MacPorts from http://www.macports.org/install.php. Notice that you have to choose installers depending on your Mac OS (Mavericks, Mountain Lion or Lion).

3.   Download OpenCV
You can download OpenCV 2.4.6 for Mac from http://sourceforge.net/projects/opencvlibrary/.
This file will be provided as a type of compressed file. By double clicking to uncompress this and place the folder anywhere you want, for example, your Xcode project folder.
To assist your understanding I designate the folder's place as "/Users/USER_NAME/Documents/XCode/opencv-2.4.6".

4.  Build OpenCV

- Execute Terminal application
- Input the followings:

  • sudo port -v selfupdate
  • sudo port install subversion
  • sudo port install cmake
  • cd /Users/USER_NAME/Documents/XCode/opencv-2.4.6 (any place of your OpenCV folder)
  • mkdir build (or you can make 'build' folder in your OpenCV folder)
  • cd build
  • cmake -G "Unix Makefiles" ..
  • make -j8
  • sudo make install
  • sudo port install opencv

The final step - sudo port install opencv would take considerable time to finish so you can take a rest a while or drink many cups of coffee :)

After all these steps you might check openCV headers and library files in this path:
- headers: /opt/local/include/opencv
- libraries: /opt/local/lib/

The library files have extension of *.dylib and theirs name start with libopencv.


5.  Test with a test project in Xcode

- Run you Xcode application

- Select 'Create a new Xcode project' from Welcome message

- When asking template of the new project, choose OS X > Command Line Tool



- Input your Product Name, Organization Name, Company Identifier as your will.
   In this example I input 'OpenCVTest' as the Product Name.

- Set Type as C++

- Save your project any place you want

- If you click your project on the left sidebar(OpenCVTest 1 target, OS X SDK 10.9) you can see Build Settings on the main window

- On Build Settings scroll down until you can see Search Paths


- In Header Search Paths input '/opt/local/include' for both Debug and Release


- In Library Search Paths input 'opt/local/lib' for both Debug and Release



- Add OpenCV frameworks
Right click on your project(OpenCVTest 1 target, OS X SDK 10.9) on the leftside bar and select New Group naming it 'OpenCV Frameworks'


- Right click on OpenCV Frameworks group you have just added and select Add Files to "OpenCVTest"... to add openCV libraries

- If file selector pops up set the path as '/opt/local/lib' to select 'libopencv_core.2.4.6.dylib' and 'libopencv_highgui.2.4.6.dylib' files, which are the required libraries for the test example

- You can see the two libraries added into your OpenCV Frameworks group on the left sidebar


- Every preparation has been done except for making your test code

- On the left sidebar choose main.cpp and you will see the code lines on the main window (If you double click it another window will pop up so just one click would be enough)

- Next to '#include <iostream>' type '#include <opencv2/opencv.hpp>'. During typing if auto-complete functionality does not show up your Xcode might fail to search either header or library path. In this case check your path in Search Paths again.

- Type test code:

int main(int argc, char *argv[]) {

    // Open the file. IplImage *img = cvCreateImage( cvSize(100,200), IPL_DEPTH_8U, 3);

    // Display the image.     cvNamedWindow("Image:", CV_WINDOW_AUTOSIZE);     cvShowImage("Image:", img);

    // Wait for the user to press a key in the GUI window.     cvWaitKey(0);

    // Free the resources.     cvDestroyWindow("Image:");     cvReleaseImage(&img);

    return 0; }



- Check the result

If everything is OK build and run to see this result:


- Although this test example shows very dull result it may be good enough as your starting program of computer vision using OpenCV




Comments