Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef PANOPTICON_H
- #define PANOPTICON_H
- #endif // PANOPTICON_H
- #include <cv.h>
- #include <cxcore.h>
- #include <highgui.h>
- #include <QtGui/QApplication>
- #include <fstream>
- #include <stdio.h>
- #include <math.h>
- #include <sys/stat.h>
- using namespace std;
- //global parameters
- const int bgLength=100;
- const int bgThreshold=8;
- const int MaxBlobNum=6;
- bool has_BG_Model=true;
- // Dir of video file
- #define VideoDir "/seq1.avi"
- int compare(const void *a,const void *b){
- // Returns -1 if elem1 < elem2
- // 0 if elem1 == elem2
- // +1 if elem1 > elem2
- if (*(double*)a < *(double*)b) {
- return -1;
- } else if (*(double*)a > *(double*)b) {
- return +1;
- }
- return 0;
- }
- double FindMedian(IplImage* data)
- {
- int w = data->width, h=data->height;
- double *arr;
- int size;
- arr = new double[w*h];
- if (w==1 || h==1)
- {
- size = (w > h ? w : h );
- for (int i=0;i<size;i++)
- {
- arr[i]=cvGet1D(data,i).val[0];
- }
- }
- else
- {
- size =(w * h );
- for(int row=0;row<h;row++)
- for(int col=0;col<w;col++)
- {
- arr[row*w + col] = cvGetReal2D(data,row,col);
- }
- }
- qsort(arr,size,sizeof(double),compare); // using qsort for sorting or u can see bubble sort, selection sort...
- //for (int i=0;i<size;i++) cout<<arr[i]<<endl;
- int middle = size/2;
- double average;
- if (size%2==0) average = static_cast<double>(arr[middle-1]+arr[middle])/2;
- else average = static_cast<double>(arr[middle]);
- return average;
- }
- // take the frame array, fill pixelwise vectors, calculate median and stddev, return median & stddev matrices
- void bgInitStats(IplImage * StddevFrame, IplImage * BGFrameArray[], IplImage * MedianFrame, IplImage * bgmFrame, IplImage * bgnFrame, IplImage * bgdFrame)
- {
- CvScalar stddev;
- CvScalar median;
- CvScalar mean;
- IplImage* temp=cvCreateImage(cvSize(bgLength,1),BGFrameArray[0]->depth,1);
- IplImage* stemp=cvCreateImage(cvSize(bgLength,1),BGFrameArray[0]->depth,1);
- //temp: vector store values taken in time for pixelwise calculations
- //stemp: stationary degerleri tutmak icin
- //pixelwise calculations: median, stdDev, (min,max,max diff) matrices
- for (int row=0;row<BGFrameArray[0]->height ;row++)
- for(int col =0; col<BGFrameArray[0]->width;col++)
- {
- //put pixel values in vector, BGframe array => temp
- for(int n=0; n<bgLength;n++)
- {
- cvSet1D(temp,n,cvGet2D(BGFrameArray[n],row,col));
- }
- //median,stddev to be calculated here
- {
- cvAvgSdv(temp,&mean,&stddev);
- median.val[0] = FindMedian(temp);
- cvSet2D(StddevFrame,row,col,stddev);
- cvSet2D(MedianFrame,row,col,median);
- }
- //varible declarations (for stationary pixel test and maxdiff)
- int bgCounter =0;
- CvScalar sPix;
- CvScalar Maxdiff= cvScalar(0);
- int prevVal=0;
- int diff=0;
- //detection of stationary pixels
- for(int n=0; n<bgLength;n++)
- {
- sPix=cvGet1D(temp,n);
- if(abs(sPix.val[0]-median.val[0])<(2*stddev.val[0]))
- {
- //case for stationary pixels
- cvSet1D(stemp,bgCounter,sPix);
- bgCounter++;
- }
- }
- //tranfer the stationary pixel values to a smaller vector
- //min, max, dif calculated from the stemp2 vector
- IplImage* stemp2=cvCreateImage(cvSize(bgCounter,1),stemp->depth,stemp->nChannels);
- cvSetImageROI(stemp,cvRect(0,0,bgCounter,1));
- cvCopy(stemp,stemp2);
- cvResetImageROI(stemp);
- //find max diff
- for (int n=0;n<bgCounter;n++)
- {
- sPix=cvGet1D(stemp2,n);
- if(n==0)
- prevVal=sPix.val[0];
- else
- {
- diff=abs(sPix.val[0]-prevVal);
- if(diff>Maxdiff.val[0])
- Maxdiff.val[0]=diff;
- prevVal=sPix.val[0];
- }
- }
- //find min, max
- double min, max;
- CvPoint min_loc, max_loc;
- cvMinMaxLoc(stemp2,&min,&max,&min_loc,&max_loc);
- CvScalar smin= cvScalar(min);
- CvScalar smax= cvScalar(max);
- // record min, max, dif to their own matrices of (row x column)
- {
- //m(x) - max frame
- cvSet2D(bgmFrame,row,col,smax);
- //n(x) - min frame
- cvSet2D(bgnFrame,row,col,smin);
- //d(x) - abs max difference
- cvSet2D(bgdFrame,row,col,Maxdiff);
- //cvSet1D(bgdFrame,row*(BGFrameArray[0]->height)+col,Maxdiff);
- }
- }
- //cvReleaseImage(&temp);
- }
- //connected component parameters
- /*////////////////////////////////////////////////////////////////
- // void find_connected_components(IplImage *mask, int poly1_hull0,
- // float perimScale, int *num,
- // CvRect *bbs, CvPoint *centers)
- // This cleans up the foreground segmentation mask derived from calls
- // to backgroundDiff
- //
- // mask Is a grayscale (8-bit depth) raw mask image that
- // will be cleaned up
- //
- // OPTIONAL PARAMETERS:
- // poly1_hull0 If set, approximate connected component by
- // (DEFAULT) polygon, or else convex hull (0)
- // perimScale Len = image (width+height)/perimScale. If contour
- // len < this, delete that contour (DEFAULT: 4)
- // num Maximum number of rectangles and/or centers to
- // return; on return, will contain number filled
- // (DEFAULT: NULL)
- // bbs Pointer to bounding box rectangle vector of
- // length num. (DEFAULT SETTING: NULL)
- // centers Pointer to contour centers vector of length
- // num (DEFAULT: NULL)
- */
- void find_connected_components( IplImage* mask, int poly1_hull0 = 1, float perimScale = 4, int* num = NULL, CvRect* bbs = NULL, CvPoint* centers = NULL )
- {
- // For connected components:
- // Approx.threshold - the bigger it is, the simpler is the boundary
- //
- #define CVCONTOUR_APPROX_LEVEL 2
- // How many iterations of erosion and/or dilation there should be
- //
- #define CVCLOSE_ITR 1
- static CvMemStorage* mem_storage = NULL;
- static CvSeq* contours = NULL;
- //CLEAN UP RAW MASK
- //
- cvMorphologyEx( mask, mask, 0, 0, CV_MOP_OPEN, CVCLOSE_ITR );
- cvMorphologyEx( mask, mask, 0, 0, CV_MOP_CLOSE, CVCLOSE_ITR );
- //FIND CONTOURS AROUND ONLY BIGGER REGIONS
- //
- //cvNamedWindow("After Morphology",CV_WINDOW_AUTOSIZE);
- //cvShowImage("After Morphology",mask);
- //cvWaitKey();
- //cvDestroyWindow("After Morphology");
- if( mem_storage==NULL ) {
- mem_storage = cvCreateMemStorage(0);
- } else {
- cvClearMemStorage(mem_storage);
- }
- CvContourScanner scanner = cvStartFindContours(mask,mem_storage,sizeof(CvContour),CV_RETR_EXTERNAL,CV_CHAIN_APPROX_SIMPLE);
- CvSeq* c;
- int numCont = 0;
- while( (c = cvFindNextContour( scanner )) != NULL ) {
- double len = cvContourPerimeter( c );
- // calculate perimeter len threshold:
- //
- double q = (mask->height + mask->width)/perimScale;
- //Get rid of blob if its perimeter is too small:
- //
- if( len < q ) {
- cvSubstituteContour( scanner, NULL );
- } else {
- // Smooth its edges if its large enough
- //
- CvSeq* c_new;
- if( poly1_hull0 ) {
- // Polygonal approximation
- //
- c_new = cvApproxPoly(c,sizeof(CvContour),mem_storage,CV_POLY_APPROX_DP,CVCONTOUR_APPROX_LEVEL,0);
- } else {
- // Convex Hull of the segmentation
- //
- c_new = cvConvexHull2(c,mem_storage,CV_CLOCKWISE,1);
- }
- cvSubstituteContour( scanner, c_new );
- numCont++;
- }
- }
- contours = cvEndFindContours( &scanner );
- // Just some convenience variables
- const CvScalar CVX_WHITE = CV_RGB(0xff,0xff,0xff);
- const CvScalar CVX_BLACK = CV_RGB(0x00,0x00,0x00);
- // PAINT THE FOUND REGIONS BACK INTO THE IMAGE
- //
- cvZero( mask );
- IplImage *maskTemp;
- // CALC CENTER OF MASS AND/OR BOUNDING RECTANGLES
- //
- if(num != NULL) {
- //User wants to collect statistics
- //
- int N = *num, numFilled = 0, i=0;
- CvMoments moments;
- double M00, M01, M10;
- maskTemp = cvCloneImage(mask);
- for(i=0, c=contours; c != NULL; c = c->h_next,i++ ) {
- if(i < N) {
- // Only process up to *num of them
- //
- cvDrawContours(maskTemp,c,CVX_WHITE,CVX_WHITE,-1,CV_FILLED,8);
- // Find the center of each contour
- //
- if(centers != NULL) {
- cvMoments(maskTemp,&moments,1);
- M00 = cvGetSpatialMoment(&moments,0,0);
- M10 = cvGetSpatialMoment(&moments,1,0);
- M01 = cvGetSpatialMoment(&moments,0,1);
- centers[i].x = (int)(M10/M00);
- centers[i].y = (int)(M01/M00);
- }
- //Bounding rectangles around blobs
- //
- if(bbs != NULL) {
- bbs[i] = cvBoundingRect(c);
- }
- cvZero(maskTemp);
- numFilled++;
- }
- // Draw filled contours into mask
- //
- cvDrawContours(mask,c,CVX_WHITE,CVX_WHITE,-1,CV_FILLED,8);
- } //end looping over contours
- *num = numFilled;
- cvReleaseImage( &maskTemp);
- }
- // ELSE JUST DRAW PROCESSED CONTOURS INTO THE MASK
- //
- else {
- // The user doesnt want statistics, just draw the contours
- //
- for( c=contours; c != NULL; c = c->h_next ) {
- cvDrawContours(mask,c,CVX_WHITE,CVX_BLACK,-1,CV_FILLED,8);
- }
- }
- }
- void PrintMatrix(CvMat* Mat)
- {
- CvSize Size = cvGetSize(Mat);
- for (int i = 0;i<Size.height;i++)
- {
- for(int j = 0;j<Size.width;j++)
- {
- CvScalar El = cvGet2D(Mat, i, j);
- printf("%f |" , El.val[0]);
- //printf("%f |" , cvmGet(Mat, i, j));
- }
- printf("||\n");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment