"Orang berilmu binasa kecuali orang yang mengamalkan ilmunya. Orang yang mengamalkan ilmunya binasa kecuali orang yang ikhlas". (Ibnu Qudamah)

Selasa, 28 Juli 2015

Frame Difference background substraction



Frame Difference background substraction adalah metode untuk mengetahui perbedaan antara dua frame dimana background didapat dari frame sebelumnya pada video streaming dan masukan frame didapat dari gambar sekarang dari video streaming. Contoh ilustrasi dari frame difference background substraction seperti berikut ini.


Berikut ini adalah program dari frame difference background substraction dan video hasil static background substraction.


PROGRAM
#include <stdlib.h>
#include <string>
#include <cstring>
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include "cv.h"
#include "highgui.h"

using namespace std;
int thresh = 100;
int max_thresh = 255;
IplImage *image=0, *frameTime1=0, *frameTime2=0, *frameForeground=0, *img1=0, *img2=0;

int main(int argc, char* argv[])
{
    CvCapture* capture = cvCaptureFromCAM(1);
    if ( !capture )
    {
        fprintf( stderr, "ERROR: capture is NULL \n" );
        getchar();
        return -1;
    }
    // Create a window in which the captured images will be presented
    cvNamedWindow( "input image", CV_WINDOW_AUTOSIZE );
    cvNamedWindow( "difference", CV_WINDOW_AUTOSIZE );
    while (1) {

            //printf("proses while\n");

           IplImage* img = 0;   //every time create/initialize an image (which name is frame) to process
            img = cvQueryFrame( capture );   //grabs and returns a frame from a camera input

            if( !img )    //if there is no frame exit the while(1)
                break;

            if( !image )    //if there is no image, do the followings
            {              //allocate all the buffers
                image = cvCreateImage( cvGetSize(img), 8, 3 );
                frameTime1 = cvCreateImage( cvGetSize(img), 8, 1 );
                frameTime2 = cvCreateImage( cvGetSize(img), 8, 1 );
                frameForeground = cvCreateImage( cvGetSize(img), 8, 1 );
                img1 = cvCreateImage( cvGetSize(img), 8, 1 );
                img2 = cvCreateImage( cvGetSize(img), 8, 1 );
            }
            cvCopy( img, image, 0 );
            cvCvtColor( image, img1, CV_BGR2GRAY );
            cvCopy( img1, frameTime1, 0 );    //currently frame in grayscale

            cvAbsDiff(frameTime1, frameTime2,frameForeground);
            cvThreshold(frameForeground,frameForeground,15,255,CV_THRESH_BINARY);
            cvErode(frameForeground,frameForeground,0,1);
            cvDilate(frameForeground,frameForeground,0,1);
            cvDilate(frameForeground,frameForeground,0,1);
            cvErode(frameForeground,frameForeground,0,1);
            cvCopy( frameTime1, frameTime2, 0 );

           cvShowImage( "input image", img);
            cvShowImage( "difference", canny1);

            if ( (cvWaitKey(10) & 255) == 27 ) break;
    }
    //deallocate windows
    cvDestroyWindow( "input image" );
    cvDestroyWindow( "difference" );
    //deallocate images
    cvReleaseCapture (&capture );
    return 0;
}
 
 VIDEO
  


Sumber:
http://theembeddedsystems.blogspot.com/2011/05/background-subtraction-using-opencv.html
http://www.academia.edu/4085596/MOTION_DETECTION_USING_OPENCV_WITH_BACKGROUND_SUBTRACTION_AND_FRAME_DIFFERENCING_TECHNIQUE
http://mac-opencv-projects.blogspot.com/2014/02/static-background-subtraction-using.html
https://en.wikipedia.org/wiki/Background_subtraction#Using_frame_differencing
https://www.behance.net/gallery/3943089/BGS-Library-A-Background-Subtraction-Library

Tidak ada komentar: