opencv中cvMatchTemplate模板匹配

#include <stdio.h> 
#include "opencv\cv.h" 
#include "opencv2\highgui\highgui.hpp" 
 
//int main( int argc, char** argv ) 
int main()
{ 
	char *a="match3.jpg";
	char *b="match4.jpg";

    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; 
/*
    if( argc < 3 ) { 
        fprintf( stderr, "Usage: template_match <reference> <template>/n" ); 
        return 1; 
    } 
*/
//    img = cvLoadImage( argv[1], CV_LOAD_IMAGE_COLOR ); 
	img=cvLoadImage(a,CV_LOAD_IMAGE_COLOR);
    if( img == 0 ) { 
//        fprintf( stderr, "Cannot load file %s!/n", argv[1] ); 
		fprintf(stderr, "Cannot load file %s!/n", a);
        return 1;  
    } 
//    tpl = cvLoadImage( argv[2], CV_LOAD_IMAGE_COLOR ); 
	tpl = cvLoadImage( b, CV_LOAD_IMAGE_COLOR );
    if( tpl == 0 ) { 
//        fprintf( stderr, "Cannot load file %s!/n", argv[2] ); 
		fprintf(stderr, "Cannot load file %s!/n", b);
        return 1; 
    } 
 
    /* get image's properties */ 
    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; 
 
    /* create new image for template matching computation */ 
    res = cvCreateImage( cvSize( res_width, res_height ), IPL_DEPTH_32F, 1 ); 
 
    /* choose template matching method to be used */ 
    cvMatchTemplate( img, tpl, res, CV_TM_SQDIFF );  
    cvMinMaxLoc( res, &minval, &maxval, &minloc, &maxloc, 0 ); 
    cvRectangle( img,cvPoint( minloc.x, minloc.y ),cvPoint( minloc.x + tpl_width, minloc.y + tpl_height ), 
                 cvScalar( 0, 0, 255, 0 ), 1, 0, 0 );     

    cvNamedWindow( "reference", CV_WINDOW_AUTOSIZE ); 
    cvNamedWindow( "template", CV_WINDOW_AUTOSIZE ); 
    cvShowImage( "reference", img ); 
    cvShowImage( "template", tpl ); 
    cvWaitKey( 0 ); 
    cvDestroyWindow( "reference" ); 
    cvDestroyWindow( "template" ); 
    cvReleaseImage( &img ); 
    cvReleaseImage( &tpl ); 
    cvReleaseImage( &res ); 
 
    return 0; 
} 

你可能感兴趣的:(opencv)