背景

因为工作需求,本人要在MAC端使用OpenCV实现一些视觉功能,配置环境成了最大的阻碍,网上查了很多相关资料和博客,都因为版本环境问题屡试屡败,不过经历重重尝试,笔者最终还是配置成功并运行了自己的源码.当然成功的关键还是因为笔者站在了巨人的肩膀上,借鉴了很多网上的教程

安装opencv

可以通过源码安装,但mac上使用brew安装更加方便

1
2
3
4
5
brew search opencv
# 最新版本是opencv4,这里安装openc@2
brew install opencv@2
#安装成功后查看
brew info opencv@2

显示相应信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
opencv@2: stable 2.4.13.7 (bottled) [keg-only]
Open source computer vision library
https://opencv.org/
/usr/local/Cellar/opencv@2/2.4.13.7_11 (278 files, 35.1MB)
Poured from bottle on 2020-08-14 at 15:44:38
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/opencv@2.rb
License: BSD-3-Clause
==> Dependencies
Build: cmake ✔, pkg-config ✔
Required: eigen ✔, ffmpeg ✔, jpeg ✔, libpng ✔, libtiff ✔, numpy@1.16 ✔, openexr ✔
==> Requirements
Required: macOS is required ✔
==> Caveats
opencv@2 is keg-only, which means it was not symlinked into /usr/local,
because this is an alternate version of another formula.

If you need to have opencv@2 first in your PATH run:
echo 'export PATH="/usr/local/opt/opencv@2/bin:$PATH"' >> /Users/tang/.bash_profile

For compilers to find opencv@2 you may need to set:
export LDFLAGS="-L/usr/local/opt/opencv@2/lib"
export CPPFLAGS="-I/usr/local/opt/opencv@2/include"

For pkg-config to find opencv@2 you may need to set:
export PKG_CONFIG_PATH="/usr/local/opt/opencv@2/lib/pkgconfig"

Demo

main.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace cv;
using std::string;


int main(int argc, const char * argv[]) {
string path = "/Users/*/Desktop/test.jpg";
Mat image = imread(path);
namedWindow("origin");
imshow("origin", image);

Mat gray;
cvtColor(image, gray, COLOR_RGBA2GRAY);
namedWindow("gray");
imshow("gray", gray);
waitKey(0);

return 0;
}

Build

xcode失败

参考了https://www.jianshu.com/p/564c8b352c7f

使用xcode,在Header Search Paths里输入:/usr/local/include 在Library Search Paths里输入:/usr/local/lib

但是build失败。

Build

1
g++ main.cpp

提示

1
2
main.cpp:9:10: fatal error: 'opencv2/opencv.hpp' file not found
#include <opencv2/opencv.hpp>
1
g++ -o main main.cpp -L/usr/local/opt/opencv@2/lib -I/usr/local/opt/opencv@2/include

提示

1
2
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

pkg-config

后参考https://www.cnblogs.com/zhonghuasong/p/5975975.html

1
g++ $(pkg-config --cflags --libs opencv) main.cpp

任然失败。后来查看brew info opencv@2,提示

1
2
For pkg-config to find opencv@2 you may need to set:
export PKG_CONFIG_PATH="/usr/local/opt/opencv@2/lib/pkgconfig"

所以先export

1
2
3
4
5
6
export PKG_CONFIG_PATH="/usr/local/opt/opencv@2/lib/pkgconfig"
#此时 可以查看具体引用库
pkg-config --cflags --libs opencv

# build
g++ $(pkg-config --cflags --libs opencv) main.cpp

这样就成功了。效果如下

img

Demo2

下面代码可以打开摄像头,读取视频信息。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#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, 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;
}

image-20200814164625520

参考文章

OpenCV:mac上安装和配置

MAC下配置OpenCV的具体方法(2016年最新)

Mac下用g++编译opencv程序报错