This tutorial will cover the easiest way to install OpenCV 2.4.9 with Qt 5.4.1, without using CMake, which can be very helpful for beginners who do not want to invest time to make their own build. There might be more updated versions of both the softwares but the essential components are still the same.
OpenCV (Open Source Computer Vision) is an open source platform which consists of function aimed at real time computer vision. It can be used to make various image processing and computer vision applications with the help of its powerful libraries.
Qt is a cross-platform application framework that is widely used for developing application software that can be run on various software and hardware platforms with little or no change in the underlying codebase. It is the open-source competitor to the popular windows development platform Visual Studio.
Download Links :-
- OpenCV 2.4.9
- Qt 5.4.1 (qt-opensource-windows-x86-mingw491_opengl-5.4.1.exe 856 MB)
- Qt based OpenCV build
Note:- Please use only the versions of the softwares mentioned in this tutorial. It will NOT work for other versions of Qt and OpenCV.
Please let us know if the download links don't work.
Installation Steps :-
- Install Qt using the setup file you just downloaded. You may install it in the default location or any other location as you wish with only one restriction. There cannot be any spaces in the installation path. For example:- F:\Qt\Qt5.4.1' is a valid installation location whereas 'F:\Qt\Qt 5.4.1' is not.
- Extract OpenCV using the self-extracting archive that you just downloaded. Again, remember that there cannot be any spaces in the extraction location else Qt won't be able to read it. After extraction, you'll get two folders in that location- 'build' and 'sources'.
- Extact the OpenCV build in the same way. Now you'll get two folders called 'include' and 'build-qt' and a text file.
- Let's say you extracted the build to 'F:\OpenCV-2.4.9'. You will get a folder called 'build-qt' in that directory. To enable Qt finding the libraries of OpenCV, the location 'F:\OpenCV-2.4.9\build-qt\bin' has to be added to the System Path variable. You can find a way to do that here. Make sure you add this path to the end of the list. Do NOT replace the contents of the PATH variable. To ensure that you've added it, open a Command Prompt window and type 'path' and hit enter. Make sure that you can see the location there.
- Now we're ready to test our installation.
Running OpenCV with Qt for the first time :-
- Start Qt. Click on New Project -> Qt Console Application.
- Name your project and set the project location and click Next.
- You'll have two files in your project. A .pro file and a .cpp file. You will now need to add a few things to the .pro file. Open it up on Qt by clicking on it.
- Open the text file (OpenCV Include text for Qt.txt) that came with the build for OpenCV and copy its contents. Paste it at the end of the .pro file.
- Next, you have to edit the INCLUDEPATH and the LIBS variables. Remeber, you have to edit them according to the location where you extracted the build for OpenCV. Also note that the double slashes ('\\') in the INCLUDEPATH are very important. Please do not use single slashes for the path. For the LIBS variable, the '-l' (hyphen small L) in the beginning and the '\' (backslash) in the end are also important.
- Once you make the updates in step 5 to your .pro file, update these in the .txt file too because you will need to add this to the .pro file every time you make a new project.
Running your first example :-
- Download this code and paste it in the .cpp file.
- Edit the variable imgloc to the location of any image file present in your PC. Here you can either use forward slashes ('/') or double back slashes ('\\') as a file path separator.
- Hit 'Run' (Green button on the left side) or press 'Ctrl + R' and you should see something like this.
If you can't follow this tutorial, here's a video tutorial for the same.