資源簡介
在網上找了好久都沒找到基于opencv的金字塔模板匹配算法代碼,我就自己把金字塔和模板匹配的代碼結合了一下,代碼基于opencv2.48.
代碼片段和文件信息
//-----------------------------------【頭文件包含部分】---------------------------------------??
//??????描述:包含程序所依賴的頭文件??
//----------------------------------------------------------------------------------------------???
#include???
#include???
#include?“opencv2/highgui/highgui.hpp“
#include?
#include?
#include?
#include?se.h>
#include?
//全局變量
//-----------------------------------【命名空間聲明部分】---------------------------------------??
//??????描述:包含程序所使用的命名空間??
//-----------------------------------------------------------------------------------------------???
using?namespace?cv;
using?namespace?std;
//全局變量
/*
Mat?srcImage;//源圖
Mat?tmpImage;//源模板
*/
Mat?dstImage1;//目標圖1
Mat?dstImage2;//目標圖2??
Mat?resultImg; //匹配結果圖像
Mat?srcImg; //原始圖像
Mat?templImg; //模板圖像
Mat?srcImage1;//源圖臨時變量
Mat?tmpImage1;//模板圖臨時變量
const?char*?imageWindow?=?“Source?Image“; //原始圖像顯示窗口
const?char*?resultWindow?=?“Result?Window“; //匹配結果圖像顯示窗口
int?matchMethod; //匹配方法index
int?maxTrackbar?=?5; //滑動條范圍(與匹配方法個數對應)
///?函數聲明?///
void?MatchingMethod(int?void*); //匹配函數
//-----------------------------------【main(?)函數】--------------------------------------------??
//??????描述:控制臺應用程序的入口函數,我們的程序從這里開始??
//-----------------------------------------------------------------------------------------------??
int?main()
{?????
double?i?kr;
//?加載原始圖像和模板圖像
srcImg?=?imread(“yuan.png“?1);
templImg?=?imread(“muban.png“?1);
srcImage1?=?srcImg;
tmpImage1?=?templImg;
//?創建顯示窗口
namedWindow(imageWindow?CV_WINDOW_AUTOSIZE);
namedWindow(resultWindow?CV_WINDOW_AUTOSIZE);
//?創建滑動條
char*?trackbarLabel?=
“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“;
//參數:滑動條名稱?顯示窗口名稱?匹配方法index?滑動條范圍?回調函數
createTrackbar(trackbarLabel?imageWindow?&matchMethod?maxTrackbar?MatchingMethod);
i=GetTickCount();
//進行向下取樣操作??
pyrDown(srcImage1?dstImage1?Size(srcImage1.cols?/?2?srcImage1.rows?/?2));
pyrDown(tmpImage1?dstImage2?Size(tmpImage1.cols?/?2?tmpImage1.rows?/?2));
MatchingMethod(0?0);
k?=?GetTickCount();
r?=?k?-?i;
// cout?<
waitKey(0);
return?0;
}
///?函數定義?///
void?MatchingMethod(int?void*) //匹配函數
{
Mat?imageROI;
//?深拷貝用于顯示
Mat?displayImg;
dstImage1.copyTo(displayImg);
//?創建匹配結果圖像,為每個模板位置存儲匹配結果
//?匹配結果圖像大小為:(W-w+1)*(H-h+1)
int?result_cols?=?dstImage1.cols?-?dstImage2.cols?+?1;
int?result_rows?=?dstImage1.rows?-?dstImage2.rows?+?1;
resultImg.create(result_cols?result_rows?CV_32FC1);
//?進行匹配并歸一化
matchTemplate(dstImage1?dstImage2?resultImg?matchMethod);
normalize(resultImg?resultImg?0?1?NORM_MINMAX?-1?Mat());
//?使用minMaxLoc找出最佳匹配
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件??????74240??2015-07-09?15:24??金字塔\Debug\金字塔.exe
?????文件?????728392??2015-07-09?15:24??金字塔\Debug\金字塔.ilk
?????文件????2173952??2015-07-09?15:24??金字塔\Debug\金字塔.pdb
?????文件??????????0??2015-07-09?15:27??金字塔\oleaut32.pdb\7B8A484D32F449DC99F82BF27DDD65E62\oleaut32.pd_
?????文件????1092608??2015-07-09?15:24??金字塔\金字塔\Debug\vc120.idb
?????文件????1585152??2015-07-09?15:24??金字塔\金字塔\Debug\vc120.pdb
?????文件?????262584??2015-07-09?15:24??金字塔\金字塔\Debug\源.obj
?????文件???????2209??2015-07-09?15:24??金字塔\金字塔\Debug\金字塔.log
?????文件????????580??2015-07-09?15:24??金字塔\金字塔\Debug\金字塔.tlog\cl.command.1.tlog
?????文件??????35486??2015-07-09?15:24??金字塔\金字塔\Debug\金字塔.tlog\CL.read.1.tlog
?????文件????????352??2015-07-09?15:24??金字塔\金字塔\Debug\金字塔.tlog\CL.write.1.tlog
?????文件???????2664??2015-07-09?15:24??金字塔\金字塔\Debug\金字塔.tlog\li
?????文件???????7478??2015-07-09?15:24??金字塔\金字塔\Debug\金字塔.tlog\li
?????文件????????336??2015-07-09?15:24??金字塔\金字塔\Debug\金字塔.tlog\li
?????文件????????165??2015-07-09?15:24??金字塔\金字塔\Debug\金字塔.tlog\金字塔.lastbuildstate
?????文件??????17996??2015-05-18?17:04??金字塔\金字塔\muban.png
?????文件?????132922??2015-05-18?17:03??金字塔\金字塔\yuan.png
?????文件???????4448??2015-06-14?22:59??金字塔\金字塔\源.cpp
?????文件???????4085??2015-05-18?11:06??金字塔\金字塔\金字塔.vcxproj
?????文件????????944??2015-05-18?11:06??金字塔\金字塔\金字塔.vcxproj.filters
?????文件???48955392??2015-07-09?15:28??金字塔\金字塔.sdf
?????文件????????973??2015-05-18?09:12??金字塔\金字塔.sln
????..A..H.?????26112??2015-07-09?15:28??金字塔\金字塔.v12.suo
?????目錄??????????0??2015-07-09?15:24??金字塔\金字塔\Debug\金字塔.tlog
?????目錄??????????0??2015-07-09?15:27??金字塔\oleaut32.pdb\7B8A484D32F449DC99F82BF27DDD65E62
?????目錄??????????0??2015-07-09?15:24??金字塔\金字塔\Debug
?????目錄??????????0??2015-05-18?17:26??金字塔\Debug
?????目錄??????????0??2015-07-09?15:27??金字塔\oleaut32.pdb
?????目錄??????????0??2015-07-09?15:31??金字塔\金字塔
?????目錄??????????0??2015-07-09?15:28??金字塔
............此處省略3個文件信息
評論
共有 條評論