參考 :
https://www.jianshu.com/p/ef806bffcb0e
https://blog.csdn.net/a13602955218/article/details/101625857
1.安裝homebrew
命令行一鍵安裝,國內加速可參考 https://www.jianshu.com/p/22820319f71b
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
安裝完后可以替換brew源,在國內加速
清華鏡像源
https://mirror.tuna.tsinghua.edu.cn/help/homebrew/
https://mirrors.tuna.tsinghua.edu.cn/help/homebrew-bottles/
# 1二進制包源 echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles' >> ~/.bash_profile source ~/.bash_profile # 2 倉庫源 $ cd "$(brew --repo)" $ git remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git $ cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core" $ git remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git $ brew update
- 在zsh下安裝使用brew
➜ ~ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" # 新的方式 Warning: The Ruby Homebrew installer is now deprecated and has been rewritten in Bash. Please migrate to the following command: /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
# 替換brew源,在國內加速
➜ ~ echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles' >> ~/.zshrc
➜ ~ source ~/.zshrc
➜ ~ cd "$(brew --repo)"
➜ Homebrew git:(stable) git remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git
➜ Homebrew git:(stable) cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
➜ homebrew-core git:(master) git remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git
➜ homebrew-core git:(master) brew update
Already up-to-date.
2.安裝OpenCV
# 搜索 $ brew search opencv ==> Formulae opencv opencv@2 opencv@3 # 安裝想要的版本 $ brew install opencv@3
安裝完后可以添加環境變量
export PATH="/usr/local/opt/opencv@3/bin:$PATH" export OpenCV_LIBS=/usr/local/opt/opencv@3/lib export OpenCV_INCLUDE_DIRS=/usr/local/opt/opencv@3/include
3 Xcode下使用
可參考 https://www.jianshu.com/p/a868e11e3f52
新建Xcode Project
添加Header Search Paths,路徑看自己的實際位置 /usr/local/Cellar/opencv@3/3.4.9_1/include
Library Search Paths中添加 /usr/local/Cellar/opencv@3/3.4.9_1/lib
右鍵黃色文件夾,添加New Group 重命名為head
在finder中前往 /usr/local/Cellar/opencv@3/3.4.9_1/lib 文件夾,將文件夾內所有dylib類型文件拖到head文件夾下,在彈出框中選定 copy items if needed
這里也可以參考 https://www.jianshu.com/p/a868e11e3f52 相關步驟
按組合鍵 command+shift+G 可以快速打開前往文件夾
修改main.cpp代碼 代碼參考:https://www.jianshu.com/p/a868e11e3f52
#include <iostream> #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/opencv.hpp> using namespace std; using namespace cv; IplImage* doCanny(IplImage* image_input, double lowThresh, double highThresh, double aperture) { if(image_input->nChannels != 1) return (0); IplImage* image_output = cvCreateImage(cvGetSize(image_input), image_input->depth, image_input->nChannels); cvCanny(image_input,image_output,lowThresh,highThresh,aperture); return(image_output); } int main(int argc, const char * argv[]) { cvNamedWindow("Camera" , CV_WINDOW_AUTOSIZE ); CvCapture* capture = cvCreateCameraCapture(CV_CAP_ANY); assert(capture != NULL); IplImage *frame = 0; frame = cvQueryFrame(capture); IplImage *frame_edge = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 1); while(1) { frame = cvQueryFrame(capture); if(!frame) break; cvConvertImage(frame,frame_edge,0); frame = cvCloneImage(frame_edge); frame_edge = doCanny(frame_edge,70,90,3); cvShowImage("Camera",frame_edge); char c = cvWaitKey(15); if(c == 27) break; } cvReleaseCapture(&capture); cvReleaseImage( &frame_edge ); cvReleaseImage( &frame); return (int)0; }
編譯運行,控制台可能會提示打開攝像頭的權限不足
OpenCV: not authorized to capture video (status 0), requesting...
2020-02-26 11:50:25.607042+0800 PrOpencv[2922:86917] [access] This app has crashed because it attempted to access privacy-sensitive data without a usage description. The app's Info.plist must contain an NSCameraUsageDescription key with a string value explaining to the user how the app uses this data.
有兩種方法解決,一種根據提示,在products文件夾新建Info.plist文件,並添加NSCameraUsageDescription key ,獲取文件夾權限,再次運行時,提示打開攝像頭,允許
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>NSCameraUsageDescription</key> <string>USE</string> <key>Privacy - Camera Usage Description</key> <string>YES</string> </dict> </plist>
另一種,直接定位到products文件夾內,雙擊運行編譯出的可執行文件,然后打開攝像頭權限即可
4 Clion下使用
安裝Clion
新建工程
配置Cmake ,修改CmakeList.txt
參考 https://blog.csdn.net/weixin_39393741/article/details/85070299
# MyPrOpencv 是工程名字
cmake_minimum_required(VERSION 3.15) # 版本 project(MyPrOpencv) # 工程名 # 設置OpenCV DIR set(OpenCV_DIR /usr/local/opt/opencv@3) # 尋找OpenCV.CMakeLists,以此找到包,並賦值各庫相關變量 find_package(OpenCV REQUIRED) # OpenCV_INCLUDE_DIRS是關於find_package的變量, # 包含了一個路徑,這樣可以在代碼中的#include做根目錄 include_directories( /usr/local/Cellar/opencv@3/3.4.9_1/include ) set(CMAKE_CXX_STANDARD 14) # 標准庫 # 添加對主函數的可執行文件 add_executable(MyPrOpencv main.cpp) # 鏈接OpenCV庫,OpenCV_LIBS為代表庫可執行文件的變量 target_link_libraries( MyPrOpencv ${OpenCV_LIBS} )
其中以下4句為新添加,OpenCV_LIBS為之前添加的環境變量,修改CMakeLists后需要Reload changes
# 設置OpenCV DIR set(OpenCV_DIR /usr/local/opt/opencv@3) # 尋找OpenCV.CMakeLists,以此找到包,並賦值各庫相關變量 find_package(OpenCV REQUIRED) # OpenCV_INCLUDE_DIRS是關於find_package的變量, # 包含了一個路徑,這樣可以在代碼中的#include做根目錄 include_directories( /usr/local/Cellar/opencv@3/3.4.9_1/include ) # 鏈接OpenCV庫,OpenCV_LIBS為代表庫可執行文件的變量 target_link_libraries( MyPrOpencv ${OpenCV_LIBS} )
修改main.cpp文件,同上
編譯成功以后可以找到編譯好的可執行文件,點擊運行,暫時沒找到添加攝像頭權限的方法。