【OpenCV学习笔记】【算法学习】一(模板匹配)

  1. #include <stdio.h>   
  2. #include "cv.h"   
  3. #include "highgui.h"   
  4.    
  5. int main( int argc, char** argv )   
  6. {   
  7.     IplImage    *img;   
  8.     IplImage    *tpl;   
  9.     IplImage    *res;   
  10.     CvPoint        minloc, maxloc;   
  11.     double        minval, maxval;   
  12.     int            img_width, img_height;   
  13.     int            tpl_width, tpl_height;   
  14.     int            res_width, res_height;   
  15.    
  16.     /* check for arguments */   
  17.     if( argc < 3 ) {   
  18.         fprintf( stderr, "Usage: template_match <reference> <template>/n" );   
  19.         return 1;   
  20.     }   
  21.    
  22.     /* load reference image */   
  23.     img = cvLoadImage( argv[1], CV_LOAD_IMAGE_COLOR );   
  24.    
  25.     /* always check */   
  26.     if( img == 0 ) {   
  27.         fprintf( stderr, "Cannot load file %s!/n", argv[1] );   
  28.         return 1;    
  29.     }   
  30.    
  31.     /* load template image */   
  32.     tpl = cvLoadImage( argv[2], CV_LOAD_IMAGE_COLOR );   
  33.    
  34.     /* always check */   
  35.     if( tpl == 0 ) {   
  36.         fprintf( stderr, "Cannot load file %s!/n", argv[2] );   
  37.         return 1;   
  38.     }   
  39.    
  40.     /* get image's properties */   
  41.     img_width  = img->width;   
  42.     img_height = img->height;   
  43.     tpl_width  = tpl->width;   
  44.     tpl_height = tpl->height;   
  45.     res_width  = img_width - tpl_width + 1;   
  46.     res_height = img_height - tpl_height + 1;   
  47.    
  48.     /* create new image for template matching computation */   
  49.     res = cvCreateImage( cvSize( res_width, res_height ), IPL_DEPTH_32F, 1 );   
  50.    
  51.     /* choose template matching method to be used */   
  52.     cvMatchTemplate( img, tpl, res, CV_TM_SQDIFF );    
  53.     cvMinMaxLoc( res, &minval, &maxval, &minloc, &maxloc, 0 );   
  54.    
  55.     /* draw red rectangle */   
  56.     cvRectangle( img,    
  57.                  cvPoint( minloc.x, minloc.y ),    
  58.                  cvPoint( minloc.x + tpl_width, minloc.y + tpl_height ),   
  59.                  cvScalar( 0, 0, 255, 0 ), 1, 0, 0 );       
  60.    
  61.     /* display images */   
  62.     cvNamedWindow( "reference", CV_WINDOW_AUTOSIZE );   
  63.     cvNamedWindow( "template", CV_WINDOW_AUTOSIZE );   
  64.     cvShowImage( "reference", img );   
  65.     cvShowImage( "template", tpl );   
  66.    
  67.     /* wait until user press a key to exit */   
  68.     cvWaitKey( 0 );   
  69.    
  70.     /* free memory */   
  71.     cvDestroyWindow( "reference" );   
  72.     cvDestroyWindow( "template" );   
  73.     cvReleaseImage( &img );   
  74.     cvReleaseImage( &tpl );   
  75.     cvReleaseImage( &res );   
  76.    
  77.     return 0;   
  78. }   

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值