Static background substraction
adalah metode untuk mengetahui perbedaan antara dua frame dimana frame pertama
digunakan sebagai background dan frame kedua sebagai masukan dapat berupa frame
tetap atau frame yang berubah ubah.Contoh ilustrasi dari static
background substraction seperti berikut ini.
Berikut ini adalah program dari
static 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 = 50;
int max_thresh = 300;
int main(int argc, char* argv[])
{
CvCapture* capture =
cvCaptureFromCAM(0);
if ( !capture ) {
fprintf( stderr,
"ERROR: capture is NULL \n" );
getchar();
return -1;
}
// Create a window in which
the captured images will be presented
cvNamedWindow(
"reference", CV_WINDOW_AUTOSIZE );
// Show the image captured
from the camera in the window and repeat
cvNamedWindow(
"background", CV_WINDOW_AUTOSIZE );
cvNamedWindow(
"difference", CV_WINDOW_AUTOSIZE );
cvNamedWindow( "canny
edge", CV_WINDOW_AUTOSIZE );
cvNamedWindow(
"res", CV_WINDOW_AUTOSIZE );
//load background template
IplImage* tpl =
cvLoadImage("background.jpg");
IplImage* gtpl =
cvCreateImage(cvGetSize(tpl), IPL_DEPTH_8U, 1);
while (1) {
IplImage *img= cvQueryFrame(capture);
if(!img) break;
IplImage* gIn =
cvCreateImage(cvGetSize(img), IPL_DEPTH_8U, 1);
IplImage*
differenceImg = cvCreateImage(cvGetSize(img), IPL_DEPTH_8U, 1);
cvCvtColor (tpl,
gtpl, CV_BGR2GRAY );
cvCvtColor (img,
gIn, CV_BGR2GRAY );
//3. Cari perbedaan
background
//Minus the current
frame from the moving average.
cvAbsDiff(gtpl,gIn,differenceImg);
//treshold
cvCreateTrackbar(
" Threshold input:", "difference", &thresh,
max_thresh, NULL );
cvThreshold(differenceImg,
differenceImg,thresh, 255, CV_THRESH_BINARY ); //nilai trail and error
//cvAdaptiveThreshold(differenceImg,differenceImg,255,CV_ADAPTIVE_THRESH_GAUSSIAN_C,CV_THRESH_BINARY,3,5);
//mode :
CV_ADAPTIVE_THRESH_GAUSSIAN_C atau CV_ADAPTIVE_THRESH_MEAN_C
//4. Gunakan metode
canny edge
IplImage* cannyImg =
cvCreateImage(cvGetSize(gIn), IPL_DEPTH_8U, 1);
//5. Filter
cvCanny(differenceImg,
cannyImg, 50, 150, 3);
cvDilate(cannyImg,cannyImg,NULL,1);
cvErode(cannyImg,cannyImg,NULL,1);
cvShowImage( "reference",
img);
cvShowImage(
"background", tpl);
cvShowImage(
"difference", cannyImg);
if ( (cvWaitKey(10)
& 255) == 27 ) break;
}
//deallocate windows
cvDestroyWindow(
"reference" );
cvDestroyWindow(
"background" );
cvDestroyWindow(
"difference" );
//deallocate images
cvReleaseImage(&tpl);
cvReleaseCapture (
&capture );
return 0;
}
|
VIDEO
Tidak ada komentar:
Posting Komentar