#include <iostream>
bool use_mask;
const char* image_window = "Source Image";
const char* result_window = "Result window";
int match_method;
int max_Trackbar = 5;
void MatchingMethod( int, void* );
const char* keys =
"{ help h| | 列印幫助資訊. }"
"{ @input1 | Template_Matching_Original_Image.jpg | image_name }"
"{ @input2 | Template_Matching_Template_Image.jpg | template_name }"
"{ @input3 | | mask_name }";
int main(
int argc,
char** argv )
{
samples::addSamplesDataSearchSubDirectory( "doc/tutorials/imgproc/histograms/template_matching/images" );
img = imread( samples::findFile( parser.get<
String>(
"@input1") ) );
templ = imread( samples::findFile( parser.get<
String>(
"@input2") ), IMREAD_COLOR );
if(argc > 3) {
use_mask = true;
mask = imread(samples::findFile( parser.get<
String>(
"@input3") ), IMREAD_COLOR );
}
if(img.
empty() || templ.
empty() || (use_mask && mask.empty()))
{
cout << "無法讀取其中一個影像" << endl;
return EXIT_FAILURE;
}
const char* trackbar_label = "方法: \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 );
return EXIT_SUCCESS;
}
void MatchingMethod( int, void* )
{
int result_cols = img.
cols - templ.
cols + 1;
int result_rows = img.
rows - templ.
rows + 1;
if (use_mask && method_accepts_mask)
else
double minVal;
double maxVal;
Point minLoc;
Point maxLoc;
minMaxLoc( result, &minVal, &maxVal, &minLoc, &maxLoc,
Mat() );
if( match_method == TM_SQDIFF || match_method == 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 );
imshow( image_window, img_display );
imshow( result_window, result );
return;
}
如果陣列沒有元素,則返回 true。
int64_t int64
void copyTo(OutputArray m) const
將矩陣複製到另一個矩陣。
void create(int rows, int cols, int type)
如果需要,分配新的陣列資料。
cv::getTickFrequency
double getTickFrequency()
int rows
行數和列數,或者矩陣具有 2 個以上維度時為 (-1, -1)
定義 mat.hpp:2165
_Tp y
點的 y 座標
定義 types.hpp:202
_Tp x
點的 x 座標
定義 types.hpp:201
void normalize(InputArray src, InputOutputArray dst, double alpha=1, double beta=0, int norm_type=NORM_L2, int dtype=-1, InputArray mask=noArray())
對陣列的範數或值範圍進行歸一化。
void minMaxLoc(InputArray src, double *minVal, double *maxVal=0, Point *minLoc=0, Point *maxLoc=0, InputArray mask=noArray())
查詢陣列中的全域性最小值和最大值。
std::string String
定義 cvstd.hpp:151
#define CV_32FC1
定義 interface.h:118
void imshow(const String &winname, InputArray mat)
在指定視窗中顯示影像。
int waitKey(int delay=0)
等待按鍵按下。
void namedWindow(const String &winname, int flags=WINDOW_AUTOSIZE)
建立視窗。
int createTrackbar(const String &trackbarname, const String &winname, int *value, int count, TrackbarCallback onChange=0, void *userdata=0)
建立滑動條並將其附加到指定視窗。
void rectangle(InputOutputArray img, Point pt1, Point pt2, const Scalar &color, int thickness=1, int lineType=LINE_8, int shift=0)
繪製一個簡單、粗或填充的矩形。
void matchTemplate(InputArray image, InputArray templ, OutputArray result, int method, InputArray mask=noArray())
將模板與重疊影像區域進行比較。
@ TM_SQDIFF
定義 imgproc.hpp:3866
@ TM_CCORR_NORMED
定義 imgproc.hpp:3881
int main(int argc, char *argv[])
定義 highgui_qt.cpp:3