【OpenCV学习】模板匹配

作者:gnuhpc
出处:http://www.cnblogs.com/gnuhpc/

#include <stdio.h> 
#include "cv.h" 
#include "highgui.h" 
 
int main( int argc, char** argv ) 
{ 
    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; 
 
    /* check for arguments */ 
    if( argc < 3 ) { 
        fprintf( stderr, "Usage: template_match <reference> <template>/n" ); 
        return 1; 
    } 
 
    /* load reference image */ 
    img = cvLoadImage( argv[1], CV_LOAD_IMAGE_COLOR ); 
 
    /* always check */ 
    if( img == 0 ) { 
        fprintf( stderr, "Cannot load file %s!/n", argv[1] ); 
        return 1;  
    } 
 
    /* load template image */ 
    tpl = cvLoadImage( argv[2], CV_LOAD_IMAGE_COLOR ); 
 
    /* always check */ 
    if( tpl == 0 ) { 
        fprintf( stderr, "Cannot load file %s!/n", argv[2] ); 
        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 ); 
 
    /* draw red rectangle */ 
    cvRectangle( img,  
                 cvPoint( minloc.x, minloc.y ),  
                 cvPoint( minloc.x + tpl_width, minloc.y + tpl_height ), 
                 cvScalar( 0, 0, 255, 0 ), 1, 0, 0 );     
 
    /* display images */ 
    cvNamedWindow( "reference", CV_WINDOW_AUTOSIZE ); 
    cvNamedWindow( "template", CV_WINDOW_AUTOSIZE ); 
    cvShowImage( "reference", img ); 
    cvShowImage( "template", tpl ); 
 
    /* wait until user press a key to exit */ 
    cvWaitKey( 0 ); 
 
    /* free memory */ 
    cvDestroyWindow( "reference" ); 
    cvDestroyWindow( "template" ); 
    cvReleaseImage( &img ); 
    cvReleaseImage( &tpl ); 
    cvReleaseImage( &res ); 
 
    return 0; 
} 

作者:gnuhpc
出处:http://www.cnblogs.com/gnuhpc/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值