You are currently viewing OpenCV in Swift MAC – OSX Project

OpenCV in Swift MAC – OSX Project

  • Post published:June 2, 2015

OpenCV is basically an OpenSource library having the best frameworks for video processing. In an OSX project, we had to implement OpenCV libraries in Swift language for MAC application. This article covers, how OpenCV libraries are used in a Linux/MAC application. Following are the steps to be followed:

1- Download latest OpenCV source code for Linux/MAC OS from this link.

OpenCV-1

2- Once OpenCV source code download completes, extract the OpenCV directory and save it at some location. This will be the raw code of OpenCV and you have to build it.

OpenCV-2

3- Now you need to check if cmake is installed on your MAC machine or not by typing “cmake” command in terminal. If it is not installed, type “sudo port install cmake” in terminal for installing cmake tool in your MAC machine. If cmake tool is already installed on your MAC then jump to next step.

OpenCV-3

4- In terminal, navigate into the OpenCV directory and type the following commands:
mkdir build
cd build
cmake –G “Unix Makefiles”..

OpenCV-4

5- Now you need to wait till terminal does its processing. Once the process finish, type the following command in terminal:
make –j8
sudo make install

OpenCV-5

It will prompt you for MAC login and password. Once you enter the password and give it a go, terminal will install some dependencies from the Internet and will install some dynamic libraries in /usr/local/include and /usr/local/lib directory on your MAC machine. Let the process finish successfully.

6- Now open Xcode and make a new project to add OpenCV libraries in your OSX application project. Press (Alt + Cmd + a) or select (File-> Add Files to project) option from menu. A window will open in Xcode. Type /usr/local/lib and press “Go” Button. The /usr/local/lib directory will open.

OpenCV-6

7- Select the required OpenCV libraries and press “Add” Button. You need to make sure that you are including the proper library, not the shortcut file. The OpenCV libraries naming convention is as libopencv_<specific Library>.<version>.dylib. Now select the following OpenCV libraries and press “add” button:
libopencv_highgui.3.0.0
libopencv_videoio.3.0.0
libopencv_core.3.0.0

OpenCV-7

8- The selected OpenCV libraries will start showing in Xcode. It is recommended to group the items under some suitable name.

OpenCV-8

9- Now you need to select the project from Xcode navigator from the left menu and go to “Build Phases” tab. Select “Link Binary with Libraries” and then add the OpenCV libraries that you included in the project (as in previous step).

OpenCV-9

10- Again select the project from Xcode navigator from the left menu and go to “Build Settings” tab. Type “Header Search Path” in the Search. Now add /usr/local/include path in “Header Search Path” and set it to recursive.

OpenCV-10

11- Once again you need to select the project from Xcode navigator from the left menu and go to “Build Settings” tab. Type “Library Search Path” in the Search and add /usr/local/lib path in “Library Search Path” and set it to recursive.

OpenCV-11

You are ready to build your OSX project now and it should be built without any issue.

CPP Code in Xcode:

OpenCV code will be done in CPP and you will have to make wrapper of CPP code in Objective-C. Later on, you can use Objective-C code in Swift language via objective-c bridging header. To add CPP code in Xcode, you need to follow more steps:

  1. To create CPP Header File, Select (File->New->File) and select Header File. You need to give an appropriate name to it as VideoSilicing.h. Add the code:
    #include <stdio.h>
    
    class VideoSilicing {
        public:
             static int processVideo(char&nbsp; const *fileName);
             static int getVideoWidth(char const *fileName);
             static int getVideoHeight(char const *fileName);
           };
  2. To create CPP File, Select (File->New->File) and select CPP File. Give an appropriate name to it as VideoSilicing.cpp. Note that the Header File in previous step is the Header File of this CPP File (VideoSilicing.cpp).
    OpenCV-12
    Add the code:

    #include "VideoSilicing.h"
    #include <opencv2/opencv.hpp> 
    
    int VideoSilicing::processVideo(char const *fileName){ 
    
          //to do code 
    
          return 0; 
    } 
    
    int VideoSilicing::getVideoWidth(char const *fileName){ &nbsp;
         int width = 0; 
    
         //to do code 
    
         return width; 
    } 
    
    int VideoSilicing::getVideoHeight(char const *fileName){ 
          cv::VideoCapture capture(fileName); // open file 
    
          int height = 0; 
    
         //to do code 
    
         return height; 
    }
  3. Now to add Objective-C code in XCode, Select (File->New->File) and select Header File. Also give it an appropriate name as ObjVideoSilicing.h. Add the code:
    #import <Foundation/Foundation.h>
    
    @interface ObjVideoSilicing : NSObject 
    
    + (void) objProcessVideoArea:(NSString*)fileName area:(NSRect)area; 
    
    + (int) objGetVideoWidth: (NSString*) fileName; 
    + (int) objGetVideoHeight: (NSString*) fileName; 
    
     
    @end
    
  4. To create Objective-C implementation code file in XCode, Select (File->New->File) and select Objective-C. Give it an appropriate name as ObjVideoSilicing.mm. Note that the Header File in the immediate previous step is the Header File of this Objective-C file. You also need to change the extension of file from “.m” to “.mm” because Objective-C interface for C code is in “.m” file and Objective-C interface for CPP code is in “.mm” file.
    OpenCV-13
    Add the code:

    #import "ObjVideoSilicing.h" 
    #import "VideSilicing.h" 
    
    @implementation ObjVideoSilicing 
    
    + (void) objProcessVideoArea: (NSString*) fileName area:(NSRect)area { 
        char const *p = fileName;&nbsp;&nbsp;&nbsp; &nbsp;
        VideoProcessing::processVideoRect( p, area.origin.x,
    area.origin.y, area.size.width, area.size.height ); 
    } 
    
    + (int) objGetVideoWidth: (NSString*) fileName { 
        char const *p = fileName;&nbsp;&nbsp;&nbsp; &nbsp;
        return VideoProcessing::getVideoWidth( p ); 
    } 
    
    + (int) objGetVideoHeight: (NSString*) fileName { 
        char const *p = fileName;&nbsp;&nbsp;&nbsp; &nbsp;
        return VideoProcessing::getVideoHeight( p ); 
    } 
    
    @end
  5. When you are creating Objective-C or CPP file in your Swift MAC project, Xcode will prompt you to add a bridge Header File in XCode. You will then allow Xcode to add bridging file. If you have already created bridge file in Swift code, then XCode will not prompt you next time. Include Objective-C file in bridging header file as:
    OpenCV-14

    #import "ObjVideoProcessing.h"
  6.  Now you can start your processing with OpenCV libraries by writing code in CPP file. If you want to include it in Swift project, give CPP code file interface in Objective-C file and include it in bridging header. Now you can use the OpenCV libraries in your Swift project.
    ObjVideoProcessing.objProcessVideoArea(videoPath, area:rect)