# qtcreator中使用OpenCV

# 1.Qt使用qmake的工程添加OpenCV库

两种方式:

  • 1>使用pkg-config

前提是已经正确编译安装了OpenCV和pkg-config

验证pkg-config --libs opencv要能得到正确的输出,如下:

(detectron) lx@lx-rob:/media/lx/data/code$ pkg-config --libs opencv4
-L/home/lx/opencv/install/lib -lopencv_gapi -lopencv_stitching -lopencv_alphamat -lopencv_aruco -lopencv_barcode -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_dnn_objdetect -lopencv_dnn_superres -lopencv_dpm -lopencv_face -lopencv_freetype -lopencv_fuzzy -lopencv_hdf -lopencv_hfs -lopencv_img_hash -lopencv_intensity_transform -lopencv_line_descriptor -lopencv_mcc -lopencv_quality -lopencv_rapid -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_sfm -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_superres -lopencv_optflow -lopencv_surface_matching -lopencv_tracking -lopencv_highgui -lopencv_datasets -lopencv_text -lopencv_plot -lopencv_videostab -lopencv_videoio -lopencv_viz -lopencv_wechat_qrcode -lopencv_xfeatures2d -lopencv_shape -lopencv_ml -lopencv_ximgproc -lopencv_video -lopencv_xobjdetect -lopencv_objdetect -lopencv_calib3d -lopencv_imgcodecs -lopencv_features2d -lopencv_dnn -lopencv_flann -lopencv_xphoto -lopencv_photo -lopencv_imgproc -lopencv_core

关于OpenCV的安装配置可以参考(一)Linux下C++ OpenCV开发环境搭建 (opens new window)

qt creator工程添加‵OpenCV库: 选中工程名,右键,add library`


输入package name即可,在project.pro文件中会自动添加如下行:

unix: CONFIG += link_pkgconfig
unix: PKGCONFIG += opencv4
  • 2>External Library

添加OpenCV库选择External Library项


添加后,在project.pro文件中会自动添加如下行:

unix:!macx: LIBS += -L$$PWD/../../sw/opencv/install/lib/ -lopencv_alphamat

INCLUDEPATH += $$PWD/../../sw/opencv/install/include
DEPENDPATH += $$PWD/../../sw/opencv/install/include

在上面LIBS+=语句中只自动添加了-lopencv_alphamat,这是不够的,一个个手动添加很麻烦,如果还可以在.pro文件中调用pkg-config语句,如下

unix:!macx: LIBS += `pkg-config --libs opencv4`

INCLUDEPATH += $$PWD/../../sw/opencv/install/include
DEPENDPATH += $$PWD/../../sw/opencv/install/include

写法类似makefile

# 2.Debug时常见问题

00:26:23: Debugging starts
/media/lx/data/code/build-Demo-Desktop_Qt_5_14_0_GCC_64bit-Debug/Demo: error while loading shared libraries: libopencv_imgcodecs.so.405: cannot open shared object file: No such file or directory
00:26:23: Debugging has finished

这是因为在LD_LIBRARY_PATH中没有添加LIBS的路径,具体可查看LD_LIBRARY_PATH环境变量,qtcreator中的LD_LIBRARY_PATH的设置方式为

(adsbygoogle = window.adsbygoogle || []).push({});