OpenCV Tutorials —— Template Matching

模板匹配

从源图像中发掘目标图像

将目标图像块逐像素滑动,然后度量此区域的源图像块和目标图像块的匹配程度

匹配都最高的像素位置作为最终定位

 

void matchTemplate(InputArray image, InputArray templ, OutputArray result, int method)

度量手段 —— 相关性

  1. method=CV_TM_SQDIFF

  2. method=CV_TM_SQDIFF_NORMED

  3. method=CV_TM_CCORR

  4. method=CV_TM_CCORR_NORMED

  5. method=CV_TM_CCOEFF

    where

  6. method=CV_TM_CCOEFF_NORMED

 

Code

#include "stdafx.h"

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>

using namespace std;
using namespace cv;

/// Global Variables
Mat img; Mat templ; Mat result;
char* image_window = "Source Image";
char* result_window = "Result window";

int match_method;
int max_Trackbar = 5;

/// Function Headers
void MatchingMethod( int, void* );

/** @function main */
int main( int argc, char** argv )
{
  /// Load image and template
  img = imread( "dashu.jpg", 1 );
  templ = imread( "temp.jpg", 1 );

  /// Create windows
  namedWindow( image_window, CV_WINDOW_AUTOSIZE );
  namedWindow( result_window, CV_WINDOW_AUTOSIZE );

  /// Create Trackbar
  char* trackbar_label = "Method: \n 0: SQDIFF \n 1: SQDIFF NORMED \n 2: TM CCORR \n 3: TM CCORR NORMED \n 4: TM COEFF \n 5: TM COEFF NORMED";
  createTrackbar( trackbar_label, image_window, &match_method, max_Trackbar, MatchingMethod );	// opencv 没有按钮,拿滑动条将就

  MatchingMethod( 0, 0 );

  waitKey(0);
  return 0;
}

/**
 * @function MatchingMethod
 * @brief Trackbar callback
 */
void MatchingMethod( int, void* )
{
  /// Source image to display
  Mat img_display;
  img.copyTo( img_display );

  /// Create the result matrix
  int result_cols =  img.cols - templ.cols + 1;
  int result_rows = img.rows - templ.rows + 1;

  result.create( result_cols, result_rows, CV_32FC1 );

  /// Do the Matching and Normalize
  matchTemplate( img, templ, result, match_method );
  normalize( result, result, 0, 1, NORM_MINMAX, -1, Mat() );	// 归一化

  /// Localizing the best match with minMaxLoc
  double minVal; double maxVal; Point minLoc; Point maxLoc;
  Point matchLoc;

  minMaxLoc( result, &minVal, &maxVal, &minLoc, &maxLoc, Mat() );	// 返回最小值最大值点位置

  /// For SQDIFF and SQDIFF_NORMED, the best matches are lower values. For all the other methods, the higher the better
  if( match_method  == CV_TM_SQDIFF || match_method == CV_TM_SQDIFF_NORMED )
    { matchLoc = minLoc; }
  else
    { matchLoc = maxLoc; }

  /// Show me what you got
  rectangle( img_display, matchLoc, Point( matchLoc.x + templ.cols , matchLoc.y + templ.rows ), Scalar::all(0), 2, 8, 0 );
  rectangle( result, matchLoc, Point( matchLoc.x + templ.cols , matchLoc.y + templ.rows ), Scalar::all(0), 2, 8, 0 );

  imshow( image_window, img_display );
  imshow( result_window, result );

  return;
}
时间: 2024-11-05 19:45:49

OpenCV Tutorials —— Template Matching的相关文章

OpenCV Tutorials &mdash;&mdash; Feature Matching with FLANN

Extractors of keypoint descriptors in OpenCV have wrappers with a common interface that enables you to easily switch between different algorithms solving the same problem.   DescriptorExtractor::compute Computes the descriptors for a set of keypoints

学习opencv tutorials

1.opencv里头动态库和静态库的区别 lib是动态库,staticlib是静态库. 这是opencv tutorials中对动态库和静态库的说明.动态库是在runtime时候才load的库文件.而静态库文件会在你build的时候build-in inside your exe file.优点是可以避免误删,缺点是应用程序变大,加载时间也会变长. 2.  Visual Studio中solution和project的关系 在VS中,一个solution中可以包含多个project. 3.  两

opencv笔记(二十二)——模板匹配 template matching

模板匹配就是在给定一幅图像和一幅模板(一般模板比图像的尺寸小很多)的情况下,找到这个图像中最最相似于模板的位置,比如 第一幅是给定的图片,第二幅是模板,第三幅就是搜索到的匹配的位置. 这个搜索的过程,我没有在源码中求索,但是根据tutorial,应该是采用sliding window的方法,利用滑动的窗口,逐像素进行匹配.这个逐像素的意思,就是锚定某个像素之后,在这个像素的右边和下方,以template的宽度和高度的距离的范围内,进行模板与图像的相似度计算. 这种方法和行人检测中常用的方法类似.

OpenCV Tutorials &mdash;&mdash; Hough Line Transform

霍夫直线变换 -- 用于检测图像中的直线 利用图像空间和Hough参数空间的点--直线对偶性,把图像空间中的检测问题转换到参数空间,通过在参数空间进行简单的累加统计,然后在Hough参数空间中寻找累加器峰值的方法检测直线 Standard and Probabilistic Hough Line Transform OpenCV implements two kind of Hough Line Transforms: The Standard Hough Transform It consis

OpenCV Tutorials &mdash;&mdash; Creating a video with OpenCV

写video 需要用到 VideoWriter  视频文件可看作一个容器 视频的类型由视频文件的后缀名来指定   Due to this OpenCV for video containers supports only the avi extension, its first version. A direct limitation of this is that you cannot save a video file larger than 2 GB. Furthermore you ca

OpenCV Tutorials &mdash;&mdash; Image Moments

图像矩 Moments moments(InputArray array, bool binaryImage=false ) Parameters: array – Raster image (single-channel, 8-bit or floating-point 2D array) or an array ( or ) of 2D points (Point or Point2f ). binaryImage – If it is true, all non-zero image pi

OpenCV Tutorials &mdash;&mdash; Hough Circle Transform

Hough 圆变换 和 Hough 直线变换原理相同,只是参数空间不同 : In the line detection case, a line was defined by two parameters . In the circle case, we need three parameters to define a circle: where define the center position (gree point) and is the radius, which allows us

OpenCV Tutorials &mdash;&mdash; Sobel Derivatives

图像边缘 -- 像素灰度值变换剧烈的点 You can easily notice that in an edge, the pixel intensity changes in a notorious way. A good way to expresschanges is by using derivatives. A high change in gradient indicates a major change in the image.   To be more graphical,

OpenCV Tutorials &mdash;&mdash; Making your own linear filters

kernel A kernel is essentially a fixed size array of numerical coefficeints along with an anchor point in that array, which is tipically located at the center. The value of the convolution is calculated in the following way: 1,Place the kernel anchor