#Region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_UseX64=y #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** Opt("MustDeclareVars", 1) #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\cards.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 For $i = 0 To _VectorOfVectorOfPointGetSize($contoursVect) - 1 _VectorOfVectorOfPointGetItemPtr($contoursVect, $i, $tVectorPointPtr) 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) Next ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Local $bounding = _cveMatCreate() Local $boundingRect = _cveInputArrayFromMat($bounding) _cveBoundingRectangleMat($cnts, $boundingRect) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;-- 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 $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, $boundingRect, _cveNoArrayMat(), $keypoints_scene, $descriptors_scene, False) ;;-- 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 ;;-- Get the corners from the image_1 ( the object to be "detected" ) Local $img_object_size = _cvSize() _cveMatGetSize($img_obj, $img_object_size) Local $obj_corners = _VectorOfPointFCreate() Local $i_arr_obj_corners = _cveInputArrayFromVectorOfPointF($obj_corners) _VectorOfPointFPush($obj_corners, _cvPoint2f(0, 0)) _VectorOfPointFPush($obj_corners, _cvPoint2f($img_object_size.width, 0)) _VectorOfPointFPush($obj_corners, _cvPoint2f($img_object_size.width, $img_object_size.height)) _VectorOfPointFPush($obj_corners, _cvPoint2f(0, $img_object_size.height)) ;; draw found regions Local $tPointFPtr = DllStructCreate("ptr value") Local $scene_corners = _VectorOfPointFCreateSize(4) _VectorOfPointFGetItemPtr($scene_corners, 0, $tPointFPtr) Local $scene_corners_0 = DllStructCreate($tagCvPoint2D32f, $tPointFPtr.value) _VectorOfPointFGetItemPtr($scene_corners, 1, $tPointFPtr) Local $scene_corners_1 = DllStructCreate($tagCvPoint2D32f, $tPointFPtr.value) _VectorOfPointFGetItemPtr($scene_corners, 2, $tPointFPtr) Local $scene_corners_2 = DllStructCreate($tagCvPoint2D32f, $tPointFPtr.value) _VectorOfPointFGetItemPtr($scene_corners, 3, $tPointFPtr) Local $scene_corners_3 = DllStructCreate($tagCvPoint2D32f, $tPointFPtr.value) ;;-- Draw lines between the corners (the mapped object in the scene - image_2 ) _cveLineMat($img_scene, _cvPoint($scene_corners_0.x + $img_object_size.width, $scene_corners_0.y), _ _cvPoint($scene_corners_1.x + $img_object_size.width, $scene_corners_1.y), _cvScalar(0, 255, 0), 4) ; _cveLineMat($img_scene, _cvPoint($scene_corners_1.x + $img_object_size.width, $scene_corners_1.y), _ _cvPoint($scene_corners_2.x + $img_object_size.width, $scene_corners_2.y), _cvScalar(0, 255, 0), 4) ; _cveLineMat($img_scene, _cvPoint($scene_corners_2.x + $img_object_size.width, $scene_corners_2.y), _ _cvPoint($scene_corners_3.x + $img_object_size.width, $scene_corners_3.y), _cvScalar(0, 255, 0), 4) ; _cveLineMat($img_scene, _cvPoint($scene_corners_3.x + $img_object_size.width, $scene_corners_3.y), _ _cvPoint($scene_corners_0.x + $img_object_size.width, $scene_corners_0.y), _cvScalar(0, 255, 0), 4) ; _cveImshowMat("", $img_scene) _cveWaitKey() _cveDestroyAllWindows() _Opencv_DLLClose()