[OpenCV Getting Started Tutorial 7] Playing with OpenCV source code: Generating OpenCV engineering solutions and compiling OpenCV source code[Copy link]
In this article, we discussed how to use the installed OpenCV to select different compiler types, generate highly restored OpenCV development solution engineering files, and appreciate the exquisite source code totaling more than 660,000 lines in the new version of OpenCV. We can recompile its source code to get binary files, or modify the original official OpenCV code and compile it for our own use, which is a solid step towards a deeper understanding of the open source charm of OpenCV.
1. Download and install cmake Download it from camke's official website: http://www.cmake.org/, first go to its download page. You can download the source code of cmake software from Source distributions on the download page. If you are interested in this open source software, you may want to take a look. http://img.blog.csdn.net/20140324173241984?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcG9lbV9xaWFubW8=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast Binary You can download the cmake executable file from distributions. We only need to download its executable file and select Windows (Win32 Installer) to download. Click here to download directly: http://www.cmake.org/files/v2.8/cmake-2.8.12.2-win32-x86.exe
This step is to download and install cmake. The download address is given. Everyone knows how to install it, so I won’t say much about it. After the installation is complete, if no desktop shortcut is generated, find cmake-gui.exe in the installation path, such as D:\Program Files(x86)\CMake 2.8\bin and run it.
In addition, a brief introduction to cmake:
CMake, short for "crossplatform make", is a cross-platform installation (compilation) tool that can use simple statements to describe the installation (compilation process) of all platforms. It can output various makefiles or project files, and can test the C++ features supported by the compiler, similar to automake under UNIX. The only difference is that the CMake configuration file is named CmakeLists.txt. Cmake does not directly build the final software, but generates standard build files (such as Unix Makefile or Windows Visual C++ projects/workspaces), which are then used in the usual way. This allows developers familiar with an integrated development environment (IDE) to build their software in a standard way. This ability to use the native build system of each platform is what distinguishes CMake from other similar systems such as SCons. :
2. Solution of using cmake to generate OpenCV source code project This step is the core content of the whole article. In order to explain clearly, we will introduce it step by step: <1>Run cmake-gui If no desktop shortcut is generated, in the installation path, such as D:\Program Files(x86)\CMake 2.8\bin to find cmake-gui.exe and run it. After running, you will get the following window: <2>Specify the installation path of OpenCV As shown below, click the “Browse Source” button in the red box and specify the storage path of the source code when OpenCV is installed in the pop-up dialog box. Take the current latest version of OpenCV 2.4.8 installed in D:\Program Files as an example, then select the path here: D:\Program Files\opencv\sources In fact, we can find that there must be a file named CMakeLists.txt under this path. This file is the configuration file left for cmake. cmake can generate different solutions based on this configuration file by selecting different compilers. The VisualStudio compiler generates the Visual Studio version of the sln solution. <3>Specify the storage path of the solution As shown below, click the "Browse Build" button in the red box, and specify the path where we store the generated opencv solution in the pop-up dialog box. For example, F:/opencv. <4> First Configure As shown in the figure, click the Configure button for the first time. http://img.blog.csdn.net/20140324174549015?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcG9lbV9xaWFubW8=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast[/img]
The following dialog box for compiler selection will pop up: First, select "Use default native compilers". Then, we can find that there are dozens of compilers available in the drop-down list for us to choose from. Because we have installed Visual Studio, the corresponding version of Visual Studio compiler will be selected by default, such as Visual Studio 10 (that is, the sln solution corresponding to VS2010 will be generated later) After confirmation, click the "Finish" button. So, cmake starts the first source code configuration process: http://img.blog.csdn.net/20140324174914906?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcG9lbV9xaWFubW8=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast During the cmake processing, there may be errors such as “Could not copy from: D:/Program Files(x86)/CMake”. 2.8/share/cmake 2.8/Templates/CMakeVSMacros2.vsmacros to: C:/Users/浅墨/Documents/VisualStudio 2010/Projects/VSMacros80/CMakeMacros/CMakeVSMacros2.vsmacros” is a red warning because the path name of the system user contains the Chinese character "浅墨". Cmake does not recognize Chinese paths, but as long as we set the generated path in the third step above to have no Chinese characters, it will be fine. Even if there is an error caused by the Chinese characters in C:/Users/, it will not affect our generation this time, so we don't need to worry about it.
PS: Regarding the Chinese system user name, this is a lesson learned the hard way. When I was playing the Unity3D game engine before, I was tortured by the Chinese system path for a long time. I got an error when I downloaded a plug-in from the Unity official store. Changing the user name in the control panel is only a temporary solution. In the end, I solved the problem by locating the Documents directory of the system user to another place. However, this will cause some software that has data stored in the Documents path to lose data, or even make it impossible to open it. ... So, it’s a lesson learned the hard way. ... Qianmo will never use a Chinese ID as the user name when reinstalling the system in the future.
When the progress bar reaches the end and the words Configuring done appear, the first source code configuration is completed, as shown in the figure <5>Second Configuration After the first configuration is completed, we need to perform the second configuration, so click the "Configure" button again. This configuration is very fast. After a few seconds, the words "Configuring done" will appear again, and the red selected parts are normal: <6>Click Generate and you’re done Now, you only need to click the Generate button once to generate the final solution: Because there have been two configure processes before, generating the solution will also be very fast: When you see the words Generating done, it means that the work is done and you can go to the F:\opencv that we specified before to find the solution we generated.
PS: The project generated by cmake for OpenCV 2.4.8 is only over 3 MB, compared to the previous OpenCV 2.4.6 version which has over 3 GB of projects. I thought it was generated wrong at first, but when I opened the sln project, I found that the source code could still be seen. It turns out that since version 2.4.7, the opencv source code has been directly included in the installation path of opencv. The sln project we generated is just connected to the source files in the installation path of opencv. The project itself is not large. III. Compile OpenCV source code With trepidation, we opened the newly generated "OpenCV.sln" solution and saw a huge project in front of us - this is a solution containing 67 projects: http://img.blog.csdn.net/20140324175415687?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcG9lbV9xaWFubW8=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast At this time, you can click on a project you are interested in in the solution resource management interface, such as the opencv_core project, and view the source code of a file in opencv_core/Src/matrix.cpp. This screenshot taken by Qianmo is the source code of a constructor of the Mat type that everyone is familiar with: After appreciating the source code for a while, we can press [F5] or use various other operations to start debugging Compilation process: Compilation result: The compilation takes about 5 minutes, and the specific time is closely related to the machine configuration. From the screenshot of Qianmo, we can see that 62 projects were compiled successfully and 0 failed, that is, there was no problem and all the compilations were successfully generated. :D In addition, the compilation will get the following warning: Don't be nervous, this is normal. Because the OpenCV2.4.8 source code project sets the "ALL_BUILD" project as the startup item by default, it will run by default after successful compilation. “ALL_BUILD” is a miscellaneous item generated by a project. It is not an exe and cannot be run by itself, so such a prompt will pop up. Regarding the specification of the startup item, we can go to the Solution Explorer, right-click on the project that needs to be set as the startup item, and click [Set as Startup Item (J)] in the pop-up menu to set it. As shown below: In fact, when we compile OpenCV, we get some binary generated files, such as DLL, Lib and exe. Because we compiled under debug, the dependency library of opencv 2.4.8 (more than 700M, a bit large) will be generated under bin\debug in the project directory, which can be used when we call opencv later. [size=32px ] 4. OpenCV code line count statistics [size= 14px] In the OpenCV 2.4 era, the source code src part has more than 170M, and the number of source code lines, According to Qianmo's statistics, there are more than 680,000 lines. (Here I can't help but sigh with emotion. The source code of Unreal Engine 4, which I just tinkered with a few days ago, is more than 1.44 million lines, more than twice that of OpenCV...) As you can see, the exact source code of OpenCV 2.4.8 has 687,870 lines. OK, the feast begins. The source code of OpenCV, the treasure house in the field of computer vision, is right in front of you. Let’s enjoy it. /font] Hmm , that’s about it for this section, see you in the next article:) This content was provided by EEWORLD forum user Rambo[/size ]Original, if you want to reprint or use it for commercial purposes, you must obtain the author's consent and indicate the source