In last tutorial we learn how to get frames of our camera, now we go to learn some basic OpenCV functions to work with our captured frames or video. There is nothing special, but it's the first steps to introduce to work with OpenCV

Imagine we want create a basic OpenCV program that get a video o camera capture and process the images to get a binary image difference between actual frame and last frame.

Then we have a actual image, lastImage, diffImage where we store the difference images, and the bitImage result that are IplImage instance.

IplImage* image;
IplImage* lastImage;
IplImage* diffImage;
IplImage* bitImage;

Whe need create these variables and initialize with cvCreateImage, this function create and allocate in memory the structure and headers of our image, this function need as attribute image size, depth and number of channels. We get frame as reference to initialize the variables.

image=cvCreateImage(cvSize(frame->width,frame->height),frame->depth,1);

The frame whe get sure is a RGB image, but we go to working with gray level images the we need conver captured frame to Gray Level. The cvCvtColor conver color spaces of image to another and is defined:

void cvCvtColor( const CvArr* src, CvArr* dst, int code );
src is the source 8-bit (8u), 16-bit (16u) or single-precision floating-point (32f) image. dst is the destination image of the same data type as the source one. The number of channels may be different. And code is color conversion operation that can be specified using CV_<src_color_space>2<dst_color_space> constants
cvCvtColor(frame, image, CV_BGR2GRAY);

We can get a new copy in memory of image withcvCloneImage

lastImage=cvCloneImage(image);

To get the image differences we use cvAbsDiff function, to get absolute diference between images:

cvAbsDiff(image,lastImage,diffImage);

The function definition

void cvAbsDiff( const CvArr* src1, const CvArr* src2, CvArr* dst );
Where src1 is the first source array. src2 is the second source array, and dst is the destination array.

Then to finish we only need get a threshold of image, and we do it with cvThreshold function

cvThreshold(diffImage,bitImage,tr,255,CV_THRESH_BINARY);

And this function have this definition:

double cvThreshold( const CvArr* src, CvArr* dst, double threshold,
double max_value, int threshold_type );
Where src is source array (single-channel, 8-bit of 32-bit floating point), dst is destination array; must be either the same type as src or 8-bit, threshold is the threshold value, max_value is the maximum value to use with CV_THRESH_BINARY and CV_THRESH_BINARY_INV thresholding types, and threshold_type is thresholding type

This is the main loop program:

for (;;) {
    IplImage* frame = 0;
    int c;

    frame = cvQueryFrame(capture);
    if (!frame)
        break;
    //If is the first frame
    if (!image) {
        //Create image header same as frame but with 1 channel to gray
        image = cvCreateImage(cvSize(frame->width, frame->height), frame->depth, 1);
        bitImage = cvCreateImage(cvSize(frame->width, frame->height), frame->depth, 1);
    }
    //Convert frame to gray and store in image
    cvCvtColor(frame, image, CV_BGR2GRAY);

    //If is the first frame
    if (!lastImage) {

        //If no lastImage clone actual image;
        lastImage = cvCloneImage(image);
    }
    if (!diffImage) {
        //Create image header same as frame but with 1 channel to gray
        diffImage = cvCreateImage(cvSize(frame->width, frame->height), frame->depth, 1);
    }

    cvShowImage("CamSub", frame);
    //Differences with actual and last image
    cvAbsDiff(image, lastImage, diffImage);
    //threshold image
    cvThreshold(diffImage, bitImage, tr, 255, CV_THRESH_BINARY);
    //Change datas;
    lastImage = cvCloneImage(image);

    cvShowImage("CamSub 1", bitImage);

    c = cvWaitKey(10);
    if ((char)c == 27)
        break;
}