Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
OpenCV 2 Cookbook
2. Manipulating the Pixels 3. Processing Images with Classes 4. Counting the Pixels with Histograms 5. Transforming images with morphological operations 6. Filtering the Images 7. Extracting Lines, Contours and Components 8. Detecting and Matching Interest Points 9. Estimating Projective Relations in Images 10. Processing Video Sequences
2. Lets start by creating a simple Hello World application on the Windows console, just to make sure everyting is working properly. Click on C r e a t eP r o j e c t . . . and choose Q tC o n s o l eA p p l i c a t i o n .
www.laganiere.name/opencvCookbook/chap1s1_2.shtml#3
1/17
1/9/14
OpenCV 2 Cookbook
We will call our project m y H e l l o W o r l d ; place it in the location of your choice and click N e x t .
www.laganiere.name/opencvCookbook/chap1s1_2.shtml#3
2/17
1/9/14
OpenCV 2 Cookbook
This one does nothing except launching the concole. Click on the green arrow and you will see it.
3. Lets now write a true Hello World application. Simply copy and paste the following C++ code over the preceding one. # i n c l u d e< i o s t r e a m > i n tm a i n ( ){ s t d : : c o u t< <" H e l l om yw o r l d ! " ; c h a rv a r ; s t d : : c i n> >v a r ; r e t u r n1 ; }
www.laganiere.name/opencvCookbook/chap1s1_2.shtml#3
3/17
1/9/14
OpenCV 2 Cookbook
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 Qt automatically created a directory called m y H e l l o W o r l d b u i l d d e s k t o pwhere it puts all the compiled and executable files. This way, the source code and the binary files does not get mixed together. This is very useful, if you use a version control software such as Subversion in which you submit only the source directory. 4. Let's now proceed to OpenCV installation. To download the OpenCV library, just go to the OpenCV official website at opencv.willowgarage.com. You'll find there the current release version in a downloadable zip file or in a Windows install. In the case of version 2.3.1, a superpack installer is available:
www.laganiere.name/opencvCookbook/chap1s1_2.shtml#3
4/17
1/9/14
OpenCV 2 Cookbook
Once this is done, you now have all OpenCV source files in the specified directory. 5. The next step is to compile the library for the compiler you want to use; here it will be the basic m i n g w / g + +compiler that Qt installed by default. Just before we do this, let's include the folder that contains the m a k ecommand in our P a t h environment variable. Indeed, compiling the library will be done using this m a k e utility command that Qt installed together with the compilers themselves. They are located in C : \ Q t S D K \ m i n g w \ b i n . Goto to your Control Panel (from the S t a r t menu) and to the S y s t e mmenu.
www.laganiere.name/opencvCookbook/chap1s1_2.shtml#3
5/17
1/9/14
OpenCV 2 Cookbook
Click on E n v i r o n m e n tV a r i a b l e s . . .
In the U s e rv a r i a b l e sbox, look for the P A T Hvariable. If it is there, click on E d i t . . . , if not click on N e w . . .to create it. This variable contains all the folder Windows will look in when you type a command. By setting it in the user variables, this definition is available to you only. If you want it to be valid for all users of your system then define it in the S y s t e mv a r i a b l e sbox.
www.laganiere.name/opencvCookbook/chap1s1_2.shtml#3
6/17
1/9/14
OpenCV 2 Cookbook
6. To build the library from the source files, OpenCV uses CMake, a cross-platform and open source tool designed to build library packages. We need to install CMake. Go to cmake.org and download the Windows Win32 Installer.
www.laganiere.name/opencvCookbook/chap1s1_2.shtml#3
7/17
1/9/14
OpenCV 2 Cookbook
In CMake, specify the directory containing the source code and the one that will contain the builds.
www.laganiere.name/opencvCookbook/chap1s1_2.shtml#3
8/17
1/9/14
OpenCV 2 Cookbook
You then specify the compilers that will generate the project. In our case, they are the compilers of MinGW installed by default by Qt.
www.laganiere.name/opencvCookbook/chap1s1_2.shtml#3
9/17
1/9/14
OpenCV 2 Cookbook
Select the build type, here R e l e a s e . If you wish, at the end, you can repeat the same process with the D e b u gmode.
www.laganiere.name/opencvCookbook/chap1s1_2.shtml#3
10/17
1/9/14
OpenCV 2 Cookbook
Since we want to use Qt, we also select the W I T H _ Q Toption
www.laganiere.name/opencvCookbook/chap1s1_2.shtml#3
11/17
1/9/14
OpenCV 2 Cookbook
www.laganiere.name/opencvCookbook/chap1s1_2.shtml#3
12/17
1/9/14
OpenCV 2 Cookbook
8. Now that you have completed the installation, you are ready to compile the OpenCV library. Start the Windows cmd console and go to the directory where you installed your builds. Type m i n g w 3 2 m a k e
This last step will install the library and the include files in the i n s t a l l directory. Note that for clarity, you can rename this directory as r e l e a s esince you ask CMake to build a Release install
www.laganiere.name/opencvCookbook/chap1s1_2.shtml#3
13/17
1/9/14
OpenCV 2 Cookbook
We are done with the installation! Congratulations! 9. Before we build our first OpenCV project, we need to add a few more folders to the P a t henvironment variable. First, you need to tell your system where to find the OpenCV dlls. From our installation process, they are in C : \ O p e n C V 2 . 3 . 1 \ i n s t a l l \ b i n .
10. Our last step is to build a simple OpenCV project to make sure everything is working properly. Start Qt and create a new Qt Console Application project called here m y F i r s t O p e n C V P r o j e c t . Goto to the p r o j e c t smenu and select the R e l e a s ebuild configuration.
www.laganiere.name/opencvCookbook/chap1s1_2.shtml#3
14/17
1/9/14
OpenCV 2 Cookbook
Our test program will simply open and display an image: # i n c l u d e< o p e n c v 2 / c o r e / c o r e . h p p > # i n c l u d e< o p e n c v 2 / h i g h g u i / h i g h g u i . h p p > i n tm a i n ( ){ / /r e a da ni m a g e c v : : M a ti m a g e =c v : : i m r e a d ( " i m g . j p g " ) ; / /c r e a t ei m a g ew i n d o wn a m e d" M yI m a g e " c v : : n a m e d W i n d o w ( " M yI m a g e " ) ; / /s h o wt h ei m a g eo nw i n d o w c v : : i m s h o w ( " M yI m a g e " ,i m a g e ) ; / /w a i tk e yf o r5 0 0 0m s c v : : w a i t K e y ( 5 0 0 0 ) ; r e t u r n1 ; } The project file must specify the OpenCV headers and libraries locations: Q T + =c o r e Q T =g u i T A R G E T=m y F i r s t O p e n C V P r o j e c t C O N F I G + =c o n s o l e C O N F I G =a p p _ b u n d l e T E M P L A T E=a p p S O U R C E S+ =m a i n . c p p I N C L U D E P A T H+ =C : \ \ O p e n C V 2 . 3 . 1 \ \ i n s t a l l \ \ i n c l u d e L I B S+ =L C : \ \ O p e n C V 2 . 3 . 1 \ \ i n s t a l l \ \ l i b\ l o p e n c v _ c o r e 2 3 1 . d l l\ l o p e n c v _ h i g h g u i 2 3 1 . d l l\ l o p e n c v _ i m g p r o c 2 3 1 . d l l\ l o p e n c v _ f e a t u r e s 2 d 2 3 1 . d l l\ l o p e n c v _ c a l i b 3 d 2 3 1 . d l l You basically just have to add the last two definitions to the existing project:
www.laganiere.name/opencvCookbook/chap1s1_2.shtml#3
15/17
1/9/14
OpenCV 2 Cookbook
Make sure you have an image called i m g . j p gin your m y F i r s t O p e n C V P r o j e c t b u i l d d e s k t o pdirectory that is the default directory when you run your project from Qt.
www.laganiere.name/opencvCookbook/chap1s1_2.shtml#3
16/17
1/9/14
OpenCV 2 Cookbook
Wow! But this is just the beginning, you can do much more with OpenCV... Good luck!
www.laganiere.name/opencvCookbook/chap1s1_2.shtml#3
17/17