Win7 安装 OpenCV2.3.1 到VS2010

Getting Started with OpenCV 2.3.1 in Microsoft Visual Studio 2010 in Windows 7 64-bit


I was trying to install OpenCV 2.3.1 on my Windows 7 64-bit machine and i kept getting the error: The application was unable to start correctly (0xc000007b)

I tried chasing the problem down using freely available program Dependency Walker and it looked like my executable project was x86 whereas all the dll files it was loading up were x64. So after many tries including re-building OpenCV using CMake, changing dll links to x86, etc., i finally figured out how to solve this problem, which i have presented as a tutorial below.

This tutorial assumes that you have installed Microsoft Visual Studio 2010 Professional.  If you are a student you can get it for free athttps://www.dreamspark.com/Products/Product.aspx?ProductId=25.

This tutorial contains the following:

  • How to download and install the OpenCV library
  • How to manually change the system path to include the bin file
  • How to create a project
  • How to configure Visual Studio to use OpenCV
  • A sample program that displays a video from a camera

Downloading and Installing OpenCV

OpenCV is a computer vision library written mainly in C that focuses on real time video processing.  It is open source or free to the public.

  1. Go to http://sourceforge.net/projects/opencvlibrary/files/opencv-win/2.3.1/
  2. Select the OpenCV-2.3.1-win-superpack.exe file and download it.
  3. Go the folder where you downloaded the executable file in Step 2 and select “Run as Administrator”.
  4. The executable file is essentially an archive of files with an extractor built in. It will ask you to select the location where you would like it to extract all its contents. Select: C:\Program Files\ as the path and click Extract. It will create a folder called OpenCV2.3.1 with the path: C:\Program Files\OpenCV2.3.1

Manually Changing the System Path to Include the Bin File

  1. To access the system path in Vista go to Control Panel\System and Security\System and on the left hand side selectAdvanced system settings this should bring up the Systems Properties dialogue.  Click on theAdvanced tab and then the Environment Variables button.
  2. This will bring up the Environment Variables dialogue.  In the System Variables box selectPathand then Edit
  3. When modifying the path know that it is sensitive to all characters including spaces.  To add the new bin, type a semicolon directly after the text that is there without adding in a space.  Next add the path to the bin file.  The path is the same as where you chose to install OpenCV back in step 4 of Downloading and Installing OpenCVsection plus \bin. For example if you chose to install OpenCV in:

    C:\Program Files\OpenCV2.3.1

    For 64 bit Windows, add the following to the system path:

    ;C:\Program Files\OpenCV2.3.1\build\bin\;C:\Program Files\OpenCV2.3.1\build\x64\vc10\bin\

    Make sure to restart your computer so the changes can take effect.

Creating a Project

  1. Click FileNewProject
  2. In the “Installed Templates”  box choose Visual C++Win32
  3. On the right choose Win32 Console Application
  4. Enter a name for the project and click OK
  5. Click finish in the dialogue box which appears

Configuring Visual Studio

  1. Click ProjectProperties to access the properties dialog
  2. In the left box click on Configuration Properties and on the top right click onConfiguration Manager

  3. In the Configuration Manager dialog box that opens up, under Active Solution Platformcombo box selectNew.
  4. In the New Solution Platform dialog box that opens up, under Type or select the new platform, selectx64,copy settings from Win32 and make sure that Create new project platformsis selected. ClickOK.
  5. You will notice that the in the Configuration Manager dialog box x64 platform has now been selected. Click Close.
  6. In the left box choose Configuration Properties C++ General
  7. In the right box, next to Additional Include Directories type the following text:
    C:\Program Files\OpenCV2.3.1\build\include;C:\Program Files\OpenCV2.3.1\build\include\opencv;%(AdditionalIncludeDirectories)

    IMPORTANT note that all these paths assume that you installed in the default location, if you installed in a different location; for example Program Files (x86) instead of Program Files, make sure you change these paths to reflect that.

  8. Next in the felt box choose Configuration Properties → Linker → Input
  9. In the right box, next to Additional Dependencies type the following text:
    "C:\Program Files\OpenCV2.3.1\build\x64\vc10\lib\opencv_core231d.lib";"C:\Program Files\OpenCV2.3.1\build\x64\vc10\lib\opencv_highgui231d.lib";"C:\Program Files\OpenCV2.3.1\build\x64\vc10\lib\opencv_video231d.lib";"C:\Program Files\OpenCV2.3.1\build\x64\vc10\lib\opencv_ml231d.lib";"C:\Program Files\OpenCV2.3.1\build\x64\vc10\lib\opencv_legacy231d.lib";"C:\Program Files\OpenCV2.3.1\build\x64\vc10\lib\opencv_imgproc231d.lib";%(AdditionalDependencies)
  10. IMPORTANT note that all these paths assume that you installed in the default location, if you installed in a different location; for example Program Files (x86) instead of Program Files, make sure you change these paths to reflect that.

  11. Click Apply then OK

Sample Program

This program will capture video from a camera and display it.  Press escape to stop capturing and displaying frames.

It may be necessary to change the number in this line of code to select the camera source:

1 CvCapture* capture = cvCaptureFromCAM(1);// capture from video device #1

If you do not know the video device number just try 0, then 1,  2 or else -1. Copy and paste the following code:

01 #include "stdafx.h"
02 #include <highgui.h>
03  
04 int _tmain(intargc, _TCHAR* argv[])
05 {
06     intc;
07     // allocate memory for an image
08     IplImage *img;
09     // capture from video device #1
10     CvCapture* capture = cvCaptureFromCAM(1);
11     // create a window to display the images
12     cvNamedWindow("mainWin", CV_WINDOW_AUTOSIZE);
13     // position the window
14     cvMoveWindow("mainWin", 5, 5);
15     while(1)
16     {
17         // retrieve the captured frame
18         img=cvQueryFrame(capture);
19         // show the image in the window
20         cvShowImage("mainWin", img );
21         // wait 10 ms for a key to be pressed
22         c=cvWaitKey(10);
23         // escape key terminates program
24         if(c == 27)
25         break;
26     }
27  return0;
28 }

If when you debugging the program, “Cannot find or open the PDB file” appears, try:

1.Open property page of the current project.

2 Choose "chracter set", change it to "Use multi-byte chracter set".

Win7 安装 OpenCV2.3.1 到VS2010_第1张图片

你可能感兴趣的:(Win7 安装 OpenCV2.3.1 到VS2010)