0% found this document useful (0 votes)
20 views6 pages

Setting Up OpenCV in Visual C++

This document provides a tutorial for setting up OpenCV in Microsoft Visual C++. It describes setting the path environment variable to locate the OpenCV binaries, configuring Visual C++ project settings to include OpenCV headers and libraries, and provides a simple "Hello World" example to test the installation. Key steps include adding OpenCV binary and include directories to the path and to Visual C++ settings, and linking additional dependencies for OpenCV libraries. The example displays an image to verify the OpenCV installation is working properly.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views6 pages

Setting Up OpenCV in Visual C++

This document provides a tutorial for setting up OpenCV in Microsoft Visual C++. It describes setting the path environment variable to locate the OpenCV binaries, configuring Visual C++ project settings to include OpenCV headers and libraries, and provides a simple "Hello World" example to test the installation. Key steps include adding OpenCV binary and include directories to the path and to Visual C++ settings, and linking additional dependencies for OpenCV libraries. The example displays an image to verify the OpenCV installation is working properly.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Using OpenCV in Microsoft Visual C++

September 2009

This is a short tutorial on how to compile and run a simple OpenCV program using Microsoft Visual
C++.

You should already know how to create a simple “Hello world” program in Microsoft Visual C++. If not,
please go through the tutorial called “Creating Your First Program in Microsoft Visual C++”. Also, if
you have not already done so, download OpenCV following the instructions in the class lecture notes.

Setting up Path Environment Variable

If you did not install OpenCV by running the executable file, you will have to manually set up your
computer’s path environment variable to point to the OpenCV binary directory. This will allow the
programs you create to use the “dll” library files in that directory. Go to “Start” -> “Settings”
-> “Control Panel” and select “System”. Select the “Advanced” tab as shown below.

Click on “Environment Variables”. This will bring up the following pop-up window:

1
Under “User variables”, create a variable called “Path” (or edit) the variable if it already exists.
Append the location of the OpenCV binary directory to the path. On my computer this is
C:\Program Files\OpenCV\bin;
However, if you loaded OpenCV into your home directory on “adit”, this location will be different;
probably something like:
Z:\adit\My Documents\OpenCV\bin;

Verify if OpenCV was correctly installed. Go to where OpenCV is located and go into the directory
called “samples/c”. Double click on the program called [Link]. If this doesn’t run, and
you get some error something like “[Link] was not found”, then the path was incorrectly set.

Setting up Visual C++

Start up Microsoft Visual C++. Go to Tools->Options. In the pop-up window, select “VC++
Directories”. Under the tab “Show Directories for”, choose “Include files”. Add the
directories in the list box that are surrounded by a red line in the following figure. Of course, you may
have to change the location if OpenCV is not located in “Program Files”.

2
Now choose Library files and add “C:\Program Files\OpenCV\lib” as shown in the following figure:

Now choose Source files and add the directories that are surrounded by a red line in the following figure:

3
Now just click OK to close the dialog.

Creating a Simple OpenCV Program

Create a simple “Hello world” program in Microsoft Visual C++ (or you can use the program from the
previous tutorial).

Every time you create a program, you have to add dependency information to VC++. Select “Project-
>myproject1 Properties …” This will bring up a window as shown below. Select
Configuration Properties -> Linker -> Input. Go to the tab for Additional
Dependencies, and add the following:

[Link] [Link] [Link] [Link]

Then just click OK to close the window.

4
In the main window, enter the following code for [Link]. (You should be able to copy and paste it.)

#include <iostream>

#include <cv.h>
#include <cxcore.h>
#include <highgui.h>

using namespace std;

int main(int argc, char* argv[])


{
printf("Hello world\n");

IplImage *img = cvLoadImage("C:/Program Files/OpenCV/samples/c/[Link]");

// Create a window
cvNamedWindow( "result",
CV_WINDOW_AUTOSIZE // allow window to resize to fit image true size
);

cvShowImage( "result", img ); // Show image in window already created

// Wait for a keystroke. If a positive argument is given, it will wait for


// that number of milliseconds and then continue. If 0 is given, the
// program will wait indefinitely for a keypress.
cvWaitKey(0);

// Clean up (not really necessary, but good programming practice)

5
cvReleaseImage( &img );
cvDestroyWindow("result");

system("PAUSE");
return EXIT_SUCCESS;
}

Choose Debug->Start Without Debugging to compile and run the program. If there are no compile errors,
the program should display an image. If a blank image is displayed, then the program cannot find the
image file specified (check that the location is correct).

Congratulations! You have created your first OpenCV program. For help, see documentation at
C:\Program Files\OpenCV\docs\[Link].

Common questions

Powered by AI

The 'cvWaitKey' function in an OpenCV program pauses the execution, waiting for a keypress. If a positive argument is given, it waits for the specified number of milliseconds before continuing. With an argument of 0, it waits indefinitely. This is useful for allowing users to view displayed images before closing the program .

Setting up the path environment variable is crucial when installing OpenCV manually because it allows the system to locate and use the OpenCV binary DLL files during the execution of programs. Without this, programs may fail to run due to missing dependencies .

Releasing resources like images and windows in OpenCV programs is considered good practice because it ensures efficient memory management and prevents memory leaks. Functions like 'cvReleaseImage' and 'cvDestroyWindow' help clean up resources that the program no longer needs, contributing to overall program stability and performance .

To ensure OpenCV is correctly integrated with Microsoft Visual C++, you need to set up the Path Environment Variable to include the OpenCV binary directory. This allows programs to use the necessary DLL files. Additionally, in Visual C++, you must configure the VC++ Directories under Options to include the Include files, Library files, and Source files paths for OpenCV. Finally, when creating a new project, add the OpenCV libraries (cv.lib, cxcore.lib, highgui.lib, cvaux.lib) to the project dependencies .

Modifying VC++ Directories in Visual C++ to include OpenCV is necessary to inform the compiler where to find the necessary Include files, Library files, and Source files. This setup allows for successful compilation and linking of OpenCV functions and libraries within a Visual C++ project .

If a program using OpenCV specifies an incorrect image path, the likely outcome is a blank or missing image display in the OpenCV window. This error occurs because the program cannot locate the specified image file to load, leading to a failure to display the intended image .

Issues when running an OpenCV sample program can often be diagnosed by checking if the path environment variable is correctly set, particularly if errors like missing DLL files occur. If a program cannot find an image file, verify that the specified path for the image is correct. Additionally, ensure that all relevant OpenCV libraries are included as dependencies in the project setup .

To include OpenCV libraries in a Visual C++ project, navigate to Project Properties, then Configuration Properties -> Linker -> Input. Under Additional Dependencies, add the OpenCV libraries such as cv.lib, cxcore.lib, highgui.lib, and cvaux.lib. This step ensures these libraries are linked with the project for proper functioning .

If an OpenCV program fails to compile due to missing headers, the initial step is to verify that the Include file directories in the VC++ Directories settings of Visual C++ are correctly configured to point to the OpenCV include paths. Ensuring these paths are correctly set allows the compiler to find needed header files .

To verify the correct installation of OpenCV, navigate to the location of OpenCV on your system and run a sample program such as 'contours.exe'. If it executes without errors, the installation is successful. Errors like "cxcore110.dll was not found" indicate incorrect setup of the path environment variable .

You might also like