60

How do you install and use OpenCV 2.4.3 under VC++ 2010 Express?

flowfree
  • 16,356
  • 12
  • 52
  • 76
  • 4
    @karlphillip [It’s OK to Ask and Answer Your Own Questions](http://blog.stackoverflow.com/2011/07/its-ok-to-ask-and-answer-your-own-questions/) – flowfree Jun 05 '12 at 18:49
  • 2
    I don't see the self-promotion/rep-farming aspect at all (except for the fact that everything we do here is in some way self-promotion). But as good as these look, tutorials are not a good fit on Stack Overflow - see Meta discussion at http://meta.stackexchange.com/questions/134645/is-stackoverflow-a-central-store-for-tutorials – Pekka Jun 05 '12 at 19:34

1 Answers1

131

1. Installing OpenCV 2.4.3

First, get OpenCV 2.4.3 from sourceforge.net. Its a self-extracting so just double click to start the installation. Install it in a directory, say C:\.

OpenCV self-extractor

Wait until all files get extracted. It will create a new directory C:\opencv which contains OpenCV header files, libraries, code samples, etc.

Now you need to add the directory C:\opencv\build\x86\vc10\bin to your system PATH. This directory contains OpenCV DLLs required for running your code.

Open Control PanelSystemAdvanced system settingsAdvanced Tab → Environment variables...

enter image description here

On the System Variables section, select Path (1), Edit (2), and type C:\opencv\build\x86\vc10\bin; (3), then click Ok.

On some computers, you may need to restart your computer for the system to recognize the environment path variables.

This will completes the OpenCV 2.4.3 installation on your computer.


2. Create a new project and set up Visual C++

Open Visual C++ and select FileNewProject...Visual C++Empty Project. Give a name for your project (e.g: cvtest) and set the project location (e.g: c:\projects).

New project dialog

Click Ok. Visual C++ will create an empty project.

VC++ empty project

Make sure that "Debug" is selected in the solution configuration combobox. Right-click cvtest and select PropertiesVC++ Directories.

Project property dialog

Select Include Directories to add a new entry and type C:\opencv\build\include.

Include directories dialog

Click Ok to close the dialog.

Back to the Property dialog, select Library Directories to add a new entry and type C:\opencv\build\x86\vc10\lib.

Library directories dialog

Click Ok to close the dialog.

Back to the property dialog, select LinkerInputAdditional Dependencies to add new entries. On the popup dialog, type the files below:

opencv_calib3d243d.lib
opencv_contrib243d.lib
opencv_core243d.lib
opencv_features2d243d.lib
opencv_flann243d.lib
opencv_gpu243d.lib
opencv_haartraining_engined.lib
opencv_highgui243d.lib
opencv_imgproc243d.lib
opencv_legacy243d.lib
opencv_ml243d.lib
opencv_nonfree243d.lib
opencv_objdetect243d.lib
opencv_photo243d.lib
opencv_stitching243d.lib
opencv_ts243d.lib
opencv_video243d.lib
opencv_videostab243d.lib

Note that the filenames end with "d" (for "debug"). Also note that if you have installed another version of OpenCV (say 2.4.9) these filenames will end with 249d instead of 243d (opencv_core249d.lib..etc).

enter image description here

Click Ok to close the dialog. Click Ok on the project properties dialog to save all settings.

NOTE:

These steps will configure Visual C++ for the "Debug" solution. For "Release" solution (optional), you need to repeat adding the OpenCV directories and in Additional Dependencies section, use:

opencv_core243.lib
opencv_imgproc243.lib
...

instead of:

opencv_core243d.lib
opencv_imgproc243d.lib
...

You've done setting up Visual C++, now is the time to write the real code. Right click your project and select AddNew Item...Visual C++C++ File.

Add new source file

Name your file (e.g: loadimg.cpp) and click Ok. Type the code below in the editor:

#include <opencv2/highgui/highgui.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main()
{
    Mat im = imread("c:/full/path/to/lena.jpg");
    if (im.empty()) 
    {
        cout << "Cannot load image!" << endl;
        return -1;
    }
    imshow("Image", im);
    waitKey(0);
}

The code above will load c:\full\path\to\lena.jpg and display the image. You can use any image you like, just make sure the path to the image is correct.

Type F5 to compile the code, and it will display the image in a nice window.

First OpenCV program

And that is your first OpenCV program!


3. Where to go from here?

Now that your OpenCV environment is ready, what's next?

  1. Go to the samples dir → c:\opencv\samples\cpp.
  2. Read and compile some code.
  3. Write your own code.
MohamedEzz
  • 2,830
  • 3
  • 20
  • 26
flowfree
  • 16,356
  • 12
  • 52
  • 76
  • 2
    Again the same problem, "opencv_core240d.dll is missing from the computer". What to do? – Abid Rahman K Jun 05 '12 at 18:06
  • 2
    It looks like you haven't add `[OPENCV_DIR]\build\x86\mingw\bin` to your path. – flowfree Jun 05 '12 at 18:53
  • 5
    +1 By far the best instructions on this that I have come across. Wish I +1 more than once! Thanks – volting Sep 15 '12 at 22:10
  • Thank you a lot!! The best instructions available in the WHOLE INTERNET!! Thanks again!!!!! – PeakGen Nov 30 '12 at 17:50
  • you are missing include in properties for c++ opencv/build/include, opencv/build/include/opencv and opencv/build/include/opencv2 – lbennet Mar 20 '13 at 19:31
  • Hi, I need to create a window application in visual studio along with OpenCV. Is it possible..? – Haris May 04 '13 at 12:11
  • 1
    @Harris: Anything's possible especially while coding. Programming is like magic - like Harry Potter's world come alive, where the world is governed by laws made by you. –  Nov 09 '13 at 19:21
  • You may have to modify the names of the libraries - e.g. I had to change all the files ending in 243d to 247d. Also, it looked like one library is no longer used - I got the error in the debug and just removed the missing file. – Dave Hilditch Nov 15 '13 at 17:08
  • I did everything according to your tutorial nice explanations by the way thank you... but I cannot read an image.. It does not give any error but it returns -1 and cannot open image evethough I give the right path – Ege Dec 20 '13 at 09:13
  • 1
    @AbidRahmanK I got the same error , what to do for it ? –  Jan 07 '14 at 20:40
  • @Flying: I am sorry I forgot what i did. I think the solution is somewhere in stackoverflow, don't know where. Try this answer: http://stackoverflow.com/a/7014918/1134940 – Abid Rahman K Jan 08 '14 at 06:54
  • This is, indeed, the BEST answer in the web. however I still cannot get rid of the `The program can't start because opencv_core248d.dll is missing` error.. – jeff Mar 02 '14 at 23:15
  • 1
    To solve the missing .dll's, I only had to restart Visual Studio so it sees the update PATH env variable. – MohamedEzz Jun 15 '14 at 16:50
  • 1
    why can't the opencv.org site have a tutorial like this one? Took me all of 5 minutes to follow this and get it to work. – hokiebird Oct 11 '14 at 04:11
  • 1
    @bsdnoobz: This worked perfectly, thanks. Do you always do this with every new project, or do you have some automated way of creating projects like this with the includes etc pre-set? – Zoran Pavlovic Oct 11 '14 at 07:39
  • Followed your tutorial, but when i press F5 for a debug run. I get an acces denied in vs2010, also when running it directly from command line, any idea what foes wrong here ?. (i have no spaces in path file, or project, or opencv) – user613326 Mar 19 '15 at 10:17
  • opencv 3.0.0 has no mingw inside ×86 folder moreover there is no libopencv_core300.dll.a , what to do – udit043 Jun 29 '15 at 08:25
  • 1
    There is no 64 bit version Visual Studio. And everbody who use Visual studio must add libraries as x86 bit. My PC has 64 bit and I tried to add 64 bit libaries and environment variable then I get errors. – Halil İbrahim Oymacı Mar 03 '16 at 22:42
  • Like MohamedEzz -> To solve the missing .dll's, I only had to restart Visual Studio so it sees the update PATH env variable. – Diizzy Aug 04 '16 at 08:05
  • For anyone trying to use this, this appears to NOT work in Visual Studio 2015 Community. That said, I'd love some one to prove me wrong. – chessofnerd Aug 13 '16 at 04:54
  • Also not working for me in VS2015 with OpenCV v3.2.0. My OpenCV install seems to be missing a lot of the suggested .lib files – Andy P Mar 23 '17 at 14:37
  • Further to my comment above, I have found a solution that at least gets this sample working (not sure about more OpenCV features). When setting up the VC++ Directories, linker dependancies and the solution build option I had to choose x64. In place of the large list of .lib files, I only needed opencv_world320d.lib – Andy P Mar 23 '17 at 14:47