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]]

相关推荐
ZHOU_WUYI17 分钟前
4.metagpt中的软件公司智能体 (ProjectManager 角色)
人工智能·metagpt
靴子学长1 小时前
基于字节大模型的论文翻译(含免费源码)
人工智能·深度学习·nlp
AI_NEW_COME2 小时前
知识库管理系统可扩展性深度测评
人工智能
海棠AI实验室2 小时前
AI的进阶之路:从机器学习到深度学习的演变(一)
人工智能·深度学习·机器学习
hunteritself2 小时前
AI Weekly『12月16-22日』:OpenAI公布o3,谷歌发布首个推理模型,GitHub Copilot免费版上线!
人工智能·gpt·chatgpt·github·openai·copilot
IT古董3 小时前
【机器学习】机器学习的基本分类-强化学习-策略梯度(Policy Gradient,PG)
人工智能·机器学习·分类
centurysee3 小时前
【最佳实践】Anthropic:Agentic系统实践案例
人工智能
mahuifa3 小时前
混合开发环境---使用编程AI辅助开发Qt
人工智能·vscode·qt·qtcreator·编程ai
四口鲸鱼爱吃盐3 小时前
Pytorch | 从零构建GoogleNet对CIFAR10进行分类
人工智能·pytorch·分类
蓝天星空3 小时前
Python调用open ai接口
人工智能·python