Beispiel #1
0
int main( int argc, char** argv )
{
    CvCapture* capture = cvCreateCameraCapture(0);
    CvMat* prevgray = 0, *gray = 0, *flow = 0, *cflow = 0;
    (void)argc; (void)argv;

    help();

    if( !capture )
        return -1;

    cvNamedWindow("flow", 1);

    for(;;)
    {
        int firstFrame = gray == 0;
        IplImage* frame = cvQueryFrame(capture);
        if(!frame)
            break;
        if(!gray)
        {
            gray = cvCreateMat(frame->height, frame->width, CV_8UC1);
            prevgray = cvCreateMat(gray->rows, gray->cols, gray->type);
            flow = cvCreateMat(gray->rows, gray->cols, CV_32FC2);
            cflow = cvCreateMat(gray->rows, gray->cols, CV_8UC3);
        }
        cvCvtColor(frame, gray, CV_BGR2GRAY);

        if( !firstFrame )
        {
            cvCalcOpticalFlowFarneback(prevgray, gray, flow, 0.5, 3, 15, 3, 5, 1.2, 0);
            cvCvtColor(prevgray, cflow, CV_GRAY2BGR);
            drawOptFlowMap(flow, cflow, 16, 1.5, CV_RGB(0, 255, 0));
            cvShowImage("flow", cflow);
        }
        if(cvWaitKey(30)>=0)
            break;
        {
        CvMat* temp;
        CV_SWAP(prevgray, gray, temp);
        }
    }
    cvReleaseCapture(&capture);
    return 0;
}
int isMotionDetected(struct MotionDetector *motionDetector, IplImage *capturedImage) {
    int initialFrame = motionDetector->matrixOfGray == 0;
    if (!motionDetector->matrixOfGray) {
        //create a matrix for matrixOfGray, with same dimensions as webcam image
        motionDetector->matrixOfGray = cvCreateMat(capturedImage->height, capturedImage->width, CV_8UC1);
        //matrix for prevgrey same dimensions as grey
        motionDetector->prev_frame_grayscale = cvCreateMat(motionDetector->matrixOfGray->rows, motionDetector->matrixOfGray->cols, motionDetector->matrixOfGray->type);
        //maps for storing fback_flow_map data from the farneback function
        motionDetector->fback_flow_map = cvCreateMat(motionDetector->matrixOfGray->rows, motionDetector->matrixOfGray->cols, CV_32FC2);
        motionDetector->cflow = cvCreateMat(motionDetector->matrixOfGray->rows, motionDetector->matrixOfGray->cols, CV_8UC3);
    }

    //convert frame to grayscale image and place it in the memory for matrixOfGray
    cvCvtColor(capturedImage, motionDetector->matrixOfGray, CV_BGR2GRAY);

    if (!initialFrame) {
        cvCalcOpticalFlowFarneback(motionDetector->prev_frame_grayscale, motionDetector->matrixOfGray, motionDetector->fback_flow_map, 0.5, 3, 15, 3, 5, 1.2, 0);
        cvCvtColor(motionDetector->prev_frame_grayscale, motionDetector->cflow, CV_GRAY2BGR);

        return motionfactor(motionDetector->fback_flow_map, motionDetector->cflow, 16) > motionDetector->motion_min_val;
    }
    return 0;
}
Beispiel #3
0
int main()
{
    const IplImage* im1 = cvLoadImage("302.png",0);
    const IplImage* im2 = cvLoadImage("303.png",0);
    //int w_s = 10;
    int w = im1->width;
    int h = im1->height;
    //printf("Width = %d\nHeight = %d\n",w,h);
    CvMat* vel = cvCreateMat(h,w,CV_32FC2);
    CvMat* velx = cvCreateMat(h,w,CV_32FC1);
    CvMat* vely = cvCreateMat(h,w,CV_32FC1);
    CvMat* u = cvCreateMat(h/10, w/10, CV_32FC1); // Averaged Optical flows
    CvMat* v = cvCreateMat(h/10, w/10, CV_32FC1);

   //printf("matDimU = %d %d\nMatDimVel = %d %d\n ",cvGetMatSize(u),cvGetMatSize(velx));
   //printf("Ptr = %d %d \n",im1->data.ptr,velx->data.ptr);
    //cvCalcOpticalFlowLK(im1,im2,cvSize(4,4),velx,vely);
    //cvCalcOpticalFlowFarneback(const CvArr* prev, const CvArr* next, CvArr* flow,
    //          double pyr_scale, int levels, int winsize, int iterations, int poly_n, double poly_sigma, int flags) flag means to use Gaussian smoothing
    cvCalcOpticalFlowFarneback(im1, im2, vel,0.5, 1, 2, 2, 2, 0.17, 0);//, iterations, poly_n, poly_sigma
    cvSplit(vel, velx, vely, NULL, NULL);
    average_flow(velx, u);
    average_flow(vely, v);

    /*//cvSave("u.xml", u);
    //cvSave("v.xml", v);*/
    saveMat(u,"ux.m");
    saveMat(v,"vy.m");

/*    CvMat* Big = cvCreateMat(50,50,CV_32FC1);
    cvSetIdentity(Big);
    CvMat* small = cvCreateMat(5,5,CV_32FC1);
    average_flow(Big,small);
    printMat(small);*/
    return 0;
}
void ofxOpticalFlowFarneback::update(IplImage * previousImage, IplImage * currentImage) {
	if((previousImage->width != currentImage->width) || (previousImage->height != currentImage->height)) {
		return; // images do not match.
	}

	int w = currentImage->width;
	int h = currentImage->height;

	if(flow) {
		if((flow->width != w) || (flow->height != h)) {
			cvReleaseMat(&flow);
			flow = cvCreateMat(h, w, CV_32FC2);
		}
	}
	else {
		flow = cvCreateMat(h, w, CV_32FC2);
	}

	int flags = 0;
	if(flowFeedback) flags |= cv::OPTFLOW_USE_INITIAL_FLOW;
	if(gaussianFiltering) flags |= cv::OPTFLOW_FARNEBACK_GAUSSIAN;

	cvCalcOpticalFlowFarneback(previousImage, currentImage, flow, pyr_scale, levels, winsize, iterations, poly_n, poly_sigma, flags);
}