基于codebook model的运动目标检测—整理opencv源码

来源:互联网 发布:淘宝卖家体验中心 编辑:程序博客网 时间:2024/05/16 13:56
************************************************** */
#include "stdafx.h"
#include "opencv2/core/core.hpp"
#include "opencv2/video/background_segm.hpp"
#include "opencv2/imgproc/imgproc_c.h"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/legacy/legacy.hpp"


#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>


using namespace std;
using namespace cv;


//VARIABLES for CODEBOOK METHOD:
CvBGCodeBookModel* model = 0;
const int NCHANNELS = 3;
bool ch[NCHANNELS]={true,true,true}; // This sets what channels should be adjusted for background bounds


const char *keys =  //input information
{
    "{nf|nframes   |10        |frames number}"
    "{c |camera    |false      |use the camera or not}"
    "{mf|movie_file|tree.avi   |used movie video file}"
};
int main(int argc, const char** argv)
{
IplImage* rawImage = 0, *yuvImage = 0; //yuvImage is for codebook method
    IplImage *ImaskCodeBook = 0,*ImaskCodeBookCC = 0;
//camera information
    CvCapture* capture = 0;
double BrightValue=0.0f;
double GaintValue=0.0,Contrast=0.0,SaturationValue=0.0;
//.......................
    int c, n, nframes = 0;


//define and init model struct
    model = cvCreateBGCodeBookModel();  


    //Set color thresholds to default values
    model->modMin[0] = 3;
    model->modMin[1] = model->modMin[2] = 3;
    model->modMax[0] = 10;
    model->modMax[1] = model->modMax[2] = 10;
    model->cbBounds[0] = model->cbBounds[1] = model->cbBounds[2] = 10;

    bool pause = false;
    bool singlestep = false;


    if( 0 )  //capture camera
    {
        capture = cvCaptureFromCAM( 0 );
    }
    else    //capture file of video
    {
        capture = cvCreateFileCapture("tree.avi");
    }


    if( !capture )
    {
        printf( "Can not initialize video capturing\n\n" );
        help();
        return -1;
    }
else{


//bright 
BrightValue=cvGetCaptureProperty(capture,CV_CAP_PROP_BRIGHTNESS);
//gain
GaintValue=cvGetCaptureProperty(capture,CV_CAP_PROP_GAIN);
//Exposure 
Contrast=cvGetCaptureProperty(capture,CV_CAP_PROP_EXPOSURE);
//Saturation
        SaturationValue=cvGetCaptureProperty(capture,CV_CAP_PROP_CONTRAST);
   printf("%f\n",BrightValue);


}

    //MAIN PROCESSING LOOP:
    for(;;)
    {
//_________________________________
        if( !pause ) //byte pause
        {


//cvSetCaptureProperty(capture,CV_CAP_PROP_BRIGHTNESS,300);
//gain
//cvSetCaptureProperty(capture,CV_CAP_PROP_GAIN,10);
//Exposure 
//cvSetCaptureProperty(capture,CV_CAP_PROP_EXPOSURE,200);
//Saturation
//cvSetCaptureProperty(capture,CV_CAP_PROP_CONTRAST,300);


            rawImage = cvQueryFrame( capture );
            ++nframes;  //
            if(!rawImage)
                break;
        }
        if( singlestep )
            pause = true;
//_________________________________




        //First time:   init
        if( nframes == 1 && rawImage )
        {
            // CODEBOOK METHOD ALLOCATION
            yuvImage = cvCloneImage(rawImage);
            ImaskCodeBook = cvCreateImage( cvGetSize(rawImage), IPL_DEPTH_8U, 1 );  //one channel
            ImaskCodeBookCC = cvCreateImage( cvGetSize(rawImage), IPL_DEPTH_8U, 1 );
            cvSet(ImaskCodeBook,cvScalar(255));


            cvNamedWindow( "Raw", 1 );
            cvNamedWindow( "ForegroundCodeBook",1);   
            cvNamedWindow( "CodeBook_ConnectComp",1);   
        }


        // If we've got an rawImage and are good to go:
        if( rawImage )
        {
            cvCvtColor( rawImage, yuvImage, CV_BGR2YCrCb );//YUV For codebook method


//first time
            //This is where we build our background model
            if( !pause && nframes-1 < 300  )  //updata 300 frame image
                cvBGCodeBookUpdate( model, yuvImage );


//second time
            if( nframes-1 == 300  )           //clear updata data
                cvBGCodeBookClearStale( model, model->t/2 );


//third time   but once updata
            //Find the foreground if any
            if( nframes-1 >= 300  )           //processing
            {
                // Find foreground by codebook method
                cvBGCodeBookDiff( model, yuvImage, ImaskCodeBook ); 
                // This part just to visualize bounding boxes and centers if desired
                cvCopy(ImaskCodeBook,ImaskCodeBookCC);
                cvSegmentFGMask( ImaskCodeBookCC );//连通域分割
            }
            //Display
            cvShowImage( "Raw", rawImage );
            cvShowImage( "ForegroundCodeBook",ImaskCodeBook);
            cvShowImage( "CodeBook_ConnectComp",ImaskCodeBookCC);
        }


        // User input:
        c = cvWaitKey(10)&0xFF;
        c = tolower(c);
        // End processing on ESC, q or Q
        if(c == 27 || c == 'q')
            break;
    }
cvBGCodeBookClearStale( model, 0 );
    cvReleaseCapture( &capture );
    cvDestroyWindow( "Raw" );
    cvDestroyWindow( "ForegroundCodeBook");
    cvDestroyWindow( "CodeBook_ConnectComp");
    return 0;
}