Tuesday, March 08, 2005

My First OpenCV Program

Now that I've gotten OpenCV installed, the first thing I needed to try was to create a sample program of some kind that was basically not useful for anything except to demonstrate that I could compile an OpenCV program. Heres the code for test.c, my first attempt:

#include "cv.h"
#include "highgui.h"

int main( int arc, char ** argv)
{
int W = 640;
int H = 480;

// Images are stored in IplImage structs. The function cvCreateImage
// allocates space for an image of a certain type, in this case an
// 8 bit, 1 channel (grayscale) image.
IplImage *image = cvCreateImage(cvSize(W, H), IPL_DEPTH_8U, 1);

// Create a named window and show our image on it
cvNamedWindow("My First OpenCV Program", 1);
cvShowImage("My First OpenCV Program", image);

// Wait for any keystroke and then release the image from memory and exit
// the program
cvWaitKey(0);
cvReleaseImage(&image);
return 0;
}

All this code does is create a window and show an image of size 640x480 that was created using cvCreateImage. The image is black, which isn't surprising since we didn't put anything in it. In order to compile it from a shell I did:

gcc test.c -o test `pkg-config --libs opencv --cflags opencv`

0 Comments:

Post a Comment

<< Home