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

Selasa, 28 Juli 2015

Background Substraction dan Template Matching

Image result for example template matching
Template matching adalah salah satu teknik dalam pengolahan citra digital yang berfungsi untuk mencocokan tiap-tiap bagian dari suatu citra dengan citra yang menjadi sampel (template). Prinsip metode ini adalah membandingkan antara citra streaming yang akan dikenali dengan citra sampel. Citra  objek yang  akan dikenali mempunyai tingkat kemiripan sendiri terhadap masing-masing citra sampel. Pengenalan dilakukan dengan melihat nilai tingkat kemiripan tertinggi dan nilai batas ambang pengenalan dari citra  objek tersebut. Bila nilai tingkat kemiripan berada di bawah nilai batas ambang maka  citra  objek tersebut dikategorikan sebagai objek tidak dikenal. Metode ini dapat anda pelajari dengan lebih baik pada e-book OpenCV yang telah saya sarankan pada postingan sebelumnya. Berikut ini adalah program dan video template matching.

#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( "template 1", CV_WINDOW_AUTOSIZE );
    cvNamedWindow( "difference", CV_WINDOW_AUTOSIZE );

    //template  1
    IplImage *tpl1 = cvLoadImage("Depan.png",CV_LOAD_IMAGE_COLOR);
    IplImage *gray_tpl1 = cvCreateImage(cvGetSize(tpl1), IPL_DEPTH_8U, 1);
    cvCvtColor (tpl1, gray_tpl1, CV_BGR2GRAY );

    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 );


            //-------------------------------Proses template 1-----------------------------
             unsigned char orang1=1;
            //4. Gunakan metode canny edge
            IplImage* canny1 = cvCreateImage(cvGetSize(frameForeground), IPL_DEPTH_8U, 1);
            cvCopy(frameForeground, canny1,0);

            //6.Lakukan proses match
            int img1_width  = canny1->width;
            int img1_height = canny1->height;
            int tpl1_width  = tpl1->width;
            int tpl1_height = tpl1->height;
            int res1_width  = img1_width - tpl1_width + 1;
            int res1_height = img1_height - tpl1_height + 1;

            IplImage *res1 = cvCreateImage(cvSize(res1_width, res1_height), IPL_DEPTH_32F, 1);

            //Pemilihan Methode Matching
            int match_method1;
            match_method1 = CV_TM_CCORR_NORMED;         
            // perform template matching
            cvMatchTemplate(canny1, gray_tpl1, res1, match_method1);

            //find best matches location
            CvPoint minloc1, maxloc1, matchloc1;
            double minval1, maxval1;
            cvMinMaxLoc( res1, &minval1, &maxval1, &minloc1, &maxloc1, 0 );

            if( match_method1 == CV_TM_SQDIFF || match_method1 == CV_TM_SQDIFF_NORMED )
            {
                matchloc1 = minloc1;
            }
            else
            { matchloc1 = maxloc1; }

            double people1=maxval1;

            if (people1>0.55 && orang1==1 )
            {
                //draw rectangle
                cvRectangle( img,cvPoint( matchloc1.x, matchloc1.y ),
                        cvPoint( matchloc1.x + tpl1_width, matchloc1.y + tpl1_height ),
                        cvScalar( 0, 0, 255, 0 ), 1, 0, 0 );

                //Program untuk menampilkan text
                CvFont font;
                cvInitFont( &font, CV_FONT_HERSHEY_SIMPLEX, 1.0, 1.0, 0, 1, 8);
               cvPutText(img,"template1",cvPoint(maxloc1.x,maxloc1.y+30),&font,CV_RGB(255,255,51));
                printf("depan= %f \n", maxval1);
            cvSaveImage("save.jpg",img);
            }         
            cvShowImage( "input image", img);
            cvShowImage( "template 1", tpl1);
            cvShowImage( "difference", canny1);

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

Tidak ada komentar: