Mac下OpenCV環境搭建
轉載自:http://blog.csdn.net/lkj345/article/details/50910613
安裝Homebrew
在終端中輸入以下程式,
<code class="hljs ruby has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">curl -<span class="hljs-constant" style="box-sizing: border-box;">LsSf</span> <span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">http:</span>/<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/github.com/mxcl</span><span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/homebrew/tarball</span><span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/master | sudo tar xvz -C/usr</span><span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/local --strip 1</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
編譯OpenCV
通過Homebrew安裝cmake,
<code class="hljs bash has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">sudo</span> brew install cmake</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
去OpenCV官網,選擇OpenCV For Linux/Mac,當前最新版本是3.1.0,下載opencv-3.1.0,大小不到80M,這比我想象中的要小得多,假設我下載解壓後的路徑是/Users/mac/Downloads/opencv-3.1.0。
開始編譯OpenCV,注意cmake一步新增-D WITH_IPP=ON,否則會出現library not found for -lippicv的錯誤,
<code class="hljs lasso has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">cd /Users/mac/Downloads/opencv<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">-</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3.1</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">.0</span> mkdir release cd release cmake <span class="hljs-attribute" style="box-sizing: border-box;">-D</span> WITH_IPP<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">ON</span> <span class="hljs-attribute" style="box-sizing: border-box;">-D</span> CMAKE_BUILD_TYPE<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span>RELEASE <span class="hljs-attribute" style="box-sizing: border-box;">-D</span> CMAKE_INSTALL_PREFIX<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span>/usr/<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">local</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">..</span> make sudo make install</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li></ul>
這樣安裝好的library檔案在/usr/local/lib中,header檔案在/usr/local/include中。
Xcode配置OpenCV
接下來在Xcode中配置OpenCV,新建Xcode工程,我使用的Xcode版本是7.2.1。
新建OS X->Application->Command Line Tool,新增工程名,語言選擇C++。
右鍵左側工程名,新建Group,命名為OpenCV Frameworks,右鍵OpenCV Frameworks,新增檔案,輸入/,將相應路徑名改為/usr/local/lib,選擇所有跟OpenCV有關的動態庫,並新增,如下圖,
點選左側工程名->Build Settings->選擇All選項。
搜尋Library Search Paths,新增/usr/local/lib。
搜尋Header Search Paths,新增/usr/local/include。
在main.cpp中鍵入如下程式碼,
<code class="hljs cpp has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#include <opencv2/opencv.hpp></span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">using</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">namespace</span> cv; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> main(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> argc, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">const</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">char</span> * argv[]) { Mat image; image = imread(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"the path of a image"</span>); namedWindow(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"test"</span>, WINDOW_AUTOSIZE); imshow(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"test"</span>, image); waitKey(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>; }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li></ul>
如果不出意外的話,將會顯示出特定路徑下的一張圖片,點選任意鍵退出程式。
如果想要解除安裝的話,去到之前建立release資料夾中執行如下程式碼,對於我來說是/Users/mac/Downloads/opencv-3.1.0/release
<code class="hljs bash has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">sudo</span> make uninstall</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>