11.4. Programming Examples Using C/C++

Windows setup tips:

To get programs to compile in MSVC++, there are some things you must do.

- Release or debug mode. MSVC++ can make either release or debug code. OpenCV ships with only release code (you can build debug yourself). If a link error comes up requesting cvd.lib you are in debug mode. Go to the Build menu, select Set Active Configuration, and set it to release code.

- Include and library file paths must be set. Go to the Tools menu, select Options. Click on the Directories tab. Under Show Directories for, select Include files. Set the include path for

C:Program FilesOpenCVcvinclude
C:Program FilesOpenCVcvauxinclude
C:Program FilesOpenCVotherlibscvcaminclude
C:Program FilesOpenCVotherlibshighgui

Next, select Library files and set the path for C: Program FilesOpenCV lib.

- To avoid "unreferenced" function errors in link, go to Project, select Settings, and click on the Link tab. Under Object/library modules add cv.lib cvcam.lib highgui.lib.

- Finally, make sure the Windows path has C:Program FilesOpenCV in in it and that you've run RegisterAll.bat there at least once.

11.4.1. Read images from disk

The program below reads an image from disk (either from the command line or Mface.jpg by default) and performs pyramid-based color region segmentation controlled by two interactive sliders. The input and output along with slider controls are shown in Figure 11.31.

#ifdef _CH_
#pragma package <opencv>
#endif

#ifndef _EiC
#include "cv.h"
#include "highgui.h"
#include <math.h>
#endif
IplImage* image[2] = { 0, 0 }, *image0 = 0, *image1 = 0;
CvSize size;
int  w0, hO,i;
int  thresholdl, threshold2;
int  1,level = 4;
int sthreshold1, sthreshold2;
int  1-comp;
int block_size = 1000;
float  parameter;
double threshold;
double rezult, min_rezult;
CvFilter filter = CV_GAUSSIAN_5×5;
CvConnectedComp *cur_comp,min_comp;
CvSeq *comp;
CvMemStorage *storage;
CvPoint pt1, pt2;

void ON_SEGMENT(int a) {
    cvPyrSegmentation(imageO, image1, storage, &comp,
                      level, threshold1, threshold2);
    l_comp = comp->total;
    i = 0;
    min_comp.value = 0;
    while(i<1_comp)
    {
        cur_comp = (CvConnectedComp*)cvGetSeqElem ( comp, i, 0);
        if(fabs(CV_RGB(255,0,0)- min_comp.value)>
           fabs(CV_RGB(255,0,0)- cur_comp->value))
           min_comp = *cur_comp;
        i++;
    }
    cvShowImage("Segmentation", image1);
}

int main( int argc, char** argv ) {
    char* filename = argc == 2 ? argv[1] : (char*)"Mface.jpg";
    if( (image[0] = cvLoadImage( filename, 1)) == 0 )
        return -1;
    cvNamedWindow("Source", 0);
    cvShowImage("Source", image[0]);
    cvNamedWindow("Segmentation", 0) ;
    storage = cvCreateMemStorage ( block_size );
    image [0]->width &= -(l<<leve1);
    image [0]->height &= -(1<<level) ;
    image0 = cvCloneImage( image[0] );
    simage1 = cvCloneImage( image[0] );
    // segmentation of the color image
    1 = 1;
    thresholdl =255;
    threshold2 =30;
    ON_SEGMENT(1);
    sthreshold1 = cvCreateTrackbar("Threshold1", "Segmentation",
                                   &thresholdl, 255, ON_SEGMENT);
    sthreshold2 = cvCreateTrackbar("Threshold2", "Segmentation",
                                   &threshold2, 255, ON-SEGMENT);
    cvShowImage("Segmentation", image1);
    cvWaitKey(0);
    cvDestroyWindow("Segmentation");
    cvDestroyWindow("Source");
    cvReleaseMemStorage(&storage );
    cvReleaseImage(&image[0]);
    cvReleaseImage(&image0);
    cvReleaseImage(&image1) ;
    return 0;
}
#ifdef _EiC main(1,"pyramid_segmentation.c");
#endif

Figure 11.31. Program: Pyramid segmentation input and output. (Used with permission from [5, 6].)


11.4.2. Read AVIs from disk, or video from a camera

The following code example shows how to read an AVI movie from a disk and how to get images directly from the video camera.

#ifdef _CH_
#pragma package <opencv>
#endif

#ifndef _EiC
#include "cv.h"
#include "highgui.h"
#include <ctype.h>
#endif

int main( int argc, char** argv) {
    CvCapture* capture = 0;

    if( argc == 1 || (argc == 2 && strlen(argv[1]) == 1 &&
        isdigit(argv[1][0])))
        capture = cvCaptureFromCAM( argc == 2 ? argv[1][0] - '0' : 0 );
    else if( argc == 2 )
        capture = cvCaptureFromAVI( argv[1] );
    if( capture )
    {
        IplImage* gray = 0;
        IplImage* laplace = 0;
        IplImage* colorlaplace = 0;
        IplImage* planes[3] = { 0, 0, 0 };
        cvNamedWindow( "Laplacian", 0 );
        for(;;)
        {
            IplImage* frame = 0;
            int i;
            if( !cvGrabFrame( capture ))
                break;
            frame = cvRetrieveFrame( capture );
            if( !frame )
                break;
            if( !gray )
            {
                for( i = 0; i < 3; i++ )
                    planes[i] = cvCreateImage(
                             cvSize(frame->width,frame->height), 8, 1 );
                laplace = cvCreate!mage(cvSize(frame->width,frame->height),
                                        IPL_DEPTH_16S, 1 );
                colorlaplace = cvCreateImage(cvSize(frame->width,
                                             frame->height), 8, 3 );
            }
            cvCvtPixToPlane( frame, planes[0], planes[1], planes[2], 0 );
            for( i = 0; i < 3; i++ )
            {
                cvLaplace( planes[i], laplace, 3 );
                cvConvertScaleAbs( laplace, planes[i], 1, 0 );
            }
            cvCvtPlaneToPix(planes[0], planes[1], planes[2], 0,
                                       colorlaplace );
            colorlaplace->origin = frame->origin;
            cvShowImage("Laplacian", colorlaplace );
            if( cvWaitKey(10) >= 0 )
                break;
        }
        cvReleaseCapture( &capture );
    }
    cvDestroyWindow("Laplacian");
    return 0;
}
#ifdef _EiC
main(1,"laplace.c") ;
#endif

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.125.188