This short piece of code below captures a frame from a web cam and shows it in a window. Some simple transformations can be applied.
#include
#include
int main(void)
{
//we assume, that there is only one camera
CvCapture* capture= cvCaptureFromCAM(-1);
IplImage* img;
int loop;
while(loop++<100)
{
cvGrabFrame(capture); // capture a frame
img=cvRetrieveFrame(capture,1); // retrieve the captured frame
// get the properties of the image
int width = img->width;
int height = img->height;
int nchannels = img->nChannels;
int step = img->widthStep;
// setup the pointer to access image data
uchar *data = ( uchar* )img->imageData;
int i, j, value;
int Rin, Gin, Bin;
int Rout, Gout, Bout;
for( i = 0 ; i < height ; i++ ) {
for( j = 0 ; j < width ; j++ ) {
Rin = data[i*step + j*nchannels + 0];
Gin = data[i*step + j*nchannels + 1];
Bin = data[i*step + j*nchannels + 2];
//------ begin image maniupulation ------
//one-to-one mapping
//Rout=Rin;Gout=Gin;Bout=Bin;
//grayscale conversion
//Rout = Gout = Bout = ( Rin + Gin + Bin ) / 3;
//some black and white thresholding
Rout = Gout = Bout = ( Rin + Gin + Bin ) > 3*255/2 ? 255: 0;
//------ end image maniupulation ------
data[i*step + j*nchannels + 0] = Rout;
data[i*step + j*nchannels + 1] = Gout;
data[i*step + j*nchannels + 2] = Bout;
}
}
cvShowImage("video output", img);
cvWaitKey(20);
}
cvReleaseCapture(&capture);
return 0;
}
The code is simply compiled with:
gcc -Wall highgui_test.c -o highgui_test -lcv -I /usr/include/opencv/ -lhighgui