Xcode通過動態庫呼叫OpenCV
新建主工程:
File->New->Project…->左側選擇OSX->Application->CommandLine Tool->Next->填寫Product Name:TestApp, Language選擇C++;->選擇要儲存的路徑,Addto:選擇當前Project->Create
新建動態庫
File->New->Projdect…->左側選擇OSX->Framework& Library->Library->Next->填寫Product Name:DyImageProcess,FrameWork選擇None(Plain C/C++ Library) Type選擇Dynamic->選擇要儲存的路徑,Addto:選擇當前Project->Create
配置動態庫:
選擇DyImageProcess.xcodeproj->Build Settings->在Search框中輸入:Search
HeaderSearch Paths: /usr/local/include
LibrarySearch Paths: /usr/local/lib
Build Phrase->Link Binary With Libraries
點選+,在彈出的Dialog中點選Add Other…按鈕,輸入/,在彈出的Dialog中將路徑補充完整/usr/local/lib, 選擇:
libopencv_core.2.4.13.dylib
libopencv_highgui.2.4.13.dylib
libopencv_imgproc.2.4.13.dylib
libopencv_ml.2.4.13.dylib
可以按下command實現多選。
在左側的選擇DyImageProcess.xcodeproj工程右鍵New File…,在彈出的Dialog中選擇C++ File, 點選Next,在Dialog中填寫FileName:IPExport,選中Also create a header file,點選Next,
在IPExport.hpp中新增如下程式碼:
extern “C” void DoImageProcess();
在IPExport.cpp中新增如下程式碼:
#include "IPExport.hpp" #include <iostream> #include <opencv2/opencv.hpp> //using namespace std; using namespace cv; void DoImageProcess() { string path = "Test.jpg"; Mat image = imread(path); namedWindow("Test"); imshow("Test", image); Mat gray; cvtColor(image, gray, COLOR_RGBA2GRAY); namedWindow("gray"); imshow("gray", gray); waitKey(0); }
Build,生成libDyImageProcess.dylib
配置主工程:
Main.cpp
#include <iostream>
#include "IPExport.hpp"
int main(int argc, const char * argv[]) {
// insert code here...
std::cout << "Test OpenCV!\n";
DoImageProcess();
return 0;
}
BuildSetting:
HeaderSearch Paths: ./DyImageProcess
UserHeader Search Path: $(BuilT_PRODUCTS_DIR) 選擇recursive
BuildPhases:
Link Binary With Libraries: 點選+,在彈出的Dialog內選擇Workspace-> libDyImageProcess.dylib
Build, 在相應的Debug或者Release目錄下放置一個Test.cpp的檔案,執行,結果如下:
按任意鍵,退出。