opencv开发环境搭建

从github下载opencv最新源码https://github.com/opencv/opencv,目前最新是`5e93c8202363a13fc72df30f8c14069c5ab66e42`.

Ubuntu环境下编译

安装依赖库:

1
2
3
4
5
6
7
8
9
sudo apt-get install build-essential

sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev

sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev

git clone https://github.com/opencv/opencv.git

sudo apt-get install cmake-gui

Mac环境下编译

进入源码路径,新建一个release的文件夹,并进入,执行:

1
2
3
cmake -G "Unix Makefiles" ..
make
sudo make install

编译完成后会在release生成lib目录,lib下存放所有编译成的动态库,可能与ubuntu下编译结果不同,ubuntu下编译只生成libopencv_world.so一个动态库,而mac下会生成opencv_core opencv_highgui opencv_imgproc opencv_ml opencv_objdetect opencv_photo opencv_video opencv_dnn opencv_imgcodecs opencv_shape等多个动态库.执行make install后会将头文件拷贝到/usr/local/include/下,将动态库拷贝到/usr/local/lib/下,将jar包等其他文件拷贝到/usr/local/share/OpenCV/下,makefile脚本加入动态链接库:

1
2
testopencv:main.cpp
g++ $+ -o $@ -lopencv_core -lopencv_highgui -lopencv_imgproc -lopencv_ml -lopencv_objdetect -lopencv_photo -lopencv_video -lopencv_dnn -lopencv_imgcodecs -lopencv_shape

main.cpp下输入下面测试代码:

1
2
3
4
5
6
7
8
9
10
11
12
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
using namespace cv;
int main(int argc, char *argv[])
{
Mat image = imread("1.png");
namedWindow("img");
imshow("img", image);
waitKey(0);
return 0;
}

在生成的执行文件同目录下放入名字为1.png的图片.

配置QT环境

在新建的QT工程中的.pro文件中添加如下配置代码:

1
2
3
4
5
6
7
INCLUDEPATH += /usr/local/include
INCLUDEPATH += /usr/local/include/opencv
INCLUDEPATH += /usr/local/include/opencv2
LIBS += -L/usr/local/lib \
-lopencv_core \
-lopencv_highgui \
-lopencv_imgproc \

完成以上步骤后按理应该是能成功的,但是运行时发现会出现如下的错误。

1
2
3
4
dyld: Symbol not found: __cg_jpeg_resync_to_restart
Referenced from: /System/Library/Frameworks/ImageIO.framework/Versions/A/ImageIO
Expected in: /usr/local/lib/libjpeg.8.dylib
in /System/Library/Frameworks/ImageIO.framework/Versions/A/ImageIO

针对以上问题,在项目-运行配置中,增加变量DYLD_LIBRARY_PATH值为/Application/QT5.7.0/5.7/clang_64/lib:/usr/local/lib

或者: 将“”目录下的对应动态链接库文件创建软连接到“/usr/local/lib”目录下:

1
2
3
4
$ cd /System/Library/Frameworks/ImageIO.framework/Versions/A/Resources
$ sudo ln -sf libJPEG.dylib /usr/local/lib/libJPEG.dylib
$ sudo ln -sf libPng.dylib /usr/local/lib/libPng.dylib
$ sudo ln -sf libTIFF.dylib /usr/local/lib/libTIFF.dylib

qt+opencv常见问题

在Mac上运行以上代码时,提示以下错误:

1
2
3
4
dyld: Symbol not found: __cg_jpeg_resync_to_restart
Referenced from: /System/Library/Frameworks/ImageIO.framework/Versions/A/ImageIO
Expected in: /usr/local/lib/libJPEG.dylib
in /System/Library/Frameworks/ImageIO.framework/Versions/A/ImageIO

解决办法是将“”目录下的对应动态链接库文件创建软连接到“/usr/local/lib”目录下:

1
2
3
4
5
$ pwd
/System/Library/Frameworks/ImageIO.framework/Versions/A/Resources
$ sudo ln -sf libJPEG.dylib /usr/local/lib/libJPEG.dylib
$ sudo ln -sf libPng.dylib /usr/local/lib/libPng.dylib
$ sudo ln -sf libTIFF.dylib /usr/local/lib/libTIFF.dylib

stackoverflow上面另一种解决方案: If using Qt Creator, you have to uncheck the Add build library search path to DYLD_LIBRARY_PATH and DYLD_FRAMEWORK_PATH option from the Run section in the Projects tab: image

坚持原创技术分享,您的支持将鼓励我继续创作!