To learn how to configure an OpenCV C/C++ project, create a new project for your compiler and copy & paste the following source code into your project:
#include <stdio.h>
// For printf()
#include <cv.h>
// Main OpenCV library.
#include <highgui.h>
// OpenCV functions for files and graphical windows.
int main(int argc, char* argv[])
{
// Open the file "lena.jpg".
IplImage* img = cvLoadImage("lena.jpg", CV_LOAD_IMAGE_UNCHANGED);
if (!img) {
printf("Error: Could not open the image file! \n");
exit(1);
}
// Blur the image.
cvSmooth(img, img, CV_BLUR);
// Save the blurred image to a file.
cvSaveImage("blurred.jpg", img);
// Show the blurred image on the screen.
cvNamedWindow("Blurred", CV_WINDOW_AUTOSIZE);
cvShowImage("Blurred", img);
// Wait for the user to press something on the graphical window.
// Note: cvWaitKey() is needed for time to draw on the screen.
cvWaitKey(0);
// Free the resources.
cvDestroyWindow("Blurred");
cvReleaseImage( &img );
return 0;
}
- To compile any OpenCV project such as this, you must first tell your compiler where to find the header include files (located in the "include/opencv" folder of OpenCV). For Visual Studio 2008, click on "Tools -> Options -> Projects and Solutions -> VC++ Directories", then click on "Show Directories for:" and select "Include Files". Add "C:\OpenCV2.1\include\opencv" (or wherever the file "cv.h" is) and hit OK. For other compilers, read the instructions at "http://opencv.willowgarage.com/wiki/InstallGuide#Configure"
- You must also tell your compiler where to find the lib files (located in the "lib" folder of OpenCV). For Visual Studio 2008, click on the same "Tools -> Options -> Projects and Solutions -> VC++ Directories", then click on "Show Directories for:" and select "Library Files". Add "C:\OpenCV2.1\lib" (or wherever the file "cv210.lib" is) and hit OK. For other compilers, read the instructions at "http://opencv.willowgarage.com/wiki/InstallGuide#Configure"
- Add the OpenCV lib files you will use. Lib files for release builds: cv210.lib, cxcore210.lib, cvaux210.lib and highgui210.lib. Lib files for debug builds: cv210d.lib, cxcore210d.lib, cvaux210d.lib and highgui210d.lib. For Visual Studio 2008, click on the Project "Properties -> Linker -> Input -> Additional Dependencies" and add "cv210d.lib cxcore210d.lib cvaux210d.lib highgui210d.lib" when in Debug configuration, then click on "Configuration" and select "Release", then in Additional Dependencies, add "cv210.lib cxcore210.lib cvaux210.lib highgui210.lib".
- Build the project. (For Visual Studio 2008, click "Build -> Build Solution"). If it gives error messages that refer to ".cpp", ".c" or ".h" files then you haven't setup the include files properly (step 1). But if it gives error messages that refer to "linker" or ".o", ".obj", ".lib" or ".a" files then you haven't setup the library files properly (steps 2 and 3).
- Copy the image "samples\c\lena.jpg" to the same folder as the generated program (on Windows this is the EXE file in the Debug or Release folder).
- Run the program. You should see a window created with a blurry photo until you type something on your computer while the window is focused, and also there should be a new image "blurred.jpg" stored in the same folder as the program. If you instantly get a popup error saying it can't find "cxcore210.dll" or "cxcore.dll" or anything like that, it means you don't have the OpenCV runtime on your path (DLL files on Windows), so you should either copy the OpenCV DLL files into the same folder as your EXE or add OpenCV's bin folder to your PATH system variable.