Study OPENCV Research report the series (ii) The source code is compiled and comes with an example project

Source: Internet
Author: User

Download and install CMake3.0.1     

To compile the source code for OpenCV2.4.9 yourself. First of all. The compilation tool must be downloaded and the more compiled tools used are cmake.

The following excerpt is an introduction to CMake:

CMake is a cross-platform installation (compilation) tool that can be used to describe the installation of all platforms (the compilation process) with simple statements. He can output a variety of Makefile or project files, can test the compiler supports C features, similar to UNIX under the Automake. Only the CMake configuration file is named CmakeLists.txt. Cmake does not directly construct the software at last, but rather produces standard constructs such as Unix's Makefile or Windows Visual C projects/workspaces. Then it is used in the general construction mode.

This makes it possible for developers who are familiar with an integrated development environment (IDE) to build their software in a standard way. This ability to use the native building systems of each platform is the difference between CMake and scons and other similar systems.
CMake can compile the source code, make the library, generate the adapter (wrapper), and can construct the running file in random order. . CMake Support In-place Construction (binary and source in the same folder tree) and Out-of-place constructs (two files in other folders), so it is very easy to build multiple binaries from the same source folder tree.

CMake also supports the construction of static and dynamic libraries.
The name "CMake" is the abbreviation for "Cross Platform make".

Although the name contains "make", the common "make" system on CMake and UNIX is separate and more advanced.



We know from the above that we can use the CMake tool to compile the source code into a VS2010 project file, so that we can build multiple examples or tutorial project in the form of different project groups in vs.


We can find the CMake installation file for the latest version number on CMake's official website: http://www.cmake.org/cmake/resources/software.html.


watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvymvzdgdvbmdodwliaw4=/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma== /dissolve/70/gravity/southeast ">

Select File "cmake-3.0.1-win32-x86.exe" download, total 10.78MB, very fast to finish.

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvymvzdgdvbmdodwliaw4=/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma== /dissolve/70/gravity/southeast ">

Once the download is complete, double-click on the installer to start the CMake installation.


Installation is very easy, basically is " next ", until the final step " complete ."







watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvymvzdgdvbmdodwliaw4=/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma== /dissolve/70/gravity/southeast ">




watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvymvzdgdvbmdodwliaw4=/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma== /dissolve/70/gravity/southeast ">




After the installation is complete. The ability to find the shortcut to the CMake3.0.1 program already installed in the start menu "CMake (Cmake-gui)"

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvymvzdgdvbmdodwliaw4=/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma== /dissolve/70/gravity/southeast ">

After the installation is ready. The following starts the compilation of the OpenCV source code.

OpenCV2.4.9 Source code compilation

Here if we have downloaded and installed the source code of OpenCV2.4.9.

(If this step does not know how to do it.) Please see I also have a tutorial "learning OpenCV's Learning Notes series (a) environment configuration")

Open the CMake program by tappingCMake (Cmake-gui) in the Start menu, and the Compile Settings screen will pop up. For example, as seen in the


In the "whereis the source code" option in the text box, enter the path of the folder where the OpenCV sources are located, you can click "Browse Source ..." Select the folder, select the path according to the actual location where the source code is installed, and select "h:/opencv/sources" here.


In the text box of thewhere to build the binariesoption, enter the file folder path where you need to build the example project and source code, where you select "H:/opencv/solution" under the same folder as the source code.

After entering the source code path and the build path, click the "Configure" button in the lower-left corner of the compilation Settings screen

The assumption is the first time configuration. This will bring up an interface that chooses to generate the project target environment, where you can select the "Visual Studio" option and tick "usedefault native compilers". Then click on "Finish" button.

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvymvzdgdvbmdodwliaw4=/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma== /dissolve/70/gravity/southeast ">

Assume that the build destination folder path does not exist. Then there will be a dialog box that prompts you to create a new folder, as you can see by clicking "Yes" directly.


The first configuration will have very many red options, leave it alone. Click the "Configure" button again.


This time, the red option is gone, which means there's no problem. Configuration is complete.

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvymvzdgdvbmdodwliaw4=/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma== /dissolve/70/gravity/southeast ">

Click "Generate" button

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvymvzdgdvbmdodwliaw4=/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma== /dissolve/70/gravity/southeast ">

After seeing the words "generatingdone" at the bottom. Description source code project compilation completed.

OpenCV2.4.9 Source code Project test      

Open the directory under the path of the build directory in the previous step, there is a "solution" directory, which is generated in the previous step, and double-click the directory.


Under the "h:/opencv/solution" path. We can see that there is a vs2010project file "OpenCV.sln" that has been compiled and created, double-click the file. You can open the project file in VS2010.

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvymvzdgdvbmdodwliaw4=/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma== /dissolve/70/gravity/southeast ">

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvymvzdgdvbmdodwliaw4=/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma== /dissolve/70/gravity/southeast ">

In the Open "OpenCV" project, we can see that there are 68 items.


Right-click on the OpenCV solution and choose Build Solution.


Compile longer. My PC configuration is relatively good, but the compilation takes about 2 minutes. Build 62 project, fail 0, explain all compile pass.

Congratulations on yourself!


Next, you'll be able to view each project individually. Compiled. An explanation of these examples of project and source code might be added:

OpenCV2.4.9 with example source code compilation and testing     

The above is the OPENCV source code compilation and project, the following describes how to compile the OpenCV to bring the example source and project.

Revisiting open CMake3.0.1, change the source code path to: "h:/opencv/sources/samples" (The detailed path varies with the installation location.) The "Sample" folder can be found under the Source Code installation folder of OpenCV.





watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvymvzdgdvbmdodwliaw4=/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma== /dissolve/70/gravity/southeast ">


After the one-pass configuration (Configure) and the build (Generate) (Configuration item above. Do not repeat here!

), under the destination folder. Be able to find examples of OPENCV that have been generated. Project file: "Samples.sln"


Click Open "Samples.sln" To see the Solution Explorer showing a common 172 items (example really much, good look ha!)


Right click on resolve and select " Build Solution "


About a minute or so (different machine configuration, compile time difference). After compiling, project builds 171 project, fails 0, and compiles one at a time. A little.

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvymvzdgdvbmdodwliaw4=/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma== /dissolve/70/gravity/southeast ">

After the overall compilation is correct, open an example project, this article selects "(EXAMPLE) Lkdemo" project. This project source code uses an improved Lucas-kanade optical flow algorithm for detecting video motion targets. Use the mouse to click on the target point, video tracking.

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvymvzdgdvbmdodwliaw4=/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma== /dissolve/70/gravity/southeast ">

Click Execute. Found a command-line interface flashed past. And then quit, why is that?

The "if (!cap.isopened ())" in the "lkdemo.cpp" file on the 56 line by stepping through the debug discovery; In the inference statement, the player is not open, and a line "Could not initialize capturing..." is displayed directly on the console. Then withdrew.

Analyze the cause. is due to "Cap.open (" Megamind.avi "); The video file "Megamind.avi" In this statement is not under the program folder. Know why. It's a good conquest.

Copy the "Megamind.avi" file to "H:\opencv\" under the "H:\opencv\sources\samples\cpp" folder (source code file folder) Samplesolution\cpp\debugfolder (this is the folder where the program "Cpp-example-lkdemo.exe" is located).

In other words, make sure that you have a "megamind.avi" file under the folder where the program executes .


watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvymvzdgdvbmdodwliaw4=/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma== /dissolve/70/gravity/southeast ">


After finishing the above work. Click Execute again, the successful execution, playing is a cartoon, in the screen after the left click, there will be a green mark point, the location of the mark Point will change with the movement of the surrounding objects, this should be called video tracking.

For example, the following:


watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvymvzdgdvbmdodwliaw4=/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma== /dissolve/70/gravity/southeast ">

OK, this is the principle and workflow I do not introduce, I look at the source code, other examples of project code can be seen one by one, and then compile and execute. Believe that these examples are familiar with understanding, OPENCV will not be unfamiliar to you. You are welcome to enter the hall of OpenCV. Start your learning phase!



Study OPENCV Research report the series (ii) The source code is compiled and comes with an example project

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.