OpenCV learning notes (1) -- OpenCV Development Environment Configuration

Source: Internet
Author: User
Tags visual studio 2010

OpenCV learning notes (1) -- OpenCV Development Environment Configuration

Abstract:

Due to the recent popularity of AR (Augmented Reality), various AR-based applications and games are gradually oriented to the public. The two most important technologies in AR are tracking, recognition, and enhanced rendering, tracking and recognition is implemented through the open-source computer vision library OpenCV, so I want to study this library. Here is my study notes, not an authoritative tutorial, if you have any errors, please point them out for me.

========================================================== ================================================== ======================

 

Preface:

What is OpenCV? Some people may not know, simply put, OpenCV -- Open Source Computer Vision Library, which is developed based on C language and some C ++ languages, it can be used for computer vision, image processing, pattern recognition, and tracking.

 

I. preparations:

1. Download The OpenCV installation package:

To the official website of OpenCV (http://opencv.org/) download the latest version of OpenCV installation package, because OpenCV for different platforms have installed programs, so we only need to select the appropriate platform version according to the current development environment, here we are developing in the win7 64bit operating system, so we should download OpenCV for Windows:

We selected the latest version 3.1, and after the download, we get its installation file: opencv-3.1.0.exe

2. Install Visual Studio 2015

 

Ii. installation and configuration:

 

1. installation:

Double-click to download the opencv-3.1.0.exe file, install it (actually decompress it), and select the installation directory:

After the installation is complete, you can see two folders in the installation directory: build and sources. build is the library files used by OpenCV, while sources is the demo source code provided by OpenCV for us:

 

2. Environment Variable Configuration:

The above steps only completed the installation, but to be able to use OpenCV for development, we also need to configure the environment variables, my computer-> properties-> advanced system settings-> environment variables, find the Path variable, select and click Edit:

For 64-bit systems, add "; opencv installation path \ build \ x64 \ vc14 \ bin" (note: ";" in the English input method is a delimiter, used to separate the configuration parameters of other software). For example, the configuration parameters of my system should be:; E: \ OpenCV \ opencv \ build \ x64 \ vc14 \ bin. In the previous opencv version, the 32-bit system should configure the path "; opencv installation path \ build \ x86 \ vc14 \ bin ", however, OpenCV of version 3.1.0 has deleted the x86 Directory, which means that 32-bit OpenCV compilation cannot be performed in the VS 2015 environment.

Note that the x64 folder is divided into two folders: vc12 and vc14, which correspond to the VS version. For example, vc2012 should be used for VS2013, and here we use VS 2015, therefore, use the vc14 file directory:

Supplement:Vc8 = Visual Studio 2005, vc9 = Visual Studio 2008, vc10 = Visual Studio 2010, vc11 = Visual Studio 2012, vc12 = Visual Studio 2013, vc14 = Visual Studio 2015

 

Note: After the environment variable is configured, You need to restart the computer to take effect!

 

3. VS 2015 Configuration:

We know that to use an external class library in VS, we need to introduce the following configuration: The External library directory and the external include file.

Open VS 2015 and create a blank win32 console project:

Select the console project in the C ++ and Win32 languages, and set the Project name:

Specify the created Project as a blank project:

After the project is created, select the ResourceFiles directory (that is, the source code directory) in the solution bar and create a new C ++ source code file:

Set the source code file name, for example, Main, and then create the source code file to the project:

In VS, there is a tool called "Property Manager", which is used to configure the overall parameters of VS. After the configuration is performed once, all new projects can be applied to change the configuration, and no one-to-one configuration operation is required, it is also very convenient to use. Click View> Other Windows> Property Manger in the toolbar to open the Property manager:

In the new property manager column, expand the directory, select Microsoft. Cpp. x64.user in Debug | Win64, and right-click Properties to go to the Properties page:

1) configure the inclusion directory:

In Common Properties-> VC ++ directory-> include directory, click the triangle icon on the right to select Edit to Edit:

Add the following three paths to it:

E: \ OpenCV \ opencv \ build \ include

E: \ OpenCV \ opencv \ build \ include \ opencv

E: \ OpenCV \ opencv \ build \ include \ opencv2

2) configure the library file directory:

After the directory configuration is complete, you need to configure the Library file: Return to the properties page and select the Library Directories (Library file directory) under the directory ):

Add the library file directory of OpenCV to the library file directory: E: \ OpenCV \ opencv \ build \ x64 \ vc14 \ lib, this directory is selected based on the directory selected by everyone in step 1 to install OpenCV:

3) configure the Dynamic Link Library:

We can check the directory of the library file added in step 2. in the lib file list, we found that only two library files are left in OpenCV 3.1.0, namely opencv_world310.lib and opencv_world310d.lib. The difference between the two library files is that opencv_world310.lib is the Release mode version, opencv_world310d.lib is the Debug mode version:

Similar to the preceding two steps, choose Linker> Input> Additional Dependencies on the properties page ):

Add one of the two library files we just saw in the OpenCV library file directory here (depending on the mode requirement, the Release mode or the Debug mode ):

In fact, for the new version of OpenCV that has been released and will be released in the future, you only need to check the Libraries under opencv \ build \ x86 \ vc10 \ lib and add them as dependencies.

 

Iii. Practice:

After completing all the above configurations, we need to perform a test to verify whether the above configurations are successful. Here, the demo function is very simple, that is, to display a specified image in a window. Add the following code to the Main. cpp file:

 

# Include
 
  
# Include
  
   
# Include
   
    
Using namespace cv; int main () {// read an image (original game image) Mat img = imread ("pic.jpg "); // create a window named "Game Original painting" namedWindow ("Game Original painting"); // display the game original painting imshow ("Game Original painting", img) in the window ); // wait 6000 ms before the window closes waitKey (6000 );}
   
  
 
Copy pic.jpg to the project directory, which is in the same directory as the source code:

 


Run the project. If everything is normal, the following results will appear:

 

4. Possible problems:

1. When the program is run and the compilation fails, an error is reported:

The cause of this error is: we have said that we cannot compile 32-bit OpenCV in VS 2015, and our entire configuration process is for 64-bit systems, so how can we make VS 2015 run 64 debugging mode? In fact, it is very simple. Just switch it in the toolbar:

2. Unable to load the image. The system prompts that the pointer is out of position:

This problem may occur for two reasons:

First, the image suffix is incorrect or the directory storage location is incorrect;

Second, in the dynamic library configuration in the property configuration, the configuration of the two library files with and without d does not match the current debugging mode. You can directly select the current project in the solution, right-click the attribute panel and modify Additional Dependencies in its Linker:

 

Related Article

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.