lunedì 26 agosto 2013

Motion Detection in OpenCV

sempre per fare un po' di esperienza per rendere la Raspberry un sistema di controllo video, un esempio di motion detection con OpenCV ripreso da questo link e leggermente modificato per salvare il video su disco
(il video e' accelerato)


per adesso il programma e' stato testato su un portatile. Presto provero' se Raspberry e' in grado di gestire il flusso dati video ed il relativo calcolo in realtime

--------------------------------------------
#include<opencv2/opencv.hpp>
#include<iostream>
#include<vector>

using namespace std;

int main(int argc, char *argv[])
{
    cv::Mat frame;
    cv::Mat back;
    cv::Mat fore;
    cv::VideoCapture cap(0);
    cv::BackgroundSubtractorMOG2 bg;
    bg.nmixtures = 3;
    bg.bShadowDetection = false;

    std::vector<std::vector<cv::Point> > contours;

    cv::namedWindow("Frame");


   double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH); 
   double dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT);
   cout << "Frame Size = " << dWidth << "x" << dHeight << endl;
   Size frameSize(static_cast<int>(dWidth), static_cast<int>(dHeight));
   VideoWriter oVideoWriter ("video.avi", CV_FOURCC('M','P','4','2'), 20, frameSize, true);
   if ( !oVideoWriter.isOpened() ) 
   {
        cout << "ERROR: Failed to write the video" << endl;
        return -1;
   }

    for(;;)
    {
        cap >> frame;
        bg.operator ()(frame,fore);
        bg.getBackgroundImage(back);
        cv::erode(fore,fore,cv::Mat());
        cv::dilate(fore,fore,cv::Mat());
        cv::findContours(fore,contours,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE);
        cv::drawContours(frame,contours,-1,cv::Scalar(0,0,255),2);
        cv::imshow("Frame",frame);

oVideoWriter.write(frame); 

        if(cv::waitKey(30) >= 0) break;
    }
    return 0;
}

Nessun commento:

Posta un commento

Sigaretta Elettronica

Avevo visto qualche tempo fa un documentario sulla RAI Svizzera sul carico inquinante delle sigarette elettroniche monouso Ne ho trovata una...