Add OpenCV Library in QT

Source: Internet
Author: User

Configure OPENCV in Qt, read a lot of "tutorials" and finally succeed. Remember the process.

Native configuration:

  WINDOW7 32-bit system;

qt-opensource-windows-x86-mingw492-5.5.1;

OpenCV2.4.10.

For me, the configuration process is difficult, because at present I have not used QT and CMake, has been using VS compiled C + +, and has not used MinGW, so look at the various commands in the tutorial or execute files are more time-consuming. Success after this various attempts. Links to references in the end, one of the things that helped me the most was the plain English, very detailed.

Pre-preparation

The opensource version of QT5 uses MinGW, and unlike QT4,QT5, you cannot change the library file to VC in Qtcreater. OpenCV, after 2.4, provides only the VS version of the library file, located in the "vc1x" folder in%opencv%\build\x86 or%opencv%\build\x64 (%opencv% represents the OpenCV installation directory).

The first picture is the content of build\x86\ in opencv2.4.10, and the second picture is the content of build\x86\ in opencv2.3.1. It is obvious that there is a MinGW library folder in oepencv2.3.1 and opencv2.4.10 only supports VC libraries. Because the computer also has these two versions of the OpenCV library files, so the first configuration I directly use 2.3.1 version (do not need to recompile MinGW repository file), but after the following correct configuration, the test program has been unable to pass, the phenomenon is in detail below.

For these reasons, it is necessary to compile OpenCV into a mingw version of the library before Qt is configured. This is also why the tutorial to use a cmake software compiled OpenCV. Here, I can list the software that needs to be prepared before configuring OPENCV in QT5:

  qt-opensource-windows-x86-mingw492-5.5.1;

cmake-3.6.0-rc3-win32-x86;

opencv2.4.10.

The QT5 and CMake versions are the latest version I downloaded, and the time is now 2016-6-28. The following will not detail each step of the configuration step, where necessary I will attach the corresponding information, the information has been explained in very detailed. Here I focus on the problems I encountered in the configuration.

Installation of Qt5,cmake and OPENCV

There are countless tutorials to install, not to be mentioned here. Note the configuration of environment variables in Windows.

In the installation of QT5 Note installation MinGW. My version here is 4.9.2, as shown in.

  

After installing the QT5, note that to add the QT5 path to the environment variable, CMake will invoke the MinGW component program when using CMake to compile the OpenCV library later, that is, the MinGW program that is checked in the installation of QT, And will also call the QT itself compiler qmake.exe, the path of the two files are in the environment variable, specifically:

%qt5%\tools\mingw492_32\bin

%qt5%\5.5\mingw492_32\bin

The above%qt5% for my Qt installation directory, for example my is C:\Qt\Qt5.5.1, then it is equivalent to substituting the above path:

C:\Qt\Qt5.5.1\Tools\mingw492_32\bin

C:\Qt\Qt5.5.1\5.5\mingw492_32\bin

Compilation of the OpenCV library

Compiling the OpenCV library into the MinGW version requires the use of CMake software, and the other software works well I have not used it. When installing, be careful to tick the CMake path (path) to the environment variable, or you will see an invalid command prompt when you use the console (CMD or PowerShell) to invoke CMake to compile the installation command.

Note that you should not compile the MinGW version of OpenCV directly after the installation is complete, to ensure that Qt is installed. Otherwise, the necessary execution program is missing, resulting in a mingw library that cannot be compiled into the required.

Open CMake, refer to this information select OpenCV source file location and output file location, click Configure. Note that I did not follow this piece of information exactly, but rather a tutorial based on another piece of information. A silly summary, here need to click "2 times configure and 1 times generate".

I have chosen the C and C + + compilers here, specifically for the step point:

  

I picked "Specify native compilers" and chose the compiler in the next step because there was always an error selecting "Use default native compilers" before, I guess my default compiler was wrong.

  

My own choice of compilers is the mingw of the QT in the GCC and g++, such as. After you select Finish, you go to the normal steps in the other tutorials, such as.

  

After the success, before the second configue, there need to be two places to tick or change, first a virgin:

  

The chosen place is the Configure type generated by CMake, which can be filled in with Debug and releaseso that the resulting library file can indicate the compilation of the above two versions.

It is also necessary to tick the with_qtso that CMake compiles a library file that can be used by QT (I think it is here that the CMake to call the qmake instruction in QT).

At this point, you can click Generate to generate it.

I had an error after this, and after viewing it, it was because CMake called Qmake to execute the file, and the first line of my environment variable was C:\PYTHON27\LIB\SITE-PACKAGES\PYQT4 (This is because the previous use of this computer is installed PYQT), and this directory has a qmake executable file, but this file does not meet the requirements of this compilation, so always error. So I inserted C:\Qt\Qt5.5.1\5.5\mingw492_32\bin and %opencv_mingw%\bin into the front.

After that, open the console software, type Mingw32-make and Mingw32-make install, as previously configured, and the program will start compiling and installing in turn, with the corresponding results:

QT Configuration

Once the OpenCV MinGW library is compiled, it is ready to be configured directly in QT. Qt configuration is based on the QT project created after the pro file, by configuring the text in the pro file, pro file writing rules and techniques can refer to this information, not to repeat, the following is my profile content, using the test instance is QT Console application:

QT + = COREQT-= Guitarget = Testqtconfig + Consoleconfig-= App_bundletemplate = appsources + Main.cppincludepath + = F:/ OPENCV2.4.10/BUILD/INCLUDE/OPENCV F:/opencv2.4.10/build/include/opencv2 F:/opencv2.4.10/buil D/includelibs +=f:/opencv2.4.10/opencvmingw/lib/libcv2.dll.alibs +=f:/opencv2.4.10/opencvmingw/lib/libopencv_ Calib3d2410.dll.aLIBS +=f:/opencv2.4.10/opencvmingw/lib/libopencv_contrib2410.dll.alibs +=f:/opencv2.4.10/ Opencvmingw/lib/libopencv_nonfree2410.dll.alibs +=f:/opencv2.4.10/opencvmingw/lib/libopencv_ocl2410.dll.alibs + = F:/opencv2.4.10/opencvmingw/lib/libopencv_gpu2410.dll.alibs +=f:/opencv2.4.10/opencvmingw/lib/libopencv_ Photo2410.dll.aLIBS +=f:/opencv2.4.10/opencvmingw/lib/libopencv_objdetect2410.dll.alibs +=f:/opencv2.4.10/ Opencvmingw/lib/libopencv_legacy2410.dll.alibs +=f:/opencv2.4.10/opencvmingw/lib/libopencv_ml2410.dll.alibs +=F :/opencv2.4.10/opencvmingw/lib/libopencv_video2410.dll.alibs +=f:/opencv2.4.10/opencvmingw/lib/libopencv_feaTures2d2410.dll.aLIBS +=f:/opencv2.4.10/opencvmingw/lib/libopencv_highgui2410.dll.alibs +=f:/opencv2.4.10/ Opencvmingw/lib/libopencv_imgproc2410.dll.alibs +=f:/opencv2.4.10/opencvmingw/lib/libopencv_flann2410.dll.alibs +=f:/opencv2.4.10/opencvmingw/lib/libopencv_core2410.dll.a

I write a lot of trouble, the paths are copied and pasted many times, you can use the-l and-l parameters to simplify. File directory grading in Includepath the slash is interesting, I use the/(slash), according to the test and lookup data, I found \ (backslash) and \ \ (double backslash) can be used. The reason for using a console app as a test is that writing a test file is simple, and does not involve much of the structure and knowledge of QT programming, and the test code is simple, as follows:

#include <iostream> #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp># Include <opencv2/imgproc/imgproc.hpp> #include <opencv2/core/core.hpp>using namespace Cv;int main () {    std::cout<< "Fuck" <<std::endl;    Mat img;    img = Imread ("d:/c.jpg");    Imshow ("Test", IMG);    Waitkey (); return 0;}

The test was successful as follows:

During the configuration process, Originally used opencv2.3.1, this library has the MinGW version of the library file, do not need to recompile with the source file (this version of the OPENCV is very strange there is no source folder, and the version after 2.4.x and this version of the file organization structure is very different). Originally thought to be a convenience, but the actual use of Qt was found, Even if my. Pro file is configured and does not run correctly, a picture is displayed (the reason my profile is configured is that you can index the header file in the OpenCV library when you write code using include when you are finished configuring the Pro file). The phenomenon is that as long as the OPENCV function is not able to run the program properly, the printing string at the beginning of the program is not displayed, the end of the printed string is not to appear. Another phenomenon is that when you create a mat class using the mat keyword without making any other related OPENCV () function calls, there is no error, but whenever you use another function, such as Mat img, img = imread (...); Will go wrong. There is no compilation problem, but it is not possible to produce results.

Resources

Http://www.laganiere.name/opencvCookbook/chap1s1_2.shtml
http://blog.163.com/allegro_tyc/blog/static/337437682014102411829281/
http://stackoverflow.com/questions/14919186/qt-creator-5-0-1-with-opencv-2-3-4-on-windows/15268560#15268560
Http://blog.sina.com.cn/s/blog_7edf527e0101359b.html
Http://blog.chinaunix.net/uid-23592843-id-2495471.html

Http://www.cnblogs.com/bolgofzjr/p/5623032.html

Add OpenCV Library in QT

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.