OpenCV Setup Instructions
Author: When: Wednesday, 03 March 2010OpenCV 2.0 can be difficult to setup and install. Here are some setup instructions for Windows and Mac OS X, including how to create a new project in Visual C / Xcode.
Windows Setup
Firstly, we need to compile OpenCV for Windows. You can download OpenCV from the SorceForge downloads page.. Get the latest version (as of writing, OpenCV-2.0.0a-win32.exe). You will also need a copy of CMake, so download and install that too.
Run CMake GUI tool and configure OpenCV there:
Click the Configure button in the lower left hand corner, and in the window that opens, choose your IDE (e.g. Visual Studio 9):
Once you've done this, click configure again, and the generate button should become enabled:
Click the generate button:
Once all the files have been generated, you will need to open the project that has been created in the output directory you choose earlier:
Build this project in both debug and release mode (this could take a bit of time depending on your computer):
When creating a new project, you will need to ensure that the headers and libraries are available:
Add the following to your new project (Tools → Options → Projects and Solutions → VC++ Directories):
- Include Files
- Add in the directory containing OpenCV header files (e.g.
C:\OpenCV2.0) - Library Files
C:\[OpenCV2.0 Build Directory]\lib\debugC:\[OpenCV2.0 Build Directory]\lib\release- Source Files
C:\[OpenCV2.0 Build Directory]\src\cvC:\[OpenCV2.0 Build Directory]\src\cvauxC:\[OpenCV2.0 Build Directory]\src\cxcoreC:\[OpenCV2.0 Build Directory]\src\highgui
Thanks to Yen-Sheng Pan for these instructions.
Mac OS X 10.6 / Xcode 3.0 Setup
Download and install the package here: OpenCV-2.0.0-Darwin.dmg.
Create a new Xcode "Command Line Tool" project:
From the menu, select Project → Edit Active Target "...". Under the "Configuration" drop down, select "All Configurations", and then edit the options under "Architectures" to match the following:
You should now be able to compile your project with OpenCV.
Simple Example
#include <iostream>
#include <opencv/cv.h>
#include <opencv/highgui.h>
const char * WindowName = "Video 01";
int main (int argc, const char * argv[]) {
CvCapture * capture = NULL;
IplImage * frame = NULL, * prevFrame = NULL, * diffFrame = NULL;
int key = 0;
// Initialize Camera
capture = cvCaptureFromCAM(0);
if (capture == NULL) {
perror("Could not open camera device!");
return 1;
}
frame = cvQueryFrame(capture);
prevFrame = cvCloneImage(frame);
diffFrame = cvCloneImage(frame);
cvNamedWindow(WindowName, CV_WINDOW_AUTOSIZE);
std::cout << "Frame : " << frame->width << ", " << frame->height << std::endl;
while (key != 'q') {
frame = cvQueryFrame(capture);
if (frame == NULL) {
perror("Could not get frame from device!");
break;
}
cvAbsDiff(frame, prevFrame, diffFrame);
cvThreshold(diffFrame, diffFrame, 32, 0, CV_THRESH_TOZERO);
cvCopy(frame, prevFrame, NULL);
cvShowImage(WindowName, diffFrame);
key = cvWaitKey(1);
}
cvDestroyWindow(WindowName);
cvReleaseCapture(&capture);
return 0;
}
Thanks to Björn Giesler for putting together an installer.
OpenCV 2.0 Documentation
The best documentation I can find is this wiki. It has a good search function and I've generally found the documentation to be concise and easy to understand.
Comments
Please note, you can leave a comment that uses (limited) XHTML and Textile syntax.