2021-05-12 14:32:11
Mac平台上OpenCV開發環境搭建
介紹下如何在Mac OS X上進行OpenCV專案的開發,嘗試的開發工具有Xcode(版本是4.6.1)和Eclipse,使用的OpenCV版本是2.4.6。
如果只是需要OpenCV的相關標頭檔案以及動態庫,請直接執行brew install opencv
(如果安裝了Homebrew的話),如果不行,請看下面的OpenCV原始碼編譯安裝過程。
1.安裝CMake
安裝CMake可以使用MacPorts,也可以使用Homebrew,如果以前安裝過兩者中的任何一個就用那個進行安裝吧,我用的是Homebrew,推薦使用Homebrew,真正的“佳釀”,命令如下:
sudo port install cmake //macports
sudo brew install cmake //homebrew
2.編譯OpenCV
OpenCV下載地址:http://sourceforge.net/projects/opencvlibrary/
目前最新版本是2.4.8,我使用的是2.4.6,下載後解壓,執行下面程式碼:
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
目錄下]
[註1:如果不需要了,想要解除安裝 OpenCV的話,可以回到release
目錄,執行sudo make uninstall
,然後手動刪除一些/usr/local
下與OpenCV有關的目錄和檔案]
[註2:如果不想把OpenCV安裝在預設的/usr/local/
目錄下的話,例如為了防止Homebrew中對opencv部分的報錯,而又無法使用Homebrew正常安裝opencv的情況下,可以考慮將opencv安裝到其他的位置,修改CMAKE_INSTALL_PREFIX=/usr/local
即可,但是在Eclipse中的專案中可能會出現問題,詳情看後面]
其他參考內容:
Building OpenCV from Source Using CMake, Using the Command Line
3.使用Xcode進行OpenCV專案開發
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
4.使用Eclipse進行OpenCV專案開發
如果使用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://www.linuxidc.com/Linux/2016-09/135030.htm
5.閱讀開源專案
閱讀開源專案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 ..
OpenCV官方教學中文版(For Python) PDF http://www.linuxidc.com/Linux/2015-08/121400.htm
Ubuntu Linux下安裝OpenCV2.4.1所需包 http://www.linuxidc.com/Linux/2012-08/68184.htm
Ubuntu 12.04 安裝 OpenCV2.4.2 http://www.linuxidc.com/Linux/2012-09/70158.htm
CentOS下OpenCV無法讀取視訊檔 http://www.linuxidc.com/Linux/2011-07/39295.htm
Ubuntu 12.04下安裝OpenCV 2.4.5總結 http://www.linuxidc.com/Linux/2013-06/86704.htm
Ubuntu 10.04中安裝OpenCv2.1九步曲 http://www.linuxidc.com/Linux/2010-09/28678.htm
基於QT和OpenCV的臉部辨識系統 http://www.linuxidc.com/Linux/2011-11/47806.htm
相關文章