opencv模版匹配

來源:互聯網
上載者:User

n久之前在網上搜的的代碼···想儲存一份···· thx to the author

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;/// 全域變數Mat img; Mat templ; Mat result;char* image_window = "Source Image";char* result_window = "Result window";char *dst_window = "Dest_windows";int match_method;int max_Trackbar = 5;/// 函式宣告void MatchingMethod( int, void* );/** @主函數 */int main( int argc, char** argv ){  /// 載入原映像和模板塊  img = imread( "d:\\me.jpg");  templ = imread("d:\\1.bmp" );/*IplImage *img_src = cvLoadImage("d:\\me.jpg", 1);IplImage *img_dst = cvLoadImage("d:\\eye.jpg", 1);img(img_src, 0);img(img_dst, 0);*/  /// 建立視窗   namedWindow( image_window, CV_WINDOW_AUTOSIZE );   namedWindow( result_window, CV_WINDOW_AUTOSIZE );   namedWindow( dst_window, CV_WINDOW_AUTOSIZE );  /// 建立滑動條  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 );  MatchingMethod( 0, 0 );  waitKey(0);  return 0;}/** * @函數 MatchingMethod * @簡單的滑動條回呼函數 */void MatchingMethod( int, void* ){  /// 將被顯示的原映像  Mat img_display;  img.copyTo( img_display );  /// 建立輸出結果的矩陣  int result_cols =  img.cols - templ.cols + 1;  int result_rows = img.rows - templ.rows + 1;  result.create( result_cols, result_rows, CV_32FC1 );  /// 進行匹配和標準化  matchTemplate( img, templ, result, match_method );  normalize( result, result, 0, 1, NORM_MINMAX, -1, Mat() );  /// 通過函數 minMaxLoc 定位最匹配的位置  double minVal; double maxVal; Point minLoc; Point maxLoc;  Point matchLoc;  minMaxLoc( result, &minVal, &maxVal, &minLoc, &maxLoc, Mat() );  /// 對於方法 SQDIFF 和 SQDIFF_NORMED, 越小的數值代表更高的匹配結果. 而對於其他方法, 數值越大匹配越好  if( match_method  == CV_TM_SQDIFF || match_method == CV_TM_SQDIFF_NORMED )    { matchLoc = minLoc; }  else    { matchLoc = maxLoc; }  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 );  imshow( dst_window, templ );  return;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.