#Region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_UseX64=y #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** #include ;~ Sources: ;~ https://docs.opencv.org/4.5.1/d8/ded/samples_2cpp_2tutorial_code_2Histograms_Matching_2MatchTemplate_Demo_8cpp-example.html#a16 ;~ https://stackoverflow.com/a/28647930 _OpenCV_DLLOpen("libemgucv-windesktop-4.5.1.4349\libs\x64\cvextern.dll") Local $bUseMask = True Local $sImageWindow = "Source Image" Local $sResultWindow = "Result window" Local $matImg = _cveImread("lena_tmpl.jpg", $CV_IMREAD_UNCHANGED) Local $matTempl = _cveImread("tmpl.png", $CV_IMREAD_COLOR) Local $matTemplMask = _cveImread("mask.png", $CV_IMREAD_GRAYSCALE) Local $width = _cveMatGetWidth($matImg) Local $height = _cveMatGetHeight($matImg) Local $w = _cveMatGetWidth($matTempl) Local $h = _cveMatGetHeight($matTempl) Local $tRedColor = _cvRGB(255, 0, 0) Local $tGreenColor = _cvRGB(0, 255, 0) Local $tBlackColor = _cvScalar(0) Local $tMatchRect = _cvRect(0, 0, $w, $h) Local $tMatchMethod = _cvNativeType("int", $CV_TM_CCORR_NORMED) ; $CV_TM_SQDIFF; $CV_TM_SQDIFF_NORMED; $CV_TM_CCOEFF_NORMED; $CV_TM_CCORR_NORMED Local $threshold = 0.9999 _cveNamedWindow($sImageWindow, $CV_WINDOW_AUTOSIZE) _cveNamedWindow($sResultWindow, $CV_WINDOW_AUTOSIZE) _MatchingMethod() _cveWaitKey(0) _cveDestroyWindow($sResultWindow) _cveDestroyWindow($sImageWindow) $tMatchMethod = 0 $tBlackColor = 0 $tGreenColor = 0 $tRedColor = 0 $tMatchRect = 0 _cveMatRelease($matTemplMask) _cveMatRelease($matTempl) _cveMatRelease($matImg) _Opencv_DLLClose() Func _MatchingMethod() Local $matEmpty = _cveMatCreate() Local $matImgDisplay = _cveMatCreate() _cveMatCopyToMat($matImg, $matImgDisplay, $matEmpty) Local $result_cols = $width - $w + 1 Local $result_rows = $height - $h + 1 Local $matResult = _cveMatCreate() _cveMatCreateData($matResult, $result_rows, $result_cols, $CV_32FC1) Local $iMatchMethod = DllStructGetData($tMatchMethod, 1) Local $bMethodAcceptsMask = $CV_TM_SQDIFF == $iMatchMethod Or $iMatchMethod == $CV_TM_CCORR_NORMED If $bUseMask And $bMethodAcceptsMask Then _cveMatchTemplateMat($matImg, $matTempl, $matResult, $iMatchMethod, $matTemplMask) Else _cveMatchTemplateMat($matImg, $matTempl, $matResult, $iMatchMethod, $matEmpty) EndIf _cveNormalizeMat($matResult, $matResult, 0, 1, $CV_NORM_MINMAX, -1, $matEmpty) Local $tMinVal = DllStructCreate("double min;") Local $tMaxVal = DllStructCreate("double max;") Local $tMinLoc = DllStructCreate($tagCvPoint) Local $tMaxLoc = DllStructCreate($tagCvPoint) Local $tMatchLoc Local $limit = 50 Local $score = 0 Local $fVisited ; For SQDIFF and SQDIFF_NORMED, the best matches are lower values. For all the other methods, the higher the better If ($iMatchMethod == $CV_TM_SQDIFF Or $iMatchMethod == $CV_TM_SQDIFF_NORMED) Then $fVisited = 1.0 Else $fVisited = 0.0 EndIf While 1 ;use infinite loop since ExitLoop will get called If $limit == 0 Then ConsoleWrite("There are too much matches" & @CRLF) ExitLoop EndIf $limit = $limit - 1 _cveMinMaxLocMat($matResult, $tMinVal, $tMaxVal, $tMinLoc, $tMaxLoc, $matEmpty) ; For SQDIFF and SQDIFF_NORMED, the best matches are lower values. For all the other methods, the higher the better If ($iMatchMethod == $CV_TM_SQDIFF Or $iMatchMethod == $CV_TM_SQDIFF_NORMED) Then $tMatchLoc = $tMinLoc $score = 1 - DllStructGetData($tMinVal, 1) _cveMatSetAt("float", $matResult, $tMinLoc, 1.0) _cveMatSetAt("float", $matResult, $tMaxLoc, 1.0) Else $tMatchLoc = $tMaxLoc $score = DllStructGetData($tMaxVal, 1) _cveMatSetAt("float", $matResult, $tMinLoc, 0.0) _cveMatSetAt("float", $matResult, $tMaxLoc, 0.0) EndIf If $score < $threshold Then ; MsgBox($MB_SYSTEMMODAL, "", "There are no more suitable matches") ConsoleWrite("There are no more suitable matches : " & $score & @CRLF) ExitLoop EndIf _DebugMsg("score " & $score) $tMatchRect.x = $tMatchLoc.x $tMatchRect.y = $tMatchLoc.y _DebugMsg("found a match at (" & $tMatchLoc.x & ", " & $tMatchLoc.y & ")") ; Draw a red rectangle around the matched position _cveRectangleMat($matImgDisplay, $tMatchRect, $tRedColor, 2, $CV_LINE_8, 0) ; Draw a black rectangle around the matched position _cveRectangleMat($matResult, $tMatchRect, $tBlackColor, 2, $CV_LINE_8, 0) ; Mark as visited _cveMatSetAt("float", $matResult, $tMinLoc, $fVisited) _cveMatSetAt("float", $matResult, $tMaxLoc, $fVisited) WEnd _cveImshowMat($sImageWindow, $matImgDisplay) _cveImshowMat($sResultWindow, $matResult) $tMinVal = 0 $tMaxVal = 0 $tMinLoc = 0 $tMaxLoc = 0 $tMatchLoc = 0 _cveMatRelease($matResult) _cveMatRelease($matImgDisplay) _cveMatRelease($matEmpty) EndFunc ;==>_MatchingMethod