• opencv template matching


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

     
    /* check for arguments */
     
    if(argc <3){
        fprintf
    (stderr,"Usage: template_match <reference> <template>\n");
       
    return1;
     
    }

     
    /* load reference image */
      img
    = cvLoadImage(argv[1], CV_LOAD_IMAGE_COLOR);

     
    /* always check */
     
    if(img ==0){
        printf
    ("Cannot load file %s!\n", argv[1]);
       
    return1;
     
    }

     
    /* load template image */
      tpl
    = cvLoadImage(argv[2], CV_LOAD_IMAGE_COLOR);

     
    /* always check */
     
    if(tpl ==0){
        printf
    ("Cannot load file %s!\n", argv[2]);
       
    return1;
     
    }

     
    CvSize size = cvSize(
        img
    ->width  - tpl->width  +1,
        img
    ->height - tpl->height +1
     
    );

     
    /* create new image for template matching computation */
      res
    = cvCreateImage(size, IPL_DEPTH_32F,1);

     
    /* choose template matching method to be used */
      cvMatchTemplate
    (img, tpl, res, CV_TM_SQDIFF);
     
    /*cvMatchTemplate(img, tpl, res, CV_TM_SQDIFF_NORMED);
      cvMatchTemplate(img, tpl, res, CV_TM_CCORR);
      cvMatchTemplate(img, tpl, res, CV_TM_CCORR_NORMED);
      cvMatchTemplate(img, tpl, res, CV_TM_CCOEFF);
      cvMatchTemplate(img, tpl, res, CV_TM_CCOEFF_NORMED);*/


      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),
             CV_RGB
    (255,0,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);

     
    return0;
    }
  • 相关阅读:
    haproxy 基于 cookie 的会话保持
    haproxy 透明代理
    haproxy tcp keepalive
    haproxy 支持 websocket
    python 中给文件加锁
    使用python生成二维码
    python中的uuid简介
    django建表报错
    pip安装第三方包PIL失败
    python获取mac地址的方法
  • 原文地址:https://www.cnblogs.com/lavieenrose/p/2390735.html
Copyright © 2020-2023  润新知