#Region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_UseX64=y #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** Opt("MustDeclareVars", 1) ; #include #include "emgucv-autoit-bindings\cve_extra.au3" _OpenCV_DLLOpen("libemgucv-windesktop-4.5.3.4721\libs\x64\cvextern.dll") Local $img_matches = _cveMatCreate() Local $img_obj = _cveImreadAndCheck("Pic4cards\queen_spade.JPG", $CV_IMREAD_COLOR) ; query image - object Local $gray_obj = _cveMatCreate() _cveCvtColorMat($img_obj, $gray_obj, $CV_COLOR_BGR2GRAY) Local $img_scene = _cveImreadAndCheck("Pic4cards\cards.jpg", $CV_IMREAD_COLOR) ; query image - scene Local $gray_scene = _cveMatCreate() _cveCvtColorMat($img_scene, $gray_scene, $CV_COLOR_BGR2GRAY) Local $Gaus_scene = _cveMatCreate() _cveGaussianBlurMat($gray_scene, $Gaus_scene, _cvSize(3, 3), 0, 0) _cveThresholdMat($Gaus_scene, $Gaus_scene, 200, 255, $CV_THRESH_BINARY) Local $contoursVect = _VectorOfVectorOfPointCreate() Local $contours = _cveOutputArrayFromVectorOfVectorOfPoint($contoursVect) Local $hierarchyMat = _cveMatCreate() Local $hierarchy = _cveOutputArrayFromMat($hierarchyMat) Local $io_arr_Gaus_scene = _cveInputOutputArrayFromMat($Gaus_scene) _cveFindContours($io_arr_Gaus_scene, $contours, $hierarchy, $CV_RETR_EXTERNAL, $CV_CHAIN_APPROX_SIMPLE) Local $cnts = _VectorOfVectorOfPointGetSize($contoursVect) ConsoleWrite("Found " & $cnts & " countours" & @CRLF & @CRLF) Local $tVectorPointPtr = DllStructCreate("ptr value") Local $tPointPtr = DllStructCreate("ptr value") Local $tPoint ;;-- Step 1: Detect the keypoints using ORB Detector, compute the descriptors Local $ORB_DETECTOR = 0 Local $BRISK_DETECTOR = 1 Local $KAZE_DETECTOR = 2 Local $algorithm = $ORB_DETECTOR Local $tFeature2DPtr = DllStructCreate("ptr value") Local $tSharedPtr = DllStructCreate("ptr") Local $destructor Switch $algorithm Case $ORB_DETECTOR _cveOrbCreate(100000, 1.2, 8, 31, 0, 2, $CV_ORB_HARRIS_SCORE, 31, 20, $tFeature2DPtr, $tSharedPtr) $destructor = "_cveOrbRelease" Case $BRISK_DETECTOR _cveBriskCreate(30, 3, 1, $tFeature2DPtr, $tSharedPtr) $destructor = "_cveBriskRelease" Case $KAZE_DETECTOR ;$can_compute = $match_type <> $CV_NORM_HAMMING And $match_type <> $CV_NORM_HAMMING2 _cveKAZEDetectorCreate(False, False, 0.001, 4, 4, $CV_KAZE_DIFF_PM_G2, $tFeature2DPtr, $tSharedPtr) $destructor = "_cveKAZEDetectorRelease" EndSwitch Local $boundingRect = _cvRect() Local $CurrentMatchesNumber = 0 Local $x = 0, $y = 0, $w = 0, $h = 0 ConsoleWrite("Loops number:" & _VectorOfVectorOfPointGetSize($contoursVect) - 1 & @CRLF) For $i = 0 To _VectorOfVectorOfPointGetSize($contoursVect) - 1 ConsoleWrite("This is Loop " & $i & @CRLF) _VectorOfVectorOfPointGetItemPtr($contoursVect, $i, $tVectorPointPtr) Local $contour = _cveInputArrayFromVectorOfPoint($tVectorPointPtr.value) _cveBoundingRectangle($contour, $boundingRect) ConsoleWrite("$boundingRect[" & $i & "] = (" & $boundingRect.x & ", " & $boundingRect.y & ", " & $boundingRect.width & ", " & $boundingRect.height & ")" & @CRLF) ; _WinAPI_DisplayStruct($boundingRect, $tagCvRect) For $j = 0 To _VectorOfPointGetSize($tVectorPointPtr.value) - 1 _VectorOfPointGetItemPtr($tVectorPointPtr.value, $j, $tPointPtr) $tPoint = DllStructCreate($tagCvPoint, $tPointPtr.value) ;ConsoleWrite("$cnts[" & $i & "][" & $j & "] = (" & $tPoint.x & ", " & $tPoint.y & ")" & @CRLF) Next ;ConsoleWrite(@CRLF) Local $cropped = _cveMatCreateFromRect($img_scene, $boundingRect) _cveImshowMat("", $cropped) _cveWaitKey() _cveDestroyAllWindows() Local $detector = $tFeature2DPtr.value Local $keypoints_object = _VectorOfKeyPointCreate() Local $keypoints_scene = _VectorOfKeyPointCreate() Local $descriptors_object = _cveMatCreate() Local $descriptors_scene = _cveMatCreate() _CvFeature2DDetectAndComputeMat($detector, $gray_obj, _cveNoArrayMat(), $keypoints_object, $descriptors_object, False) Local $KeypointsNumber = _VectorOfKeyPointGetSize($keypoints_object) ConsoleWrite("Object keypoints: " & $KeypointsNumber & @CRLF) _CvFeature2DDetectAndComputeMat($detector, $cropped, _cveNoArrayMat(), $keypoints_scene, $descriptors_scene, False) Local $KeyPSceneNumber = _VectorOfKeyPointGetSize($keypoints_scene) ConsoleWrite("Scene keypoints: " & $KeyPSceneNumber & @CRLF) ;;-- Step 2: Matching descriptor vectors with a BruteForce based matcher ;; Since ORB is a floating-point descriptor NORM_L2 is used Local $tMatcherPtr = DllStructCreate("ptr value") If $algorithm = $KAZE_DETECTOR Then Local $bf_matcher = _cveBFMatcherCreate($CV_NORM_L2, False, $tMatcherPtr) Else Local $bf_matcher = _cveBFMatcherCreate($CV_NORM_HAMMING, False, $tMatcherPtr) EndIf Local $matcher = $tMatcherPtr.value Local $knn_matches = _VectorOfVectorOfDMatchCreate() ; _cveDescriptorMatcherKnnMatch1Mat($matcher, $descriptors_object, $descriptors_scene, $knn_matches, 2, _cveNoArrayMat(), False) ; Local $MatchesNumber = _VectorOfDMatchGetSize($knn_matches) ConsoleWrite("matches: " & $MatchesNumber & @CRLF) ;;-- Filter matches using the Lowe's ratio test Local $ratio_thresh = 0.75 ; Local $good_matches = _VectorOfDMatchCreate() ; Local $tVectorDMatchPtr = DllStructCreate("ptr value") Local $tDMatchPtr0 = DllStructCreate("ptr value") Local $tDMatchPtr1 = DllStructCreate("ptr value") For $i = 0 To _VectorOfVectorOfDMatchGetSize($knn_matches) - 1 _VectorOfVectorOfDMatchGetItemPtr($knn_matches, $i, $tVectorDMatchPtr) _VectorOfDMatchGetItemPtr($tVectorDMatchPtr.value, 0, $tDMatchPtr0) Local $tDMatch0 = DllStructCreate($tagCvDMatch, $tDMatchPtr0.value) _VectorOfDMatchGetItemPtr($tVectorDMatchPtr.value, 1, $tDMatchPtr1) Local $tDMatch1 = DllStructCreate($tagCvDMatch, $tDMatchPtr1.value) If $tDMatch0.distance < $ratio_thresh * $tDMatch1.distance Then _VectorOfDMatchPush($good_matches, $tDMatch0) EndIf Next Local $GoodMatchesNumber = _VectorOfDMatchGetSize($good_matches) ConsoleWrite("Good matches: " & $GoodMatchesNumber & @CRLF) If $GoodMatchesNumber > $CurrentMatchesNumber Then $CurrentMatchesNumber = $GoodMatchesNumber $x = $boundingRect.x $y = $boundingRect.y $w = $boundingRect.width $h = $boundingRect.height EndIf ConsoleWrite("CurrentMatchesNumber:" & $CurrentMatchesNumber & @CRLF) Next Local $tMatchRect = _cvRect($x, $y, $w, $h) ; Draw a red rectangle around the matched position _cveRectangleMat($img_scene, $tMatchRect, _cvRGB(255, 0, 0), 2, $CV_LINE_8, 0) _cveImshowMat("", $img_scene) _cveWaitKey() _cveDestroyAllWindows() _Opencv_DLLClose()