Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 usingINCLUDEPATH 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. I use the below command to add OpenCV libraries for my projects every time.

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:

INCLUDEPATH += /usr/local/include/opencv4
like image 861
Saeed Masoomi Avatar asked Jan 04 '23 03:01

Saeed Masoomi


2 Answers

1) You can create a .prf (project feature) file in your mkspecs/features directory:

/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. Or you can even auto-enable this feature by editing 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. 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). 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.

2) Alternatively, if you don't want to pollute Qt installation, you can use manual include:

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)
...
like image 179
Matt Avatar answered Jan 05 '23 15:01

Matt


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:

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:

enter image description here

Then you will get a dialog where you must select the type of library:

enter image description here

In this case I used the fourth option, and put the name of the library: opencv.

enter image description here

Then you press the next and finish buttons.

like image 43
eyllanesc Avatar answered Jan 05 '23 17:01

eyllanesc