1、安装cmake
brew install cmake
brew link cmake
2、下载
https://github.com/opencv/opencv
3、解压后
mkdir build
cd build
cmake -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX=./output ..输出路径
make
make install
在XCode中使用OpenCV
1. 创建一个空的command line工程。
2. 添加lib文件:在Build Phases 选项卡的Link Binary With Libraries 添加{buildpath编译的路径}/output/lib文件夹下的全部dylib文件
3. 添加lib文件查找支持: 在Build Settings选项卡,在“Library Search Paths”栏中输入"{buildpath编译的路径}/output/lib"
4. 添加头文件:在“Build Settings”选项卡,在“Header Search Paths”栏中输入:{buildpath编译的路径}/output/include
这时候可以编译了,但是跑不了
还需要在“Build Settings”选项卡的"Runpath Search Paths"也加上"{buildpath编译的路径}/output/lib"
还需要改库的签名
1)、codesign -f -s "Apple Development: your_name@email(XXXXXXXX)" your_path/output/lib/lib*.dylib
2)、codesign -f -s "Apple Development: your_name@email(XXXXXXXX)" /usr/local/opt/openjpeg/lib/*.dylib
demo代码
#include
#include
#include
//BGR -> Gray
cv::MatBGR2GRAY(cv::Mat img){
//get height and width
intwidth = img.cols;
intheight = img.rows;
//prepare output
cv::Mat out =cv::Mat::zeros(height, width, CV_8UC1);
//each y, x
for(inty =0; y < height; y++){
for(intx =0; x < width; x++){
//BGR -> Gray
out.at(y, x) =0.2126* (float)img.at(y, x)[2] \
+0.7152* (float)img.at(y, x)[1] \
+0.0722* (float)img.at(y, x)[0];
}
}
returnout;
}
intmain(intargc,constchar* argv[]){
//read image
cv::Mat img =cv::imread("imori.jpg", cv::IMREAD_COLOR);
//BGR -> Gray
cv::Mat out =BGR2GRAY(img);
//cv::imwrite("out.jpg", out);
cv::imshow("sample", out);
cv::waitKey(0);
cv::destroyAllWindows();
return0;
}