今天大部分時間都是在琢磨如何在Mac OS X上進行OpenCV項目的開發,嘗試的開發工具備Xcode(版本是4.6.1)和Eclipse,使用的OpenCV版本是2.4.6。html
若是隻是須要OpenCV的相關頭文件以及動態庫,請直接執行brew install opencv
(若是安裝了Homebrew的話),若是不行,請看下面的OpenCV源碼編譯安裝過程。linux
安裝CMake可使用MacPorts,也可使用Homebrew,若是之前安裝過二者中的任何一個就用那個進行安裝吧,我用的是Homebrew,推薦使用Homebrew,真正的「佳釀」,命令以下:c++
sudo port install cmake //macports sudo brew install cmake //homebrew
OpenCV下載地址:http://sourceforge.net/projects/opencvlibrary/git
目前最新版本是2.4.8,我使用的是2.4.6,下載後解壓,執行下面代碼:github
cd <path-to-opencv-source> mkdir release cd release cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local .. make sudo make install
[完成以後在/usr/local/include
目錄下便有了opencv
和opencv2
兩個目錄,在/usr/local/lib
目錄下有不少的opencv
相關的動態庫,例如libopencv_core.dylib
等等,還有幾個其餘的文件,它們都存放在/usr/local
目錄下]shell
[注1:若是不須要了,想要卸載 OpenCV的話,能夠回到release
目錄,執行sudo make uninstall
,而後手動刪除一些/usr/local
下與OpenCV有關的目錄和文件]xcode
[注2:若是不想把OpenCV安裝在默認的/usr/local/
目錄下的話,例如爲了防止Homebrew中對opencv部分的報錯,而又沒法使用Homebrew正常安裝opencv的狀況下,能夠考慮將opencv安裝到其餘的位置,修改CMAKE_INSTALL_PREFIX=/usr/local
便可,可是在Eclipse中的項目中可能會出現問題,詳情看後面]ide
其餘參考內容:工具
Building OpenCV from Source Using CMake, Using the Command Line開發工具
1.Open Xcode, choose New -> New Project -> Command Line Tool
2.Name it and select C++
for type
3.Click on your project from the left menu. Click the build settings
tab from the top. Filter all. Scroll to Search Paths
. Under header search paths
, for debug and release, set the path to /usr/local/include
. Under library search paths
, set the path to $(PROJECT_DIR)
. Finally, check if C++ standard library
is libstdc++
or not, if not, change it to this!
4.Click on your project from the left menu. File->New->New Group
, Name the group OpenCV Frameworks
.
5.Select the folder (group) you just labeled, OpenCV Frameworks
in the left menu. Go to File -> add Files
, Type /
, which will allow you to manually go to a folder. Go to -> /usr/local/lib
6.Select both of these files, libopencv_core.dylib
, libopencv_highgui.dylib
, and click Add
. (you may need to add other library files from this folder to run other code.)
7.You must include this line of code in the beginning of your main.cpp file:#include <opencv2/opencv.hpp>
能夠修改main.cpp,代碼以下,運行結果就是顯示一張指定的圖片。
#include <opencv2/opencv.hpp> using namespace cv; int main(int argc, char** argv) { Mat image; image = imread("/Users/hujiawei/Pictures/others/other_naicha/naicha.jpg", 1); namedWindow("Display Image", WINDOW_AUTOSIZE); imshow("Display Image", image); waitKey(0); return 0; }
其餘參考內容:
C++ linking error after upgrading to Mac OS X 10.9 / Xcode 5.0.1
MathLink linking error after OS X 10.9 (Mavericks) upgrade
若是使用Eclipse開發的話按照下面的步驟進行:
1.按照正常的步驟,使用Eclipse創建一個Mac C++
工程,包含一個cpp文件
2.右擊工程名, 選擇Properties
,在屬性配置頁中選擇,點擊C/C++ Build
, 在下拉選項中選擇 Settings
. 在右邊的選項卡中選擇 Tool Settings
。
3.在GCC C++ Compiler
選項列表中選擇Includes
,在Include paths(-l)
中添加安裝好的opencv的頭文件存放目錄:/usr/local/include/
[存放opencv頭文件的目錄,自行看狀況而定]
4.在MacOS X C++Linker
選項列表中選擇Library
,在Library search path (-L)
中添加安裝好的opencv dylib文件存放目錄:/usr/local/lib/
[通過個人測試只能是這個目錄!其餘目錄甚至是它的子目錄都不行!若是在其餘路徑中,複製過來也行!]
5.在MacOS X C++Linker
選項列表中選擇Library
, 在Libraries(-l)
中依次點擊+
號,添加須要使用的lib文件(一般狀況下,使用前三個,注意不要包括前綴lib
,能夠添加版本號):
opencv_core opencv_imgproc opencv_highgui opencv_ml opencv_video opencv_features2d opencv_calib3d opencv_objdetect opencv_contrib opencv_legacy opencv_flann
6.從新build項目便可。
若是遇到問題ld: symbol(s) not found for architecture x86_64
,先檢查代碼中是否須要包含尚未添加的庫文件,再檢查是不是其餘問題。若是是Mac平臺,下面還有一個關於問題ld: symbol(s) not found for architecture x86_64
的解釋可供參考:
There are two implementations of the standard C++ library available on OS X: libstdc++ and libc++. They are not binary compatible and libMLi3 requires libstdc++. On 10.8 and earlier libstdc++ is chosen by default, on 10.9 libc++ is chosen by default. To ensure compatibility with libMLi3, we need to choose libstdc++ manually. To do this, add -stdlib=libstdc++ to the linking command.
更多相關內容參考:
http://blog.sciencenet.cn/blog-702148-657754.html
閱讀開源項目Mastering OpenCV with Practical Computer Vision Projects中的代碼,以第8章Face Recognition using Eigenfaces or Fisherfaces爲例
編寫一個shell,內容以下(修改自README.txt
),其中的OpenCV_DIR
爲OpenCV源碼編譯後獲得的文件夾(如上面的release目錄),執行這個shell即可以獲得Xcode項目,固然打開這個項目以後還要修改相應的配置。
export OpenCV_DIR="/Volumes/hujiawei/Users/hujiawei/Android/opencv-2.4.6.1/build" mkdir build cd build cp $OpenCV_DIR/../data/lbpcascades/lbpcascade_frontalface.xml . cp $OpenCV_DIR/../data/haarcascades/haarcascade_eye.xml . cp $OpenCV_DIR/../data/haarcascades/haarcascade_eye_tree_eyeglasses.xml . cmake -G Xcode -D OpenCV_DIR=$OpenCV_DIR ..