mac下opencv编译使用

2020-04-10  本文已影响0人  王卓是个音视频开发工程师

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代码

<code>

#include<opencv2/core.hpp>

#include<opencv2/highgui.hpp>

#include<iostream>

//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;

}

</code>

上一篇下一篇

猜你喜欢

热点阅读