HOWTO: Setup XCode 6.1 to work with OpenCV3 libraries

来源:互联网 发布:云裳恋衣淘宝店 编辑:程序博客网 时间:2024/06/07 17:16

HOWTO: Setup XCode 6.1 to work with OpenCV3 libraries

Overview

This post demonstrates how to setup your XCode IDE to work with openCV libraries on Yosemite. Initially XCode can be an intimidating environment, but past all the windows and complex sections, it boasts a number of beneficial features that aid and simplify programming – which for newcomers to openCV are a must. Features such as autocomplete and integrated compiler are extremely useful in simplifying compilation, speeding up programming time and clarifying how pieces of code fit together.

WARNING: This guide assumes you have compiled and installed openCV
It not, see this guide.

How To

Let’s begin.

  • Open XCode
  • Create New Project
  • Under OSX Application > Choose Command Line Tool
  • Give your application a name then Save

Assigning the Search Paths

To use the openCV libraries you will need to tell XCode where to find them.

  • Click the XCode project file in your inspector (which is the blue icon in the furthest left hand tab). You should now have three tabs in the centre window. Build Settings | Build Options | Build Rules
  • Click Build Settings
  • Scroll down until you find Search Paths
    Screen Shot 2014-11-19 at 09.30.10 (2)
  • Double Click the Header Search Paths option, then Click the plus icon
  • Type in the following /usr/local/include
    Screen Shot 2014-11-19 at 09.32.25 (2)
  • Next Double Click the Library Search Paths option, then Click the plus icon
  • Type in the following /usr/local/lib

Linking the Libraries

Now we need to tell XCode which libraries we want to build against. Still in Build Settings.

  • Scroll until you find Linking
  • Double Click Other Linker Flags and Click the plus icon
  • Copy and Paste the following, then press enter
    -lopencv_calib3d -lopencv_core -lopencv_features2d -lopencv_flann -lopencv_highgui -lopencv_imgcodecs -lopencv_imgproc -lopencv_ml -lopencv_objdetect -lopencv_photo -lopencv_shape -lopencv_stitching -lopencv_superres -lopencv_ts -lopencv_video -lopencv_videoio -lopencv_videostab

    Screen Shot 2014-11-19 at 09.37.29 (2)

Setting the Build Path

XCode automatically stores the executable file in a derived data folder, we want to change it so that the executable is stored in your project directory.

  • Open XCode Preferences
  • Select Locations Tab
    Screen Shot 2014-11-19 at 09.52.11 (2)
  • Click Advanced
  • Change the Location Button from Unique to Legacy
    Screen Shot 2014-11-19 at 09.52.20 (2)

Build your Application

Now you are ready to build your application. In your main.cpp file, add the following code.
This is a sample application from the openCV package.

#include <opencv2/imgcodecs.hpp>#include <opencv2/videoio/videoio.hpp>#include <opencv2/highgui/highgui.hpp>#include <iostream>#include <stdio.h>using namespace cv;using namespace std;//hide the local functions in an anon namespacenamespace {    void help(char** av) {        cout << "The program captures frames from a video file, image sequence (01.jpg, 02.jpg ... 10.jpg) or camera connected to your computer." << endl        << "Usage:\n" << av[0] << " <video file, image sequence or device number>" << endl        << "q,Q,esc -- quit" << endl        << "space   -- save frame" << endl << endl        << "\tTo capture from a camera pass the device number. To find the device number, try ls /dev/video*" << endl        << "\texample: " << av[0] << " 0" << endl        << "\tYou may also pass a video file instead of a device number" << endl        << "\texample: " << av[0] << " video.avi" << endl        << "\tYou can also pass the path to an image sequence and OpenCV will treat the sequence just like a video." << endl        << "\texample: " << av[0] << " right%%02d.jpg" << endl;    }        int process(VideoCapture& capture) {        int n = 0;        char filename[200];        string window_name = "video | q or esc to quit";        cout << "press space to save a picture. q or esc to quit" << endl;        namedWindow(window_name, WINDOW_KEEPRATIO); //resizable window;        Mat frame;                for (;;) {            capture >> frame;            if (frame.empty())                break;                        imshow(window_name, frame);            char key = (char)waitKey(30); //delay N millis, usually long enough to display and capture input                        switch (key) {                case 'q':                case 'Q':                case 27: //escape key                    return 0;                case ' ': //Save an image                    sprintf(filename,"filename%.3d.jpg",n++);                    imwrite(filename,frame);                    cout << "Saved " << filename << endl;                    break;                default:                    break;            }        }        return 0;    }}int main(int ac, char** av) {        if (ac != 2) {        help(av);        return 1;    }    std::string arg = av[1];    VideoCapture capture(arg); //try to open string, this will attempt to open it as a video file or image sequence    if (!capture.isOpened()) //if this fails, try to open as a video camera, through the use of an integer param        capture.open(atoi(arg.c_str()));    if (!capture.isOpened()) {        cerr << "Failed to open the video device, video file or image sequence!\n" << endl;        help(av);        return 1;    }    return process(capture);}

Then hold CMD + B (This builds and compiles the application)
Go to your project folder. There should be a new folder called Debug, inside is your executable file.
Open Terminal then navigate to the folder, then simply type ./<yourprojectname> 0
This should open window and display the camera image.
And there you go!

0 0
原创粉丝点击