MAC下使用homebrew安装安装opencv,配置在Clion和Xcode上使用
参考 :
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文件,同上
编译成功以后可以找到编译好的可执行文件,点击运行,暂时没找到添加摄像头权限的方法。