Upload
rashed44
View
232
Download
0
Embed Size (px)
Citation preview
8/13/2019 OpenCV Install Guide
1/17
1/9/14 OpenCV 2 Cookbook
www.laganiere.name/opencvCookbook/chap1s1_2.shtml#3
OpenCV 2 Computer Vision Application ProgrammingCookbook
Welcome to the Author's Website
1. Installing the OpenCV Library2. Creating an OpenCV project with MS
Visual C++3. Creating an OpenCV project with Qt4. Loading, displaying andsaving
images5. Creating a GUI application using Qt
1. Playing with Images
1.1.2 Installing the OpenCV Library for Qt
The Qt SDKis probably the best tool to build cross-platform OpenCV applications. Inaddition, it offers nice UI tools and a complete API. Finally, it comes with Qt Creator, afriendly IDE for developing C++ projects. This recipe will show you, in details, how toinstall Qt, compile the OpenCVlibrary for it and finally run your first OpenCVapplication.
At the time of writing this recipe, the latest Qt version was 4.7.3 (Qt Creator 2.2.1).Current version of OpenCV was 2.3.1 (August 2011). We will also use CMake whichwas at version 2.8.5. The installation has been done on Windows 7. Please refer to theOpenCV Cookbookfor more details.
1. The first step is obviously to download Qt. Simply run the installer with alldefault option. Once installed, you can run Qt Creator.
2. Lets start by creating a simple Hello Worldapplication on the Windows console,just to make sure everyt ing is working properly. Click on Create Project...
and choose Qt Console Application.
0. Introduction
1. Playing with Images
2.Manipulating the Pixels
3. Processing Images withClasses
4. Counting the Pixels withHistograms
5. Transforming images withmorphological operations
6. Filtering the Images
7. Extracting Lines, Contoursand Components
8. Detecting and MatchingInterest Points
9. Estimating ProjectiveRelations in Images
10. Processing VideoSequences
http://www.laganiere.name/opencvCookbook/chap5.shtmlhttp://www.laganiere.name/opencvCookbook/chap5.shtmlhttp://www.laganiere.name/opencvCookbook/chap4.shtmlhttp://www.laganiere.name/opencvCookbook/chap3.shtmlhttp://www.googleadservices.com/pagead/aclk?sa=L&ai=CdGnbTczOUp_OIcKAmwW0oIDYD-XlxbkElZr_kn2IxPGPLRABILXWqSVQ-tqw9P7_____AWAzoAHD-9DUA8gBAqgDAcgDwQSqBJ0BT9CpjrKqEsujcw9Qr7-6jDIPy8YxNtVclxiwe4iR_UWGJtzzsojoE6bvmixNDTaTYlPpLEDpJzpbfgwMLhQkSvzDKDh20Nr5CvAHv4cPvHLxxTvArXlN_m-Jl9AOCIZZ6BA5lM60GocXQhJs2gV39H-nu7BDKo7_oFyoN6y_FzELvrrOS2UJJX2_-9JtMwRZVXW6Pb1qjXk5LL4fIogGAaAGAoAHpYSvKw&num=1&cid=5GiW2j45317Yvssod0FuFTrZ&sig=AOD64_0Gy0Q--lsJAKXCgQc8-9jsXyID3Q&client=ca-pub-8501294875466025&adurl=http://k.keyade.com/kack/1/%3FkaPt%3Dgoogle%26kaTckM%3Dp%26kaPcId%3D98678%26kaTckId%3D33564446413%26kaAcId%3D191%26kaCpnId%3D%26kaNtw%3Dcontent%26kaKwd%3D%26kaDmn%3Dwww.laganiere.name%26kaTgt%3D%26kaNtw2%3Dd%26kaMtTp%3D%26kaAdPos%3Dnone%26kaDev%3Dchttp://www.googleadservices.com/pagead/aclk?sa=L&ai=CdGnbTczOUp_OIcKAmwW0oIDYD-XlxbkElZr_kn2IxPGPLRABILXWqSVQ-tqw9P7_____AWAzoAHD-9DUA8gBAqgDAcgDwQSqBJ0BT9CpjrKqEsujcw9Qr7-6jDIPy8YxNtVclxiwe4iR_UWGJtzzsojoE6bvmixNDTaTYlPpLEDpJzpbfgwMLhQkSvzDKDh20Nr5CvAHv4cPvHLxxTvArXlN_m-Jl9AOCIZZ6BA5lM60GocXQhJs2gV39H-nu7BDKo7_oFyoN6y_FzELvrrOS2UJJX2_-9JtMwRZVXW6Pb1qjXk5LL4fIogGAaAGAoAHpYSvKw&num=1&cid=5GiW2j45317Yvssod0FuFTrZ&sig=AOD64_0Gy0Q--lsJAKXCgQc8-9jsXyID3Q&client=ca-pub-8501294875466025&adurl=http://k.keyade.com/kack/1/%3FkaPt%3Dgoogle%26kaTckM%3Dp%26kaPcId%3D98678%26kaTckId%3D33564446413%26kaAcId%3D191%26kaCpnId%3D%26kaNtw%3Dcontent%26kaKwd%3D%26kaDmn%3Dwww.laganiere.name%26kaTgt%3D%26kaNtw2%3Dd%26kaMtTp%3D%26kaAdPos%3Dnone%26kaDev%3Dchttp://www.laganiere.name/opencvCookbook/chap8.shtmlhttp://www.laganiere.name/opencvCookbook/chap7.shtmlhttp://www.laganiere.name/opencvCookbook/chap2.shtmlhttp://www.laganiere.name/opencvCookbook/chap1.shtmlhttp://www.laganiere.name/opencvCookbook/index.shtmlhttp://www.laganiere.name/http://www.laganiere.name/http://www.laganiere.name/http://www.laganiere.name/opencvCookbook/chap10.shtmlhttp://www.laganiere.name/opencvCookbook/chap9.shtmlhttp://www.laganiere.name/opencvCookbook/chap8.shtmlhttp://www.laganiere.name/opencvCookbook/chap7.shtmlhttp://www.laganiere.name/opencvCookbook/chap6.shtmlhttp://www.laganiere.name/opencvCookbook/chap5.shtmlhttp://www.laganiere.name/opencvCookbook/chap4.shtmlhttp://www.laganiere.name/opencvCookbook/chap3.shtmlhttp://www.laganiere.name/opencvCookbook/chap2.shtmlhttp://www.laganiere.name/opencvCookbook/chap1.shtmlhttp://www.laganiere.name/opencvCookbook/index.shtmlhttp://www.googleadservices.com/pagead/aclk?sa=L&ai=CdGnbTczOUp_OIcKAmwW0oIDYD-XlxbkElZr_kn2IxPGPLRABILXWqSVQ-tqw9P7_____AWAzoAHD-9DUA8gBAqgDAcgDwQSqBJ0BT9CpjrKqEsujcw9Qr7-6jDIPy8YxNtVclxiwe4iR_UWGJtzzsojoE6bvmixNDTaTYlPpLEDpJzpbfgwMLhQkSvzDKDh20Nr5CvAHv4cPvHLxxTvArXlN_m-Jl9AOCIZZ6BA5lM60GocXQhJs2gV39H-nu7BDKo7_oFyoN6y_FzELvrrOS2UJJX2_-9JtMwRZVXW6Pb1qjXk5LL4fIogGAaAGAoAHpYSvKw&num=1&cid=5GiW2j45317Yvssod0FuFTrZ&sig=AOD64_0Gy0Q--lsJAKXCgQc8-9jsXyID3Q&client=ca-pub-8501294875466025&adurl=http://k.keyade.com/kack/1/%3FkaPt%3Dgoogle%26kaTckM%3Dp%26kaPcId%3D98678%26kaTckId%3D33564446413%26kaAcId%3D191%26kaCpnId%3D%26kaNtw%3Dcontent%26kaKwd%3D%26kaDmn%3Dwww.laganiere.name%26kaTgt%3D%26kaNtw2%3Dd%26kaMtTp%3D%26kaAdPos%3Dnone%26kaDev%3Dchttp://qt.nokia.com/downloadshttp://www.packtpub.com/opencv-2-computer-vision-application-programming-cookbook/bookhttp://qt.nokia.com/http://www.laganiere.name/8/13/2019 OpenCV Install Guide
2/17
1/9/14 OpenCV 2 Cookbook
www.laganiere.name/opencvCookbook/chap1s1_2.shtml#3 2
We will call our project myHelloWorld; place it in the location of your choice
and click Next.
We c reate a Desktop applicat ion.
A default project is then created.
8/13/2019 OpenCV Install Guide
3/17
1/9/14 OpenCV 2 Cookbook
www.laganiere.name/opencvCookbook/chap1s1_2.shtml#3 3
This one does nothing except launching the concole. Click on the green arrowand you will see it.
3. Lets now write a true Hello World application. Simply copy and paste thefollowing C++ code over the preceding one.
#include
int main() {
std::cout > var;
return 1;
}
8/13/2019 OpenCV Install Guide
4/17
1/9/14 OpenCV 2 Cookbook
www.laganiere.name/opencvCookbook/chap1s1_2.shtml#3 4
And click again on the green arrow to run the new code.
If you obtain the result above, then everything is working well. Note that Qtautomatically created a directory called myHelloWorld-build-desktopwhere it
puts all the compiled and executable files. This way, the source code and thebinary files does not get mixed together. This is very useful, if you use a versioncontrol software such as Subversionin which you submit only the sourcedirectory.
4. Let's now proceed to OpenCV installation. To download the OpenCV library, justgo to the OpenCV official website at opencv.willowgarage.com. You'll find therethe current release version in a downloadable zip file or in a Windows install. Inthe case of version 2.3.1, a superpack installer is available:
http://opencv.willowgarage.com/http://www.laganiere.name/subversionTut8/13/2019 OpenCV Install Guide
5/17
1/9/14 OpenCV 2 Cookbook
www.laganiere.name/opencvCookbook/chap1s1_2.shtml#3 5
Run it and extract it to the directory of your choice:
Once this is done, you now have all OpenCV source files in the specifieddirectory.
5. The next step is to compile the library for the compiler you want to use; here itwill be the basic mingw/g++compiler that Qt installed by default. Just before we
do this, let's include the folder that c ontains the makecommand in our Path
environment variable. Indeed, compiling the library will be done using this make
utility command that Qt installed together with the compilers themselves. Theyare located in C:\QtSDK\mingw\bin. Goto to your Control Panel (from the Start
menu) and to the Systemmenu.
In the Advanced system settingsmenu, you select the Advancedtab.
8/13/2019 OpenCV Install Guide
6/17
1/9/14 OpenCV 2 Cookbook
www.laganiere.name/opencvCookbook/chap1s1_2.shtml#3 6
Click on Environment Variables...
In the User variablesbox, look for the PATHvariable. If it is there, click on
Edit..., if not click on New...to create it. This variable contains all the folder
Windows will look in when you type a command. By setting it in the uservariables, this definition is available to you only. If you want it to be valid for allusers of your system then define it in the System variablesbox.
8/13/2019 OpenCV Install Guide
7/17
1/9/14 OpenCV 2 Cookbook
www.laganiere.name/opencvCookbook/chap1s1_2.shtml#3 7
6. To build the library from the source f iles, OpenCV uses CMake, a cross-platformand open source tool designed to build library packages. We need to installCMake. Go to cmake.organd download the Windows Win32 Installer.
7. Once CMake installed, you can start the gui-based applicat ion (cmake-gui)
http://www.cmake.org/8/13/2019 OpenCV Install Guide
8/17
1/9/14 OpenCV 2 Cookbook
www.laganiere.name/opencvCookbook/chap1s1_2.shtml#3 8
In CMake, spec ify the directory containing the source code and the one that will
contain the builds.
Click on Configure. This will create the output directory.
8/13/2019 OpenCV Install Guide
9/17
1/9/14 OpenCV 2 Cookbook
www.laganiere.name/opencvCookbook/chap1s1_2.shtml#3 9
You then specify the c ompilers that will generate the project. In our case, theyare the compilers of MinGW installed by default by Qt.
These are gcc and g++.
CMake now displays the different build options.
8/13/2019 OpenCV Install Guide
10/17
1/9/14 OpenCV 2 Cookbook
www.laganiere.name/opencvCookbook/chap1s1_2.shtml#3 10
Select the build type, here Release. If you wish, at the end, you can repeat the
same process with the Debugmode.
8/13/2019 OpenCV Install Guide
11/17
1/9/14 OpenCV 2 Cookbook
www.laganiere.name/opencvCookbook/chap1s1_2.shtml#3 1
Since we want to use Qt, we also select the WITH_QToption
Once your options selected, you click on Configureagain.
8/13/2019 OpenCV Install Guide
12/17
1/9/14 OpenCV 2 Cookbook
www.laganiere.name/opencvCookbook/chap1s1_2.shtml#3 12
And you click on Generateto complete the installation.
8/13/2019 OpenCV Install Guide
13/17
1/9/14 OpenCV 2 Cookbook
www.laganiere.name/opencvCookbook/chap1s1_2.shtml#3 13
8. Now that you have completed the installation, you are ready to compile theOpenCV library. Start the Windows cmd console and go to the directory whereyou installed your builds. Type mingw32-make
Building everything will take time...
Once this built completed, you type mingw32-make install
This last step will install the library and the include files in the install
directory. Note that for clarity, you can rename this directory as releasesince
you ask CMake to build a Release install
8/13/2019 OpenCV Install Guide
14/17
1/9/14 OpenCV 2 Cookbook
www.laganiere.name/opencvCookbook/chap1s1_2.shtml#3 14
We are done with the installation! Congratulations!
9. Before we build our first OpenCV project, we need to add a few more folders tothe Pathenvironment variable. First, you need to tell your system where to find
the OpenCV dlls. From our installation process, they are in C:\OpenCV-
2.3.1\install\bin.
The Qt dlls are also required; you should find them at C:\QtSDK\QtCreator\bin
10. Our last step is to build a simple OpenCV project to make sure everything isworking properly. Start Qt and create a new Qt Console Application projectcalled here myFirstOpenCVProject. Goto to the projectsmenu and select the
Releasebuild configuration.
8/13/2019 OpenCV Install Guide
15/17
1/9/14 OpenCV 2 Cookbook
www.laganiere.name/opencvCookbook/chap1s1_2.shtml#3 15
Our test program will simply open and display an image:
#include
#include
int main() {
// read an image
cv::Mat image= cv::imread("img.jpg");
// create image window named "My Image"
cv::namedWindow("My Image");
// show the image on window
cv::imshow("My Image", image);
// wait key for 5000 ms
cv::waitKey(5000);
return 1;
}
The project file must specify the OpenCV headers and libraries locations:
QT += core
QT -= gui
TARGET = myFirstOpenCVProject
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
INCLUDEPATH += C:\\OpenCV-2.3.1\\install\\include
LIBS += -LC:\\OpenCV-2.3.1\\install\\lib \
-lopencv_core231.dll \
-lopencv_highgui231.dll \
-lopencv_imgproc231.dll \
-lopencv_features2d231.dll \
-lopencv_calib3d231.dll
You basically just have to add the last two definitions to the existing project:
8/13/2019 OpenCV Install Guide
16/17
1/9/14 OpenCV 2 Cookbook
www.laganiere.name/opencvCookbook/chap1s1_2.shtml#3 16
Make sure you have an image called img.jpgin your myFirstOpenCVProject-build-desktopdirectory that is the default directory when you run your project
from Qt.
and you should see the image displayed.
8/13/2019 OpenCV Install Guide
17/17
1/9/14 OpenCV 2 Cookbook
Wow! But this is just the beginning, you can do much more with OpenCV... Goodluck!
Top of the page
(c) Robert Laganiere 2011
http://www.googleadservices.com/pagead/aclk?sa=L&ai=Cq6b5TczOUrvxIcqlmQXaroHAD4mI7sYGqfm6h3zAjbcBEAEgtdapJVCmj536BWAzoAGH--faA8gBA6gDAcgDwQSqBJ0BT9D-IabRXB2vhXEEtHwCG5E32L6pKF2L6KfsFL9dxBpeHVIu3LXxms_HheR4Lhaq1IFasbTm4_497nIqgWdMbaskv0UGaItTjYzqEkbZIpvM_NyzwnlhlaihIJg8W9TVM0NGbjl2oBg2tv7rLnTdOrBrFONLVi1VI4vxe4iSEhWQExYXujyp4nthYOw2PE0nt_Ef816mV6deiE7EjYgGAaAGA4AH4YSYJQ&num=1&cid=5GiG24uYPt299ERrFWNY_kBK&sig=AOD64_0BA_ljUfEArgrtG8l-pn1tL8Cb5w&client=ca-pub-8501294875466025&adurl=http://www.softlayer.com/info/one-platform%3Futm_source%3Dgoogle%26utm_medium%3Dcpc%26utm_term%3D%26utm_content%3DWeb%252BHosting%26utm_campaign%3DPPC-SNG-Region-DISP%26placement%3Dwww.laganiere.namehttp://-/?-