Codice:
#include
#include "cv.h"
#include "highgui.h"
main( )
{
IplImage *img;
IplImage *tpl;
IplImage *res;
CvPoint minloc, maxloc;
double minval, maxval;
int img_width, img_height;
int tpl_width, tpl_height;
int res_width, res_height;
img = cvLoadImage( argv[1], CV_LOAD_IMAGE_COLOR );
tpl = cvLoadImage( argv[2], CV_LOAD_IMAGE_COLOR );
/* Ricavo proprietà dell'immagine */
img_width = img->width;
img_height = img->height;
tpl_width = tpl->width;
tpl_height = tpl->height;
res_width = img_width - tpl_width + 1;
res_height = img_height - tpl_height + 1;
//Creo una nuova immagine per effettuare il matching
res = cvCreateImage( cvSize( res_width, res_height ), IPL_DEPTH_32F, 1 );
cvMatchTemplate( img, tpl, res, CV_TM_SQDIFF );
cvMinMaxLoc( res, &minval, &maxval, &minloc, &maxloc, 0 );
/* Disegno il rettangolo */
cvRectangle( img,
cvPoint( minloc.x, minloc.y ),
cvPoint( minloc.x + tpl_width, minloc.y + tpl_height ),
cvScalar( 0, 255, 0, 0 ), 3, 0, 0 );
/* Visualizzo le immagini */
cvNamedWindow( "Originale", CV_WINDOW_AUTOSIZE );
cvNamedWindow( "Da riconoscere", CV_WINDOW_AUTOSIZE );
cvShowImage( "Originale", img );
cvShowImage( "Da riconoscere", tpl );
/* Aspetto ke venga premuto qualsiasi tasto */
cvWaitKey( 0 );
/* Libero la memoria */
cvDestroyWindow( "Originale" );
cvDestroyWindow( "Da riconoscere" );
cvReleaseImage( &img );
cvReleaseImage( &tpl );
cvReleaseImage( &res );
}
Immagine:
Nessun commento:
Posta un commento