opencv开发环境搭建

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

Ubuntu环境下编译

安装依赖库:

复制代码
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的文件夹,并进入,执行:

复制代码
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脚本加入动态链接库:

复制代码
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下输入下面测试代码:

复制代码
#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文件中添加如下配置代码:

复制代码
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 \

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

复制代码
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"目录下:

复制代码
$ 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上运行以上代码时,提示以下错误:

复制代码
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"目录下:

复制代码
$ 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:

![[8.Attachments/image/e7fa96f0a446cfa74bbefa0fb52e2cd3_MD5.png]]

相关推荐
hello_ejb33 分钟前
聊聊Spring AI的MilvusVectorStore
java·人工智能·spring
HR Zhou6 分钟前
群体智能优化算法-算术优化算法(Arithmetic Optimization Algorithm, AOA,含Matlab源代码)
人工智能·算法·数学建模·matlab·优化·智能优化算法
yolo大师兄14 分钟前
【YOLO系列(V5-V12)通用数据集-火灾烟雾检测数据集】
人工智能·深度学习·yolo·目标检测·机器学习
jndingxin18 分钟前
OpenCV 图形API(15)计算两个矩阵(通常代表二维向量的X和Y分量)每个对应元素之间的相位角(即角度)函数phase()
人工智能·opencv
liruiqiang0534 分钟前
循环神经网络 - 机器学习任务之同步的序列到序列模式
网络·人工智能·rnn·深度学习·神经网络·机器学习
JOYCE_Leo1635 分钟前
图像退化对目标检测的影响 !!
人工智能·目标检测·目标跟踪
IT观察1 小时前
Spark 2.0携手Solcore:AI重构去中心化质押算力生态 !
人工智能·重构·spark
摆烂仙君1 小时前
3D意识(3D Awareness)浅析
人工智能·深度学习·计算机视觉·3d
cnbestec1 小时前
Hello Robot创新突破!Stretch3机器人搭载RUMs模型实现未知环境中“即插即用”
人工智能·深度学习·机器人
说私域1 小时前
定制开发开源AI智能名片S2B2C商城小程序:技术赋能商业价值实现路径研究
大数据·人工智能·小程序·开源