简体   繁体   English

如何使 OpenCV 成为我的 qt 项目的默认库?

[英]How can I make OpenCV the default library for my qt projects?

Please suppose that I want to link OpenCV libraries in Qt-creator , in common, I will add headers using INCLUDEPATH and link libraries using the LIBS variable, which is used in the qmake file but if we use OpenCV in most of our projects then we have to include OpenCV library every time, so is there any way to add opencv libraries automatically at the time of creating a project.请假设我想在 Qt-creator 中链接 OpenCV 库,一般来说,我将使用INCLUDEPATH添加头文件,并使用LIBS变量添加库,该变量在 qmake 文件中使用,但如果我们在大多数项目中使用 OpenCV,那么我们每次都必须包含OpenCV库,所以有没有办法在创建项目时自动添加opencv库。 I use the below command to add OpenCV libraries for my projects every time.我每次都使用以下命令为我的项目添加 OpenCV 库。

INCLUDEPATH += -I/usr/local/include/opencv
LIBS += -L/usr/local/lib -lopencv_stitching -lopencv_superres ...and etc.

UPDATE更新

I will use the following headers for OpenCV4:我将为 OpenCV4 使用以下头文件:

INCLUDEPATH += /usr/local/include/opencv4

1) You can create a .prf (project feature) file in your mkspecs/features directory: 1) 您可以在mkspecs/features目录中创建一个 .prf(项目功能)文件:

/usr/share/qt5/mkspecs/features/opencv.prf /usr/share/qt5/mkspecs/features/opencv.prf

INCLUDEPATH += -I/usr/local/include/opencv
LIBS += -L/usr/local/lib -lopencv_stitching -lopencv_superres ...and another libraries

Now simply add CONFIG += opencv to your .pro file to have it working.现在只需将CONFIG += opencv添加到您的 .pro 文件中即可使其正常工作。 Or you can even auto-enable this feature by editing mkspecs/qconfig.pri :或者您甚至可以通过编辑mkspecs/qconfig.pri来自动启用此功能:

/usr/share/qt5/mkspecs/qconfig.pri /usr/share/qt5/mkspecs/qconfig.pri

...
CONFIG += ... opencv
...

BTW.顺便说一句。 qconfig.pri is a part of qt_config , which is loaded by all QMake's machine-dependent specs, so it should always work. qconfig.pri是一部分qt_config ,这是所有QMAKE的依赖于机器的规格装,所以它应该总是工作。 However, it's also possible to patch only a specific spec (for example, /usr/share/qt5/mkspecs/linux-g++/qmake.conf , or whatever is appropriate for your configuration).但是,也可以仅修补特定规范(例如, /usr/share/qt5/mkspecs/linux-g++/qmake.conf或任何适合您的配置的规范)。 Of course, it's even possible to add all these INCLUDEPATH+=... and LIBS+=... straight into that qmake.conf and get rid of the .prf file completely.当然,甚至可以将所有这些INCLUDEPATH+=...LIBS+=...直接添加到qmake.conf并完全摆脱 .prf 文件。

2) Alternatively, if you don't want to pollute Qt installation, you can use manual include: 2)或者,如果你不想污染Qt安装,你可以使用手动包含:

opencv.pri opencv.pri

INCLUDEPATH += -I/usr/local/include/opencv
LIBS += -L/usr/local/lib -lopencv_stitching -lopencv_superres ...and another libraries

myprogram.pro我的程序

include(path/to/opencv.pri)
...

When you installed opencv you must also install the opencv.pc file, this file can be used to make it simple, since Qt supports package.config, if so, it replaces what it shows by the following:当你安装 opencv 的时候你还必须安装opencv.pc文件,这个文件可以用来使它变得简单,因为 Qt 支持 package.config,如果是这样,它会用以下内容替换它显示的内容:

unix: CONFIG += link_pkgconfig
unix: PKGCONFIG += opencv

Actually Qt Creator offers a simple way, you just have to right click on the name of your project and select the option Add Library:实际上 Qt Creator 提供了一个简单的方法,您只需右键单击您的项目名称并选择选项添加库:

在此处输入图片说明

Then you will get a dialog where you must select the type of library:然后您将看到一个对话框,您必须在其中选择库的类型:

在此处输入图片说明

In this case I used the fourth option, and put the name of the library: opencv.在这种情况下,我使用了第四个选项,并输入了库的名称:opencv。

在此处输入图片说明

Then you press the next and finish buttons.然后按下一步和完成按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM